refactor: Remove automatic device unpairing monitor
This commit is contained in:
39
main.py
39
main.py
@@ -145,40 +145,6 @@ async def set_adapter_alias(alias):
|
||||
except Exception as 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(msg):
|
||||
if msg.message_type != MessageType.SIGNAL or msg.member != 'PropertiesChanged' or msg.interface != 'org.freedesktop.DBus.Properties':
|
||||
return
|
||||
|
||||
interface_name, changed_properties, invalidated_properties = msg.body
|
||||
|
||||
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_message_handler(_properties_changed_handler)
|
||||
|
||||
await bus.call(
|
||||
Message(
|
||||
destination='org.freedesktop.DBus',
|
||||
path='/org/freedesktop/DBus',
|
||||
interface='org.freedesktop.DBus',
|
||||
member='AddMatch',
|
||||
signature='s',
|
||||
body=[f"type='signal',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path_namespace='/org/bluez'"]))
|
||||
|
||||
# Keep the task alive to listen for signals
|
||||
while True:
|
||||
await asyncio.sleep(3600)
|
||||
|
||||
# --- End Bluetooth ---
|
||||
|
||||
@@ -241,7 +207,7 @@ def suppress_hfp_rejection_error(loop, context):
|
||||
exception = context.get('exception')
|
||||
if isinstance(exception, DBusError) and 'HFP profile not supported' in str(exception):
|
||||
# This is the expected error from AuthorizeService, so we can suppress the traceback.
|
||||
logging.debug('Suppressed expected DBusError for HFP rejection.')
|
||||
logging.info('Suppressed expected DBusError for HFP rejection exception.')
|
||||
return
|
||||
|
||||
# For all other exceptions, fall back to the default handler.
|
||||
@@ -261,8 +227,7 @@ async def main():
|
||||
|
||||
manage_task = asyncio.create_task(manage_bluetooth())
|
||||
mqtt_task = asyncio.create_task(fetch_mqtt())
|
||||
unpair_task = asyncio.create_task(monitor_unpairing())
|
||||
await asyncio.gather(manage_task, mqtt_task, unpair_task)
|
||||
await asyncio.gather(manage_task, mqtt_task)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user