Skip to content

Commit

Permalink
(fix) Still breaking head against adv logic
Browse files Browse the repository at this point in the history
  • Loading branch information
agittins committed Oct 12, 2023
1 parent 64c39d5 commit 66fe3a9
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions custom_components/bermuda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,29 @@ def _refresh_area_by_min_distance(self, device: BermudaDevice):
if scanner.rssi_distance < self.options.get(
CONF_MAX_RADIUS, DEFAULT_MAX_RADIUS
): # It's inside max_radius...
if closest_scanner is None or (
scanner.rssi_distance < closest_scanner.rssi_distance
or scanner.stamp > closest_scanner.stamp - ADVERT_FRESHTIME
): # This scanner is closer, and the advert is still fresh in comparison..
closest_scanner = scanner
if closest_scanner is None:
# no encumbent, we win! (unless we don't have a stamp to validate our claim)
# FIXME: This effectively excludes HCI/usb adaptors currently since we
# haven't found a way to get ad timestamps from HA's bluez yet.
if scanner.stamp > 0:
closest_scanner = scanner
else:
# is it fresh enough to win on proximity alone?
is_fresh_enough = (
scanner.stamp > closest_scanner.stamp - ADVERT_FRESHTIME
)
# is it so much fresher that it wins outright?
is_fresher = (
scanner.stamp > closest_scanner.stamp + ADVERT_FRESHTIME
)
# is it closer?
is_closer = scanner.rssi_distance < closest_scanner.rssi_distance

if is_fresher or (
is_closer and is_fresh_enough
): # This scanner is closer, and the advert is still fresh in comparison..
closest_scanner = scanner

if closest_scanner is not None:
# We found a winner
old_area = device.area_name
Expand Down

0 comments on commit 66fe3a9

Please sign in to comment.