Skip to content

Commit

Permalink
backends/bluezdbus/manager: add device path to condition callbacks
Browse files Browse the repository at this point in the history
This adds a device path key to the condition callbacks. This will avoid
calling callbacks for other devices that are not the subject of the
current "PropertiesChanged" signal.
  • Loading branch information
dlech committed Aug 28, 2023
1 parent 4621d05 commit cd0fda5
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class CallbackAndState(NamedTuple):
"""


DevicePropertiesChangedCallback = Callable[[], None]
"""
A callback that is called when the properties of a device change in BlueZ.
"""


DeviceRemovedCallback = Callable[[str], None]
"""
A callback that is called when a device is removed from BlueZ.
Expand Down Expand Up @@ -168,7 +174,7 @@ def __init__(self):
self._advertisement_callbacks: List[CallbackAndState] = []
self._device_removed_callbacks: List[DeviceRemovedCallbackAndState] = []
self._device_watchers: Set[DeviceWatcher] = set()
self._condition_callbacks: Set[Callable] = set()
self._condition_callbacks: Dict[str, Set[DevicePropertiesChangedCallback]] = {}
self._services_cache: Dict[str, BleakGATTServiceCollection] = {}

def _check_adapter(self, adapter_path: str) -> None:
Expand Down Expand Up @@ -805,13 +811,14 @@ def callback():
):
event.set()

self._condition_callbacks.add(callback)
device_callbacks = self._condition_callbacks.setdefault(device_path, set())
device_callbacks.add(callback)

try:
# can be canceled
await event.wait()
finally:
self._condition_callbacks.remove(callback)
device_callbacks.remove(callback)

def _parse_msg(self, message: Message):
"""
Expand Down Expand Up @@ -945,7 +952,9 @@ def _parse_msg(self, message: Message):
)

# handle device condition watchers
for condition_callback in self._condition_callbacks:
for condition_callback in self._condition_callbacks.get(
message.path, ()
):
condition_callback()

# handle device connection change watchers
Expand Down

0 comments on commit cd0fda5

Please sign in to comment.