From fec1b86d0d835f205b5bb334ce83c332102c1f75 Mon Sep 17 00:00:00 2001 From: Alex Shabala Date: Tue, 12 Nov 2024 15:33:03 +0200 Subject: [PATCH] Refactor: Move nearest scanner entity_id as attribute of existing nearest scanner sensor --- custom_components/bermuda/sensor.py | 74 +++++++++++------------------ 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/custom_components/bermuda/sensor.py b/custom_components/bermuda/sensor.py index 92fbefe..086acd9 100644 --- a/custom_components/bermuda/sensor.py +++ b/custom_components/bermuda/sensor.py @@ -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)) @@ -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.""" @@ -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