Skip to content

Commit

Permalink
feat: refactor scanner_adv_received to reduce ref counting (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Feb 1, 2025
1 parent ddb5fe9 commit a1945ce
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/habluetooth/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,6 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
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.
#
Expand All @@ -529,8 +528,8 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
#
if (
(old_service_info := self._all_history.get(address)) is not None
and source != (old_source := old_service_info.source)
and (scanner := self._sources.get(old_source)) is not None
and service_info.source != old_service_info.source
and (scanner := self._sources.get(old_service_info.source)) is not None
and scanner.scanning
and self._prefer_previous_adv_from_different_source(
address, old_service_info, service_info
Expand All @@ -549,11 +548,10 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
# source, we need to check it as well to see if we prefer
# the old connectable advertisement
or (
(old_connectable_source := old_connectable_service_info.source)
!= source
old_connectable_service_info.source != service_info.source
and (
connectable_scanner := self._sources.get(
old_connectable_source
old_connectable_service_info.source
)
)
is not None
Expand All @@ -576,14 +574,13 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:

# Track advertisement intervals to determine when we need to
# switch adapters or mark a device as unavailable
tracker = self._advertisement_tracker
if (
last_source := tracker.sources.get(address)
) is not None and last_source != source:
last_source := self._advertisement_tracker.sources.get(address)
) is not None and last_source != service_info.source:
# Source changed, remove the old address from the tracker
tracker.async_remove_address(address)
if address not in tracker.intervals:
tracker.async_collect(service_info)
self._advertisement_tracker.async_remove_address(address)
if address not in self._advertisement_tracker.intervals:
self._advertisement_tracker.async_collect(service_info)

# If the advertisement data is the same as the last time we saw it, we
# don't need to do anything else unless its connectable and we are missing
Expand Down Expand Up @@ -615,7 +612,7 @@ def scanner_adv_received(self, service_info: BluetoothServiceInfoBleak) -> None:
service_info.manufacturer_data,
service_info.service_data,
service_info.service_uuids,
source,
service_info.source,
service_info.device,
service_info._advertisement,
True,
Expand Down

0 comments on commit a1945ce

Please sign in to comment.