Optimize bluetooth base scanners for python3.11+ (#96165)

This commit is contained in:
J. Nick Koston 2023-07-09 12:56:33 -10:00 committed by GitHub
parent 7390e3a997
commit e8397063d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -303,7 +303,19 @@ class BaseHaRemoteScanner(BaseHaScanner):
) -> None:
"""Call the registered callback."""
self._last_detection = advertisement_monotonic_time
if prev_discovery := self._discovered_device_advertisement_datas.get(address):
try:
prev_discovery = self._discovered_device_advertisement_datas[address]
except KeyError:
# We expect this is the rare case and since py3.11+ has
# near zero cost try on success, and we can avoid .get()
# which is slower than [] we use the try/except pattern.
device = BLEDevice(
address=address,
name=local_name,
details=self._details | details,
rssi=rssi, # deprecated, will be removed in newer bleak
)
else:
# Merge the new data with the old data
# to function the same as BlueZ which
# merges the dicts on PropertiesChanged
@ -344,13 +356,6 @@ class BaseHaRemoteScanner(BaseHaScanner):
device.details = self._details | details
# pylint: disable-next=protected-access
device._rssi = rssi # deprecated, will be removed in newer bleak
else:
device = BLEDevice(
address=address,
name=local_name,
details=self._details | details,
rssi=rssi, # deprecated, will be removed in newer bleak
)
advertisement_data = AdvertisementData(
local_name=None if local_name == "" else local_name,