Skip to content

Commit

Permalink
Merge pull request #42 from agittins/dev
Browse files Browse the repository at this point in the history
(fix) Ignore stale adverts for area presence
  • Loading branch information
agittins authored Oct 12, 2023
2 parents b891b4a + 395f9bb commit cb71cba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
32 changes: 20 additions & 12 deletions custom_components/bermuda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from homeassistant.util.dt import monotonic_time_coarse
from homeassistant.util.dt import now

from .const import ADVERT_FRESHTIME
from .const import CONF_ATTENUATION
from .const import CONF_DEVICES
from .const import CONF_DEVTRACK_TIMEOUT
Expand Down Expand Up @@ -539,24 +540,31 @@ def _refresh_area_by_min_distance(self, device: BermudaDevice):
# whittle down to the closest beacon inside max range
if scanner.rssi_distance < self.options.get(
CONF_MAX_RADIUS, DEFAULT_MAX_RADIUS
): # potential...
if (
closest_scanner is None
or scanner.rssi_distance < closest_scanner.rssi_distance
):
): # It's inside max_radius...
if closest_scanner is None or (
scanner.rssi_distance < closest_scanner.rssi_distance
and 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 not None:
# We found a winner
old_area = device.area_name
device.area_id = closest_scanner.area_id
areas = self.area_reg.async_get_area(
device.area_id
).name # potentially a list?!
if len(areas) == 1:
device.area_name = areas[0]
areas = self.area_reg.async_get_area(device.area_id)
if hasattr(areas, "name"):
device.area_name = areas.name
else:
# none or a list, perhaps...
device.area_name = areas
# Wasn't a single area entry. Let's freak out.
_LOGGER.warning(
"Could not discern area from scanner %s: %s."
"Please assign an area then reload this integration",
closest_scanner.name,
areas,
)
device.area_name = f"No area: {closest_scanner.name}"
device.area_distance = closest_scanner.rssi_distance
if old_area != device.area_name and device.create_sensor:
_LOGGER.debug("Device %s now in %s", device.name, device.area_name)
else:
# Not close to any scanners!
device.area_id = None
Expand Down
5 changes: 5 additions & 0 deletions custom_components/bermuda/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

DOCS = {}

ADVERT_FRESHTIME = 2.5
# If two scanners are battling to "win" a device, the winner can not be more than
# this many seconds older than its opponent. Prevents a stale but very close
# advert from overriding a newer advertisement from a less-close scanner.

# Configuration and options

CONF_DEVICES = "configured_devices"
Expand Down

0 comments on commit cb71cba

Please sign in to comment.