feat: Automatically remove devices unpaired by remote host
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
29
main.py
29
main.py
@@ -158,6 +158,32 @@ async def set_adapter_alias(alias):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Failed to set adapter alias: {e}")
|
logging.error(f"Failed to set adapter alias: {e}")
|
||||||
|
|
||||||
|
async def monitor_unpairing():
|
||||||
|
logging.info("Starting to monitor for device unpairing events.")
|
||||||
|
adapter_obj = await get_adapter()
|
||||||
|
if not adapter_obj:
|
||||||
|
logging.error("Bluetooth adapter not found, cannot monitor unpairing.")
|
||||||
|
return
|
||||||
|
|
||||||
|
adapter_iface = adapter_obj.get_interface(ADAPTER_IFACE)
|
||||||
|
|
||||||
|
def _properties_changed_handler(interface_name, changed_properties, invalidated_properties, msg):
|
||||||
|
if interface_name == DEVICE_IFACE and 'Paired' in changed_properties and not changed_properties['Paired'].value:
|
||||||
|
device_path = msg.path
|
||||||
|
logging.info(f"Device {device_path} was unpaired by the remote host. Removing it.")
|
||||||
|
asyncio.create_task(adapter_iface.call_remove_device(device_path))
|
||||||
|
|
||||||
|
bus.add_signal_receiver(
|
||||||
|
_properties_changed_handler,
|
||||||
|
signal_name='PropertiesChanged',
|
||||||
|
dbus_interface='org.freedesktop.DBus.Properties',
|
||||||
|
path_namespace='/org/bluez'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Keep the task alive to listen for signals
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(3600)
|
||||||
|
|
||||||
# --- End Bluetooth ---
|
# --- End Bluetooth ---
|
||||||
|
|
||||||
async def manage_bluetooth():
|
async def manage_bluetooth():
|
||||||
@@ -225,7 +251,8 @@ async def main():
|
|||||||
|
|
||||||
manage_task = asyncio.create_task(manage_bluetooth())
|
manage_task = asyncio.create_task(manage_bluetooth())
|
||||||
mqtt_task = asyncio.create_task(fetch_mqtt())
|
mqtt_task = asyncio.create_task(fetch_mqtt())
|
||||||
await asyncio.gather(manage_task, mqtt_task)
|
unpair_task = asyncio.create_task(monitor_unpairing())
|
||||||
|
await asyncio.gather(manage_task, mqtt_task, unpair_task)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user