Skip to content

Commit 11665f1

Browse files
ashabalaagittins
authored andcommitted
Add scanner_entity_id attribute to BermudaSensorScanner
1 parent 59dae04 commit 11665f1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

custom_components/bermuda/coordinator.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def __init__(
160160
self._entity_registry = er.async_get(self.hass)
161161
self._device_registry = dr.async_get(self.hass)
162162

163+
# Add storage for scanner entity IDs
164+
self.scanner_entity_ids: dict[str, str] = {}
165+
163166
# Track the list of Private BLE devices, noting their entity id
164167
# and current "last address".
165168
self.pb_state_sources: dict[str, str | None] = {}
@@ -1120,6 +1123,9 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11201123
_previous_scannerlist = [device.address for device in self.devices.values() if device.is_scanner]
11211124
_purge_scanners = _previous_scannerlist.copy()
11221125

1126+
# Clear existing mappings to ensure fresh start
1127+
self.scanner_entity_ids.clear()
1128+
11231129
# _LOGGER.error("Preserving %d current scanner entries", len(_previous_scannerlist))
11241130

11251131
# Find active HaBaseScanners in the backend, and only pay attention to those
@@ -1147,6 +1153,28 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11471153
hascanner.source,
11481154
)
11491155
continue
1156+
1157+
if scanner_devreg:
1158+
_LOGGER.debug("Found device entry id: %s", scanner_devreg.id)
1159+
1160+
# Get all entities for this device
1161+
entities = list(self._entity_registry.entities.get_entries_for_device_id(scanner_devreg.id))
1162+
1163+
if len(entities) > 1:
1164+
# Device must have multiple entities. Reduce search space
1165+
entities = [entity for entity in entities if entity.domain in ("switch", "light")]
1166+
if entities:
1167+
# Take the first valid entity
1168+
self.scanner_entity_ids[scanner_address] = entities[0].entity_id
1169+
_LOGGER.debug(
1170+
"Mapped scanner %s (%s) to entity %s",
1171+
scanner_devreg.name,
1172+
scanner_address,
1173+
entities[0].entity_id,
1174+
)
1175+
else:
1176+
_LOGGER.debug("No entity found for scanner %s", scanner_address)
1177+
11501178
# _LOGGER.info("Great! Found scanner: %s (%s)", scanner_ha.name, scanner_ha.source)
11511179
# Since this scanner still exists, we won't purge it
11521180
if scanner_address in _purge_scanners:

custom_components/bermuda/sensor.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ def name(self):
173173
def native_value(self):
174174
return self._device.area_scanner
175175

176+
@property
177+
def extra_state_attributes(self) -> Mapping[str, Any] | None:
178+
"""Return extra state attributes for the nearest scanner."""
179+
attribs = super().extra_state_attributes or {}
180+
181+
if self._device.area_scanner:
182+
for scanner in self.coordinator.scanner_list:
183+
scanner_device = self.coordinator.devices[scanner]
184+
if scanner_device.address.upper() in self._device.area_scanner:
185+
if scanner_device.address in self.coordinator.scanner_entity_ids:
186+
attribs["scanner_entity_id"] = self.coordinator.scanner_entity_ids[scanner_device.address]
187+
_LOGGER.debug(
188+
"Set scanner_entity_id to %s for device %s", attribs["scanner_entity_id"], self._device.name
189+
)
190+
break
191+
192+
return attribs
193+
176194

177195
class BermudaSensorRssi(BermudaSensor):
178196
"""Sensor for RSSI of closest scanner."""

0 commit comments

Comments
 (0)