Skip to content

Commit

Permalink
feat: speed up dispatching bleak callbacks (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed May 22, 2024
1 parent d6e80c5 commit cbc8b26
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/habluetooth/manager.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ cdef object APPLE_MFR_ID

@cython.locals(uuids=set)
cdef _dispatch_bleak_callback(
object callback,
dict filters,
BleakCallback bleak_callback,
object device,
object advertisement_data
)
Expand Down
29 changes: 10 additions & 19 deletions src/habluetooth/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,20 @@


def _dispatch_bleak_callback(
callback: AdvertisementDataCallback | None,
filters: dict[str, set[str]],
bleak_callback: BleakCallback,
device: BLEDevice,
advertisement_data: AdvertisementData,
) -> None:
"""Dispatch the callback."""
if not callback:
# Callback destroyed right before being called, ignore
return

if (uuids := filters.get(FILTER_UUIDS)) is not None and not uuids.intersection(
advertisement_data.service_uuids
):
if (
uuids := bleak_callback.filters.get(FILTER_UUIDS)
) is not None and not uuids.intersection(advertisement_data.service_uuids):
return

try:
callback(device, advertisement_data)
bleak_callback.callback(device, advertisement_data)
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Error in callback: %s", callback)
_LOGGER.exception("Error in callback: %s", bleak_callback.callback)


class BleakCallback:
Expand Down Expand Up @@ -454,9 +449,10 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
return

address = service_info.address
old_connectable_service_info = None
if connectable := service_info.connectable:
old_connectable_service_info = self._connectable_history.get(address)
else:
old_connectable_service_info = None
source = service_info.source
# This logic is complex due to the many combinations of scanners
# that are supported.
Expand Down Expand Up @@ -577,12 +573,7 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
device = service_info.device
advertisement_data = service_info.advertisement
for bleak_callback in bleak_callbacks:
_dispatch_bleak_callback(
bleak_callback.callback,
bleak_callback.filters,
device,
advertisement_data,
)
_dispatch_bleak_callback(bleak_callback, device, advertisement_data)

self._discover_service_info(service_info)

Expand Down Expand Up @@ -710,7 +701,7 @@ def async_register_bleak_callback(
# or we are in passive mode
for history in self._connectable_history.values():
_dispatch_bleak_callback(
callback, filters, history.device, history.advertisement
callback_entry, history.device, history.advertisement
)

return partial(self._bleak_callbacks.remove, callback_entry)
Expand Down

0 comments on commit cbc8b26

Please sign in to comment.