Skip to content

Commit 787cc82

Browse files
committed
Add scanner_entity_id attribute to BermudaSensorScanner
1 parent 8f905a4 commit 787cc82

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
@@ -158,6 +158,9 @@ def __init__(
158158
self._entity_registry = er.async_get(self.hass)
159159
self._device_registry = dr.async_get(self.hass)
160160

161+
# Add storage for scanner entity IDs
162+
self.scanner_entity_ids: dict[str, str] = {}
163+
161164
# Track the list of Private BLE devices, noting their entity id
162165
# and current "last address".
163166
self.pb_state_sources: dict[str, str | None] = {}
@@ -1105,6 +1108,9 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11051108
_previous_scannerlist = [device.address for device in self.devices.values() if device.is_scanner]
11061109
_purge_scanners = _previous_scannerlist.copy()
11071110

1111+
# Clear existing mappings to ensure fresh start
1112+
self.scanner_entity_ids.clear()
1113+
11081114
# _LOGGER.error("Preserving %d current scanner entries", len(_previous_scannerlist))
11091115

11101116
# Find active HaBaseScanners in the backend, and only pay attention to those
@@ -1132,6 +1138,28 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
11321138
hascanner.source,
11331139
)
11341140
continue
1141+
1142+
if scanner_devreg:
1143+
_LOGGER.debug("Found device entry id: %s", scanner_devreg.id)
1144+
1145+
# Get all entities for this device
1146+
entities = list(self._entity_registry.entities.get_entries_for_device_id(scanner_devreg.id))
1147+
1148+
if len(entities) > 1:
1149+
# Device must have multiple entities. Reduce search space
1150+
entities = [entity for entity in entities if entity.domain in ("switch", "light")]
1151+
if entities:
1152+
# Take the first valid entity
1153+
self.scanner_entity_ids[scanner_address] = entities[0].entity_id
1154+
_LOGGER.debug(
1155+
"Mapped scanner %s (%s) to entity %s",
1156+
scanner_devreg.name,
1157+
scanner_address,
1158+
entities[0].entity_id,
1159+
)
1160+
else:
1161+
_LOGGER.debug("No entity found for scanner %s", scanner_address)
1162+
11351163
# _LOGGER.info("Great! Found scanner: %s (%s)", scanner_ha.name, scanner_ha.source)
11361164
# Since this scanner still exists, we won't purge it
11371165
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)