Streamline setup of deCONZ switch platform (#71661)

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

View File

@ -4,12 +4,12 @@ from __future__ import annotations
from typing import Any from typing import Any
from pydeconz.models.event import EventType
from pydeconz.models.light.light import Light from pydeconz.models.light.light import Light
from homeassistant.components.switch import DOMAIN, SwitchEntity from homeassistant.components.switch import DOMAIN, SwitchEntity
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 .const import POWER_PLUGS from .const import POWER_PLUGS
@ -30,33 +30,21 @@ async def async_setup_entry(
gateway.entities[DOMAIN] = set() gateway.entities[DOMAIN] = set()
@callback @callback
def async_add_switch(lights: list[Light] | None = None) -> None: def async_add_switch(_: EventType, light_id: str) -> None:
"""Add switch from deCONZ.""" """Add switch from deCONZ."""
entities = [] light = gateway.api.lights.lights[light_id]
if light.type not in POWER_PLUGS:
if lights is None: return
lights = list(gateway.api.lights.lights.values()) async_add_entities([DeconzPowerPlug(light, gateway)])
for light in lights:
if (
light.type in POWER_PLUGS
and light.unique_id not in gateway.entities[DOMAIN]
):
entities.append(DeconzPowerPlug(light, gateway))
if entities:
async_add_entities(entities)
config_entry.async_on_unload( config_entry.async_on_unload(
async_dispatcher_connect( gateway.api.lights.lights.subscribe(
hass,
gateway.signal_new_light,
async_add_switch, async_add_switch,
EventType.ADDED,
) )
) )
for light_id in gateway.api.lights.lights:
async_add_switch() async_add_switch(EventType.ADDED, light_id)
class DeconzPowerPlug(DeconzDevice, SwitchEntity): class DeconzPowerPlug(DeconzDevice, SwitchEntity):