Streamline setup of deCONZ siren platform (#71660)

This commit is contained in:
Robert Svensson 2022-05-11 13:30:16 +02:00 committed by GitHub
parent db17d7aecf
commit 44f8c555a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from typing import Any
from pydeconz.models.event import EventType
from pydeconz.models.light.siren import Siren
from homeassistant.components.siren import (
@ -13,7 +14,6 @@ from homeassistant.components.siren import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .deconz_device import DeconzDevice
@ -30,33 +30,19 @@ async def async_setup_entry(
gateway.entities[DOMAIN] = set()
@callback
def async_add_siren(lights: list[Siren] | None = None) -> None:
def async_add_siren(_: EventType, siren_id: str) -> None:
"""Add siren from deCONZ."""
entities = []
if lights is None:
lights = list(gateway.api.lights.sirens.values())
for light in lights:
if (
isinstance(light, Siren)
and light.unique_id not in gateway.entities[DOMAIN]
):
entities.append(DeconzSiren(light, gateway))
if entities:
async_add_entities(entities)
siren = gateway.api.lights.sirens[siren_id]
async_add_entities([DeconzSiren(siren, gateway)])
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
gateway.signal_new_light,
gateway.api.lights.sirens.subscribe(
async_add_siren,
EventType.ADDED,
)
)
async_add_siren()
for siren_id in gateway.api.lights.sirens:
async_add_siren(EventType.ADDED, siren_id)
class DeconzSiren(DeconzDevice, SirenEntity):