diff --git a/homeassistant/components/risco/binary_sensor.py b/homeassistant/components/risco/binary_sensor.py index 99995ee5b64..ba01b70686b 100644 --- a/homeassistant/components/risco/binary_sensor.py +++ b/homeassistant/components/risco/binary_sensor.py @@ -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): diff --git a/homeassistant/components/risco/const.py b/homeassistant/components/risco/const.py index 80153153530..46eb011ba5b 100644 --- a/homeassistant/components/risco/const.py +++ b/homeassistant/components/risco/const.py @@ -11,7 +11,6 @@ DOMAIN = "risco" RISCO_EVENT = "risco_event" DATA_COORDINATOR = "risco" -DATA_ZONES = "zones" EVENTS_COORDINATOR = "risco_events" DEFAULT_SCAN_INTERVAL = 30 diff --git a/homeassistant/components/risco/entity.py b/homeassistant/components/risco/entity.py index 17e27caf18b..04b521156b1 100644 --- a/homeassistant/components/risco/entity.py +++ b/homeassistant/components/risco/entity.py @@ -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.""" diff --git a/homeassistant/components/risco/sensor.py b/homeassistant/components/risco/sensor.py index 4694fc86238..62ef6643551 100644 --- a/homeassistant/components/risco/sensor.py +++ b/homeassistant/components/risco/sensor.py @@ -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