Obtain zone entity id in Risco sensors through entity registry (#43007)

pull/43017/head
On Freund 2020-11-09 19:44:30 +02:00 committed by GitHub
parent 1338c4a425
commit 4ed8e209f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 14 deletions

View File

@ -5,8 +5,8 @@ from homeassistant.components.binary_sensor import (
)
from homeassistant.helpers import entity_platform
from .const import DATA_COORDINATOR, DATA_ZONES, DOMAIN
from .entity import RiscoEntity
from .const import DATA_COORDINATOR, DOMAIN
from .entity import RiscoEntity, binary_sensor_unique_id
SERVICE_BYPASS_ZONE = "bypass_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]
entities = {
zone_id: RiscoBinarySensor(coordinator, zone_id, zone)
entities = [
RiscoBinarySensor(coordinator, zone_id, zone)
for zone_id, zone in coordinator.data.zones.items()
}
hass.data[DOMAIN][config_entry.entry_id][DATA_ZONES] = entities
async_add_entities(entities.values(), False)
]
async_add_entities(entities, False)
class RiscoBinarySensor(BinarySensorEntity, RiscoEntity):
@ -58,7 +57,7 @@ class RiscoBinarySensor(BinarySensorEntity, RiscoEntity):
@property
def unique_id(self):
"""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
def device_state_attributes(self):

View File

@ -11,7 +11,6 @@ DOMAIN = "risco"
RISCO_EVENT = "risco_event"
DATA_COORDINATOR = "risco"
DATA_ZONES = "zones"
EVENTS_COORDINATOR = "risco_events"
DEFAULT_SCAN_INTERVAL = 30

View File

@ -2,6 +2,11 @@
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):
"""Risco entity base class."""

View File

@ -1,8 +1,10 @@
"""Sensor for Risco Events."""
from homeassistant.components.binary_sensor import DOMAIN as BS_DOMAIN
from homeassistant.const import DEVICE_CLASS_TIMESTAMP
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 = {
2: "Alarm",
@ -51,6 +53,7 @@ class RiscoSensor(CoordinatorEntity):
self._excludes = excludes
self._name = name
self._entry_id = entry_id
self._entity_registry = None
@property
def name(self):
@ -64,6 +67,9 @@ class RiscoSensor(CoordinatorEntity):
async def async_added_to_hass(self):
"""When entity is added to hass."""
self._entity_registry = (
await self.hass.helpers.entity_registry.async_get_registry()
)
self.async_on_remove(
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}
if self._event.zone_id is not None:
zones = self.hass.data[DOMAIN][self._entry_id][DATA_ZONES]
zone = zones.get(self._event.zone_id)
if zone is not None:
attrs["zone_entity_id"] = zone.entity_id
zone_unique_id = binary_sensor_unique_id(
self.coordinator.risco, self._event.zone_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