Fire bluetooth listener for all matching devices (#80440)

* Fire listener for all matching devices

* Add test case for seen device

* Avoid looping all data if we have address match

* Initialize to empty list
This commit is contained in:
Joakim Plate 2022-10-16 21:49:12 +02:00 committed by GitHub
parent e71bd2c20b
commit 9eb4faf037
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 10 deletions

View file

@ -483,15 +483,19 @@ class BluetoothManager:
# immediately with the last packet so the subscriber can see the
# device.
all_history = self._get_history_by_type(connectable)
if (
(address := callback_matcher.get(ADDRESS))
and (service_info := all_history.get(address))
and ble_device_matches(callback_matcher, service_info)
):
try:
callback(service_info, BluetoothChange.ADVERTISEMENT)
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Error in bluetooth callback")
service_infos: Iterable[BluetoothServiceInfoBleak] = []
if address := callback_matcher.get(ADDRESS):
if service_info := all_history.get(address):
service_infos = [service_info]
else:
service_infos = all_history.values()
for service_info in service_infos:
if ble_device_matches(callback_matcher, service_info):
try:
callback(service_info, BluetoothChange.ADVERTISEMENT)
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Error in bluetooth callback")
return _async_remove_callback