From a9034e440ea99886e892541712f2dfd9b1964a92 Mon Sep 17 00:00:00 2001 From: rencsaridogan Date: Mon, 29 Jun 2026 10:36:31 +0200 Subject: [PATCH] fix: derive device MAC from BLE local name on EU/NA v2 API OMRON's EU/NA v2 init-user response does not include a `macAddress` field for registered devices, so `get_registered_devices` dropped every device via the "skip device without MAC address" guard, leaving the device list empty even when the account had active devices. OMRON encodes the MAC in the BLE local name (e.g. `blesmart_0001020828ffb210aaa4` -> `28:FF:B2:10:AA:A4`), the same value the Bluetooth scanner derives. Fall back to that local name, then to `deviceSerialID` via `serial_to_mac`, before skipping. The derivation matches `omramin.omron_ble_scan`, so the resulting MAC is consistent with BLE discovery and manual `add --macaddr`. Co-Authored-By: Claude Opus 4.8 --- omronconnect.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/omronconnect.py b/omronconnect.py index e084cf1..1eab46f 100644 --- a/omronconnect.py +++ b/omronconnect.py @@ -278,6 +278,17 @@ def serial_to_mac(serial: str) -> str: return ":".join(values[5:2:-1] + values[2::-1]) +def ble_local_name_to_mac(local_name: str) -> str: + # e.g. blesmart_0001020828ffb210aaa4 to 28:FF:B2:10:AA:A4 + # OMRON encodes the MAC as the last 12 hex chars of the BLE local name. + # Mirrors the BLE-scan derivation in omramin.omron_ble_scan. + name = (local_name or "").strip() + mac_hex = name[-12:].upper() + if len(mac_hex) != 12 or not all(c in "0123456789ABCDEF" for c in mac_hex): + return "" + return ":".join(mac_hex[i : i + 2] for i in range(0, 12, 2)) + + def convert_weight_to_kg(weight: T.Union[int, float], unit: int) -> float: if unit == WeightUnit.G: return weight / 1000 @@ -833,6 +844,14 @@ def get_registered_devices(self, days: T.Optional[int] = 30) -> T.Optional[list[ continue macAddress = attrs.get("macAddress", "").strip() + if not macAddress: + # EU/NA v2 responses omit macAddress; OMRON encodes the MAC in the + # BLE local name (blesmart_<...>) and falls back to deviceSerialID. + macAddress = ble_local_name_to_mac(attrs.get("deviceLocalName", "")) + if not macAddress: + serial = attrs.get("deviceSerialID", "") + if serial: + macAddress = serial_to_mac(serial) if not macAddress: L.debug(f"Skipping device without MAC address: {attrs.get('name', 'unknown')}") continue