From 54255331d5b1e79227ec0c3fe04afa3da07f1112 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 28 Jun 2023 20:40:03 -0500 Subject: [PATCH] Small cleanups to bluetooth manager advertisement processing (#95453) Avoid a few lookups that are rarely used now --- homeassistant/components/bluetooth/manager.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/bluetooth/manager.py b/homeassistant/components/bluetooth/manager.py index f1221290c74..d1fcb115180 100644 --- a/homeassistant/components/bluetooth/manager.py +++ b/homeassistant/components/bluetooth/manager.py @@ -413,23 +413,20 @@ class BluetoothManager: # Pre-filter noisy apple devices as they can account for 20-35% of the # traffic on a typical network. - advertisement_data = service_info.advertisement - manufacturer_data = advertisement_data.manufacturer_data if ( - len(manufacturer_data) == 1 - and (apple_data := manufacturer_data.get(APPLE_MFR_ID)) - and apple_data[0] not in APPLE_START_BYTES_WANTED - and not advertisement_data.service_data + (manufacturer_data := service_info.manufacturer_data) + and APPLE_MFR_ID in manufacturer_data + and manufacturer_data[APPLE_MFR_ID][0] not in APPLE_START_BYTES_WANTED + and len(manufacturer_data) == 1 + and not service_info.service_data ): return - device = service_info.device - address = device.address + address = service_info.device.address all_history = self._all_history connectable = service_info.connectable connectable_history = self._connectable_history old_connectable_service_info = connectable and connectable_history.get(address) - source = service_info.source # This logic is complex due to the many combinations of scanners # that are supported. @@ -544,13 +541,17 @@ class BluetoothManager: "%s: %s %s match: %s", self._async_describe_source(service_info), address, - advertisement_data, + service_info.advertisement, matched_domains, ) - if connectable or old_connectable_service_info: + if (connectable or old_connectable_service_info) and ( + bleak_callbacks := self._bleak_callbacks + ): # Bleak callbacks must get a connectable device - for callback_filters in self._bleak_callbacks: + device = service_info.device + advertisement_data = service_info.advertisement + for callback_filters in bleak_callbacks: _dispatch_bleak_callback(*callback_filters, device, advertisement_data) for match in self._callback_index.match_callbacks(service_info):