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