Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: speed up dispatching bleak callbacks #46

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Check warning on line 74 in src/habluetooth/manager.py

View check run for this annotation

Codecov / codecov/patch

src/habluetooth/manager.py#L74

Added line #L74 was not covered by tests
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Error in callback: %s", callback)
_LOGGER.exception("Error in callback: %s", bleak_callback.callback)

Check warning on line 76 in src/habluetooth/manager.py

View check run for this annotation

Codecov / codecov/patch

src/habluetooth/manager.py#L76

Added line #L76 was not covered by tests


class BleakCallback:
Expand Down Expand Up @@ -454,9 +449,10 @@
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

Check warning on line 455 in src/habluetooth/manager.py

View check run for this annotation

Codecov / codecov/patch

src/habluetooth/manager.py#L455

Added line #L455 was not covered by tests
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 @@
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)

Check warning on line 576 in src/habluetooth/manager.py

View check run for this annotation

Codecov / codecov/patch

src/habluetooth/manager.py#L576

Added line #L576 was not covered by tests

self._discover_service_info(service_info)

Expand Down Expand Up @@ -710,7 +701,7 @@
# 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
Loading