Skip to content

Commit

Permalink
Refactor: Move nearest scanner entity_id as attribute of existing nea…
Browse files Browse the repository at this point in the history
…rest scanner sensor
  • Loading branch information
Alex Shabala committed Nov 12, 2024
1 parent 26d0999 commit fec1b86
Showing 1 changed file with 28 additions and 46 deletions.
74 changes: 28 additions & 46 deletions custom_components/bermuda/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def device_new(address: str, scanners: list[str]) -> None:
entities.append(BermudaSensorRange(coordinator, entry, address))
entities.append(BermudaSensorScanner(coordinator, entry, address))
entities.append(BermudaSensorRssi(coordinator, entry, address))
entities.append(BermudaSensorScannerEntity(coordinator, entry, address))

for scanner in scanners:
entities.append(BermudaSensorScannerRange(coordinator, entry, address, scanner))
Expand Down Expand Up @@ -176,6 +175,34 @@ def name(self):
def native_value(self):
return self._device.area_scanner

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
attributes = super().extra_state_attributes or {}

scanner_mac = next(
(mac for mac in self._device.scanners if mac.upper() in self._device.area_scanner.upper()),
None,
)

if scanner_mac:
device = self.devreg.async_get_device({}, {(dr.CONNECTION_NETWORK_MAC, scanner_mac.lower())})
if device:
ent_reg = er.async_get(self.hass)
entities = [
entry.entity_id
for entry in ent_reg.entities.values()
if entry.domain
in [
"light",
"switch",
] # Filter out devices unlikely to be bluetooth proxies
and entry.device_id == device.id
]
if entities:
attributes["nearest_scanner_entity_id"] = entities[0]

return attributes


class BermudaSensorRssi(BermudaSensor):
"""Sensor for RSSI of closest scanner."""
Expand Down Expand Up @@ -432,48 +459,3 @@ def native_value(self) -> int:
def name(self):
"""Gets the name of the sensor."""
return "Visible device count"


class BermudaSensorScannerEntity(BermudaSensor):
"""Sensor for entity ID of nearest detected scanner."""

@property
def unique_id(self):
return f"{self._device.unique_id}_scanner_entity"

@property
def name(self):
return "Nearest Scanner Entity"

@property
def native_value(self):
scanner_mac = next(
(mac for mac in self._device.scanners if mac.upper() in self._device.area_scanner.upper()), None
)

if scanner_mac:
device = self.devreg.async_get_device({}, {(dr.CONNECTION_NETWORK_MAC, scanner_mac.lower())})
if device:
# Get light/switch entity for this device
ent_reg = er.async_get(self.hass)
entities = [
entry.entity_id
for entry in ent_reg.entities.values()
if entry.domain in ["light", "switch"] # Filter out devices unlikely to be bluetooth proxies
and entry.device_id == device.id
]
if entities:
return entities[0]
return None

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
scanner_mac = next(
(mac for mac in self._device.scanners if mac.upper() in self._device.area_scanner.upper()), None
)

if scanner_mac:
device = self.devreg.async_get_device({}, {(dr.CONNECTION_NETWORK_MAC, scanner_mac.lower())})
if device:
return {"scanner_mac": scanner_mac, "device_name": device.name, "area_id": device.area_id}
return None

0 comments on commit fec1b86

Please sign in to comment.