From b300845c82b4d62f941b5e589663fdb67b760d3f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 22 May 2024 14:48:52 -1000 Subject: [PATCH] feat: refactor changed_manufacturer_data to improve performance (#19) --- src/bluetooth_sensor_state_data/__init__.py | 25 +++++++++------------ 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/bluetooth_sensor_state_data/__init__.py b/src/bluetooth_sensor_state_data/__init__.py index 7f74570..b6f5933 100644 --- a/src/bluetooth_sensor_state_data/__init__.py +++ b/src/bluetooth_sensor_state_data/__init__.py @@ -18,9 +18,7 @@ class BluetoothData(SensorData): def __init__(self) -> None: """Initialize the class.""" super().__init__() - self._last_manufacturer_data_set_by_source: dict[ - str, set[tuple[int, bytes]] - ] = {} + self._last_manufacturer_data_by_source: dict[str, dict[int, bytes]] = {} def changed_manufacturer_data( self, data: BluetoothServiceInfo, exclude_ids: set[int] | None = None @@ -32,30 +30,27 @@ def changed_manufacturer_data( """ manufacturer_data = data.manufacturer_data source = data.source + last_manufacturer_data = self._last_manufacturer_data_by_source.get(source) - last_manufacturer_data_set = ( - self._last_manufacturer_data_set_by_source.setdefault(source, set()) - ) if exclude_ids: # If there are specific manufacturer data IDs to exclude, # then remove them from the set of manufacturer data. - manufacturer_data_set = { - key_val - for key_val in manufacturer_data.items() - if key_val[0] not in exclude_ids + new_manufacturer_data = { + k: v for k, v in manufacturer_data.items() if k not in exclude_ids } else: - manufacturer_data_set = set(manufacturer_data.items()) - self._last_manufacturer_data_set_by_source[source] = manufacturer_data_set + new_manufacturer_data = manufacturer_data - if not last_manufacturer_data_set: + self._last_manufacturer_data_by_source[source] = manufacturer_data + + if not last_manufacturer_data: # If there is no previous data and there is only one value # return it return ( - dict(manufacturer_data_set) if len(manufacturer_data_set) == 1 else {} + new_manufacturer_data.copy() if len(new_manufacturer_data) == 1 else {} ) - return dict(manufacturer_data_set - last_manufacturer_data_set) + return dict(new_manufacturer_data.items() - last_manufacturer_data.items()) @abstractmethod def _start_update(self, data: BluetoothServiceInfo) -> None: