diff --git a/src/habluetooth/manager.pxd b/src/habluetooth/manager.pxd index 6945e72..41c77d6 100644 --- a/src/habluetooth/manager.pxd +++ b/src/habluetooth/manager.pxd @@ -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 ) diff --git a/src/habluetooth/manager.py b/src/habluetooth/manager.py index da67633..d361416 100644 --- a/src/habluetooth/manager.py +++ b/src/habluetooth/manager.py @@ -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: @@ -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. @@ -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) @@ -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)