Streamline setup of deCONZ cover platform (#71656)

This commit is contained in:
Robert Svensson 2022-05-11 13:19:28 +02:00 committed by GitHub
parent b9f3f1af6c
commit 920c0d1667
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, cast from typing import Any, cast
from pydeconz.models.event import EventType
from pydeconz.models.light.cover import Cover from pydeconz.models.light.cover import Cover
from homeassistant.components.cover import ( from homeassistant.components.cover import (
@ -15,7 +16,6 @@ from homeassistant.components.cover import (
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .deconz_device import DeconzDevice from .deconz_device import DeconzDevice
@ -38,32 +38,19 @@ async def async_setup_entry(
gateway.entities[DOMAIN] = set() gateway.entities[DOMAIN] = set()
@callback @callback
def async_add_cover(lights: list[Cover] | None = None) -> None: def async_add_cover(_: EventType, cover_id: str) -> None:
"""Add cover from deCONZ.""" """Add cover from deCONZ."""
entities = [] cover = gateway.api.lights.covers[cover_id]
async_add_entities([DeconzCover(cover, gateway)])
if lights is None:
lights = list(gateway.api.lights.covers.values())
for light in lights:
if (
isinstance(light, Cover)
and light.unique_id not in gateway.entities[DOMAIN]
):
entities.append(DeconzCover(light, gateway))
if entities:
async_add_entities(entities)
config_entry.async_on_unload( config_entry.async_on_unload(
async_dispatcher_connect( gateway.api.lights.covers.subscribe(
hass,
gateway.signal_new_light,
async_add_cover, async_add_cover,
EventType.ADDED,
) )
) )
for cover_id in gateway.api.lights.covers:
async_add_cover() async_add_cover(EventType.ADDED, cover_id)
class DeconzCover(DeconzDevice, CoverEntity): class DeconzCover(DeconzDevice, CoverEntity):