Skip to content

Commit fec1b86

Browse files
author
Alex Shabala
committed
Refactor: Move nearest scanner entity_id as attribute of existing nearest scanner sensor
1 parent 26d0999 commit fec1b86

File tree

1 file changed

+28
-46
lines changed

1 file changed

+28
-46
lines changed

custom_components/bermuda/sensor.py

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def device_new(address: str, scanners: list[str]) -> None:
6060
entities.append(BermudaSensorRange(coordinator, entry, address))
6161
entities.append(BermudaSensorScanner(coordinator, entry, address))
6262
entities.append(BermudaSensorRssi(coordinator, entry, address))
63-
entities.append(BermudaSensorScannerEntity(coordinator, entry, address))
6463

6564
for scanner in scanners:
6665
entities.append(BermudaSensorScannerRange(coordinator, entry, address, scanner))
@@ -176,6 +175,34 @@ def name(self):
176175
def native_value(self):
177176
return self._device.area_scanner
178177

178+
@property
179+
def extra_state_attributes(self) -> Mapping[str, Any] | None:
180+
attributes = super().extra_state_attributes or {}
181+
182+
scanner_mac = next(
183+
(mac for mac in self._device.scanners if mac.upper() in self._device.area_scanner.upper()),
184+
None,
185+
)
186+
187+
if scanner_mac:
188+
device = self.devreg.async_get_device({}, {(dr.CONNECTION_NETWORK_MAC, scanner_mac.lower())})
189+
if device:
190+
ent_reg = er.async_get(self.hass)
191+
entities = [
192+
entry.entity_id
193+
for entry in ent_reg.entities.values()
194+
if entry.domain
195+
in [
196+
"light",
197+
"switch",
198+
] # Filter out devices unlikely to be bluetooth proxies
199+
and entry.device_id == device.id
200+
]
201+
if entities:
202+
attributes["nearest_scanner_entity_id"] = entities[0]
203+
204+
return attributes
205+
179206

180207
class BermudaSensorRssi(BermudaSensor):
181208
"""Sensor for RSSI of closest scanner."""
@@ -432,48 +459,3 @@ def native_value(self) -> int:
432459
def name(self):
433460
"""Gets the name of the sensor."""
434461
return "Visible device count"
435-
436-
437-
class BermudaSensorScannerEntity(BermudaSensor):
438-
"""Sensor for entity ID of nearest detected scanner."""
439-
440-
@property
441-
def unique_id(self):
442-
return f"{self._device.unique_id}_scanner_entity"
443-
444-
@property
445-
def name(self):
446-
return "Nearest Scanner Entity"
447-
448-
@property
449-
def native_value(self):
450-
scanner_mac = next(
451-
(mac for mac in self._device.scanners if mac.upper() in self._device.area_scanner.upper()), None
452-
)
453-
454-
if scanner_mac:
455-
device = self.devreg.async_get_device({}, {(dr.CONNECTION_NETWORK_MAC, scanner_mac.lower())})
456-
if device:
457-
# Get light/switch entity for this device
458-
ent_reg = er.async_get(self.hass)
459-
entities = [
460-
entry.entity_id
461-
for entry in ent_reg.entities.values()
462-
if entry.domain in ["light", "switch"] # Filter out devices unlikely to be bluetooth proxies
463-
and entry.device_id == device.id
464-
]
465-
if entities:
466-
return entities[0]
467-
return None
468-
469-
@property
470-
def extra_state_attributes(self) -> Mapping[str, Any] | None:
471-
scanner_mac = next(
472-
(mac for mac in self._device.scanners if mac.upper() in self._device.area_scanner.upper()), None
473-
)
474-
475-
if scanner_mac:
476-
device = self.devreg.async_get_device({}, {(dr.CONNECTION_NETWORK_MAC, scanner_mac.lower())})
477-
if device:
478-
return {"scanner_mac": scanner_mac, "device_name": device.name, "area_id": device.area_id}
479-
return None

0 commit comments

Comments
 (0)