Obtain zone entity id in Risco sensors through entity registry (#43007)
This commit is contained in:
parent
1338c4a425
commit
4ed8e209f1
4 changed files with 27 additions and 14 deletions
|
@ -5,8 +5,8 @@ from homeassistant.components.binary_sensor import (
|
||||||
)
|
)
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DATA_ZONES, DOMAIN
|
from .const import DATA_COORDINATOR, DOMAIN
|
||||||
from .entity import RiscoEntity
|
from .entity import RiscoEntity, binary_sensor_unique_id
|
||||||
|
|
||||||
SERVICE_BYPASS_ZONE = "bypass_zone"
|
SERVICE_BYPASS_ZONE = "bypass_zone"
|
||||||
SERVICE_UNBYPASS_ZONE = "unbypass_zone"
|
SERVICE_UNBYPASS_ZONE = "unbypass_zone"
|
||||||
|
@ -21,12 +21,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
)
|
)
|
||||||
|
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
|
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
|
||||||
entities = {
|
entities = [
|
||||||
zone_id: RiscoBinarySensor(coordinator, zone_id, zone)
|
RiscoBinarySensor(coordinator, zone_id, zone)
|
||||||
for zone_id, zone in coordinator.data.zones.items()
|
for zone_id, zone in coordinator.data.zones.items()
|
||||||
}
|
]
|
||||||
hass.data[DOMAIN][config_entry.entry_id][DATA_ZONES] = entities
|
async_add_entities(entities, False)
|
||||||
async_add_entities(entities.values(), False)
|
|
||||||
|
|
||||||
|
|
||||||
class RiscoBinarySensor(BinarySensorEntity, RiscoEntity):
|
class RiscoBinarySensor(BinarySensorEntity, RiscoEntity):
|
||||||
|
@ -58,7 +57,7 @@ class RiscoBinarySensor(BinarySensorEntity, RiscoEntity):
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique id for this zone."""
|
"""Return a unique id for this zone."""
|
||||||
return f"{self._risco.site_uuid}_zone_{self._zone_id}"
|
return binary_sensor_unique_id(self._risco, self._zone_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
|
|
|
@ -11,7 +11,6 @@ DOMAIN = "risco"
|
||||||
RISCO_EVENT = "risco_event"
|
RISCO_EVENT = "risco_event"
|
||||||
|
|
||||||
DATA_COORDINATOR = "risco"
|
DATA_COORDINATOR = "risco"
|
||||||
DATA_ZONES = "zones"
|
|
||||||
EVENTS_COORDINATOR = "risco_events"
|
EVENTS_COORDINATOR = "risco_events"
|
||||||
|
|
||||||
DEFAULT_SCAN_INTERVAL = 30
|
DEFAULT_SCAN_INTERVAL = 30
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
|
||||||
|
def binary_sensor_unique_id(risco, zone_id):
|
||||||
|
"""Return unique id for the binary sensor."""
|
||||||
|
return f"{risco.site_uuid}_zone_{zone_id}"
|
||||||
|
|
||||||
|
|
||||||
class RiscoEntity(CoordinatorEntity):
|
class RiscoEntity(CoordinatorEntity):
|
||||||
"""Risco entity base class."""
|
"""Risco entity base class."""
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
"""Sensor for Risco Events."""
|
"""Sensor for Risco Events."""
|
||||||
|
from homeassistant.components.binary_sensor import DOMAIN as BS_DOMAIN
|
||||||
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
|
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DATA_ZONES, DOMAIN, EVENTS_COORDINATOR
|
from .const import DOMAIN, EVENTS_COORDINATOR
|
||||||
|
from .entity import binary_sensor_unique_id
|
||||||
|
|
||||||
CATEGORIES = {
|
CATEGORIES = {
|
||||||
2: "Alarm",
|
2: "Alarm",
|
||||||
|
@ -51,6 +53,7 @@ class RiscoSensor(CoordinatorEntity):
|
||||||
self._excludes = excludes
|
self._excludes = excludes
|
||||||
self._name = name
|
self._name = name
|
||||||
self._entry_id = entry_id
|
self._entry_id = entry_id
|
||||||
|
self._entity_registry = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -64,6 +67,9 @@ class RiscoSensor(CoordinatorEntity):
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""When entity is added to hass."""
|
"""When entity is added to hass."""
|
||||||
|
self._entity_registry = (
|
||||||
|
await self.hass.helpers.entity_registry.async_get_registry()
|
||||||
|
)
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
self.coordinator.async_add_listener(self._refresh_from_coordinator)
|
self.coordinator.async_add_listener(self._refresh_from_coordinator)
|
||||||
)
|
)
|
||||||
|
@ -96,10 +102,14 @@ class RiscoSensor(CoordinatorEntity):
|
||||||
|
|
||||||
attrs = {atr: getattr(self._event, atr, None) for atr in EVENT_ATTRIBUTES}
|
attrs = {atr: getattr(self._event, atr, None) for atr in EVENT_ATTRIBUTES}
|
||||||
if self._event.zone_id is not None:
|
if self._event.zone_id is not None:
|
||||||
zones = self.hass.data[DOMAIN][self._entry_id][DATA_ZONES]
|
zone_unique_id = binary_sensor_unique_id(
|
||||||
zone = zones.get(self._event.zone_id)
|
self.coordinator.risco, self._event.zone_id
|
||||||
if zone is not None:
|
)
|
||||||
attrs["zone_entity_id"] = zone.entity_id
|
zone_entity_id = self._entity_registry.async_get_entity_id(
|
||||||
|
BS_DOMAIN, DOMAIN, zone_unique_id
|
||||||
|
)
|
||||||
|
if zone_entity_id is not None:
|
||||||
|
attrs["zone_entity_id"] = zone_entity_id
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue