Streamline setup of deCONZ number platform (#71840)

This commit is contained in:
Robert Svensson 2022-05-15 15:56:45 +02:00 committed by GitHub
parent 2b637f71fa
commit 8ea5ec6f08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from pydeconz.models.event import EventType
from pydeconz.models.sensor.presence import PRESENCE_DELAY, Presence from pydeconz.models.sensor.presence import PRESENCE_DELAY, Presence
from homeassistant.components.number import ( from homeassistant.components.number import (
@ -14,7 +15,6 @@ from homeassistant.components.number 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 import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -62,48 +62,27 @@ async def async_setup_entry(
gateway.entities[DOMAIN] = set() gateway.entities[DOMAIN] = set()
@callback @callback
def async_add_sensor(sensors: list[Presence] | None = None) -> None: def async_add_sensor(_: EventType, sensor_id: str) -> None:
"""Add number config sensor from deCONZ.""" """Add sensor from deCONZ."""
entities = [] sensor = gateway.api.sensors.presence[sensor_id]
if sensors is None:
sensors = list(gateway.api.sensors.presence.values())
for sensor in sensors:
if sensor.type.startswith("CLIP"): if sensor.type.startswith("CLIP"):
continue return
known_entities = set(gateway.entities[DOMAIN])
for description in ENTITY_DESCRIPTIONS.get(type(sensor), []): for description in ENTITY_DESCRIPTIONS.get(type(sensor), []):
if ( if (
not hasattr(sensor, description.key) not hasattr(sensor, description.key)
or description.value_fn(sensor) is None or description.value_fn(sensor) is None
): ):
continue continue
async_add_entities([DeconzNumber(sensor, gateway, description)])
new_entity = DeconzNumber(sensor, gateway, description)
if new_entity.unique_id not in known_entities:
entities.append(new_entity)
if entities:
async_add_entities(entities)
config_entry.async_on_unload( config_entry.async_on_unload(
async_dispatcher_connect( gateway.api.sensors.presence.subscribe(
hass,
gateway.signal_new_sensor,
async_add_sensor, async_add_sensor,
EventType.ADDED,
) )
) )
for sensor_id in gateway.api.sensors.presence:
async_add_sensor( async_add_sensor(EventType.ADDED, sensor_id)
[
gateway.api.sensors.presence[key]
for key in sorted(gateway.api.sensors.presence, key=int)
]
)
class DeconzNumber(DeconzDevice, NumberEntity): class DeconzNumber(DeconzDevice, NumberEntity):