From fbf9d1c54ff7d13b1337401f2600d7a96975a0a0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 20 Aug 2024 22:37:47 -0500 Subject: [PATCH] Index BlueZ advertisement_callbacks by adapter - Instead of doing a linear search of all the callbacks to find the one for the adapter, store them in a dict so the adapter path can be looked up - Remove unused arg from _run_advertisement_callbacks --- CHANGELOG.rst | 1 + bleak/backends/bluezdbus/manager.py | 62 ++++++++++++----------------- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ab51b831..f4024fc7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,7 @@ and this project adheres to `Semantic Versioning None: # need to remove callbacks first, otherwise we get TxPower # and RSSI properties removed during stop which causes # incorrect advertisement data callbacks - self._advertisement_callbacks.remove(callback_and_state) + self._advertisement_callbacks[adapter_path].remove( + advertisement_callback + ) self._device_removed_callbacks.remove( device_removed_callback_and_state ) @@ -478,7 +467,9 @@ async def stop() -> None: return stop except BaseException: # if starting scanning failed, don't leak the callbacks - self._advertisement_callbacks.remove(callback_and_state) + self._advertisement_callbacks[adapter_path].remove( + advertisement_callback + ) self._device_removed_callbacks.remove(device_removed_callback_and_state) raise @@ -512,8 +503,7 @@ async def passive_scan( # error message. self._check_adapter(adapter_path) - callback_and_state = CallbackAndState(advertisement_callback, adapter_path) - self._advertisement_callbacks.append(callback_and_state) + self._advertisement_callbacks[adapter_path].append(advertisement_callback) device_removed_callback_and_state = DeviceRemovedCallbackAndState( device_removed_callback, adapter_path @@ -556,7 +546,9 @@ async def stop() -> None: # need to remove callbacks first, otherwise we get TxPower # and RSSI properties removed during stop which causes # incorrect advertisement data callbacks - self._advertisement_callbacks.remove(callback_and_state) + self._advertisement_callbacks[adapter_path].remove( + advertisement_callback + ) self._device_removed_callbacks.remove( device_removed_callback_and_state ) @@ -580,7 +572,9 @@ async def stop() -> None: except BaseException: # if starting scanning failed, don't leak the callbacks - self._advertisement_callbacks.remove(callback_and_state) + self._advertisement_callbacks[adapter_path].remove( + advertisement_callback + ) self._device_removed_callbacks.remove(device_removed_callback_and_state) raise @@ -926,7 +920,7 @@ def _parse_msg(self, message: Message) -> None: # devices that only advertise once and then go to sleep for a while. elif interface == defs.DEVICE_INTERFACE: self._run_advertisement_callbacks( - obj_path, cast(Device1, unpacked_props), unpacked_props.keys() + obj_path, cast(Device1, unpacked_props) ) elif message.member == "InterfacesRemoved": obj_path, interfaces = message.body @@ -1003,7 +997,7 @@ def _parse_msg(self, message: Message) -> None: device_path = message_path self._run_advertisement_callbacks( - device_path, cast(Device1, self_interface), changed.keys() + device_path, cast(Device1, self_interface) ) # handle device condition watchers @@ -1035,22 +1029,16 @@ def _parse_msg(self, message: Message) -> None: message_path, new_value ) - def _run_advertisement_callbacks( - self, device_path: str, device: Device1, changed: Iterable[str] - ) -> None: + def _run_advertisement_callbacks(self, device_path: str, device: Device1) -> None: """ Runs any registered advertisement callbacks. Args: device_path: The D-Bus object path of the remote device. device: The current D-Bus properties of the device. - changed: A list of properties that have changed since the last call. """ - for callback, adapter_path in self._advertisement_callbacks: - # filter messages from other adapters - if adapter_path != device["Adapter"]: - continue - + adapter_path = device["Adapter"] + for callback in self._advertisement_callbacks[adapter_path]: callback(device_path, device.copy())