Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Nearest scanner entity_id sensor #374

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions custom_components/bermuda/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
UnitOfLength,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect

from .const import (
Expand Down Expand Up @@ -173,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