Skip to content

Fix compatibility with Bermuda - #10

Open
FezVrasta wants to merge 5 commits into
kvj:mainfrom
FezVrasta:main
Open

Fix compatibility with Bermuda#10
FezVrasta wants to merge 5 commits into
kvj:mainfrom
FezVrasta:main

Conversation

@FezVrasta

Copy link
Copy Markdown

The upstream integration registers the scanner with source = entry_id — a 32-character UUID string. Several parts of HA and third-party integrations assume the scanner source is a MAC address, which causes two problems:

1. Bermuda BLE Trilateration cannot use the scanner

Bermuda calls mac_norm() and mac_math_offset() on every scanner's source to look up the device in the HA device registry. Applied to a UUID string these produce garbage values, the lookup returns nothing, and Bermuda raises a "scanners without areas" repair issue for the proxy. The scanner is never usable for distance calculations.

Fix: A stable, locally-administered MAC address is deterministically derived from the entry_id via SHA-256. The first 6 bytes of the digest are taken, the locally-administered bit is set and the multicast bit is cleared, giving a consistent xx:xx:xx:xx:xx:xx address that is stable across restarts without persisting any extra state.

2. The device entry does not merge with HA's bluetooth integration entry

Since HA 2025.2, async_register_scanner creates a device registry entry with connections = {("bluetooth", source.upper())}. The upstream sensor.py registered the companion device with identifier key "entry_id" (non-standard) instead of DOMAIN, and without a matching bluetooth connection, so the two entries never merged. This prevented area/name information from being shared between them.

Fix: device_info now uses (DOMAIN, entry_id) as the identifier and adds connections = {("bluetooth", source_mac.upper())}, allowing HA to merge the two device entries. Once you assign an area to the device, Bermuda picks it up automatically.

3. Bermuda shows last advertisement as ~55 years ago

The Android app includes a Unix epoch timestamp (milliseconds) with each advertisement batch. The upstream code divided this by 1000 and passed it directly as advertisement_monotonic_time, but HA expects a time.monotonic() value — seconds since machine boot, typically a small number. Passing ~1.74 billion seconds made every advertisement appear to have been received 55 years in the past, so Bermuda reported distances as stale and unusable.

Fix: The advertisement age is computed as time.time() - ts_ms / 1000 and subtracted from time.monotonic(), correctly anchoring the phone's wall-clock timestamp to the local monotonic clock.

FezVrasta and others added 4 commits March 1, 2026 15:44
- Derive a stable locally-administered MAC from the config entry ID via
  SHA-256 so the scanner source is a valid MAC address instead of a raw
  UUID string. Bermuda's mac_norm() / mac_math_offset() then work
  correctly and the device registry lookup succeeds.
- Fix device_info identifier key from ("entry_id", …) to (DOMAIN, …).
- Add bluetooth connection to device_info so HA's bluetooth integration
  device entry merges with the companion proxy device entry, allowing
  Bermuda to read the area assignment for distance calculations.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The Android app sends a Unix epoch timestamp in milliseconds. Passing it
(divided by 1000) directly as advertisement_monotonic_time made Bermuda
report "last seen ~1.7 billion seconds ago" because HA expects a
time.monotonic() value (machine uptime), not wall-clock time.

Convert by computing the age of the advertisement and subtracting from
time.monotonic(). Fall back to time.monotonic() when no timestamp is
provided.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Converting the phone's Unix timestamp to a local monotonic time assumes
the phone and HA server clocks are in sync. Any drift is added to the
computed advertisement age; a phone running 10 s behind the server makes
every advertisement appear 10 s stale in Bermuda.

Using time.monotonic() stamps each advertisement as received "now" by
HA. The maximum error is the app's batch interval (≤ 3 s), which is
negligible compared to Bermuda's staleness thresholds.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant