mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Streamline setup of deCONZ number platform (#71840)
This commit is contained in:
parent
2b637f71fa
commit
8ea5ec6f08
@ -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 sensor.type.startswith("CLIP"):
|
||||||
if sensors is None:
|
return
|
||||||
sensors = list(gateway.api.sensors.presence.values())
|
for description in ENTITY_DESCRIPTIONS.get(type(sensor), []):
|
||||||
|
if (
|
||||||
for sensor in sensors:
|
not hasattr(sensor, description.key)
|
||||||
|
or description.value_fn(sensor) is None
|
||||||
if sensor.type.startswith("CLIP"):
|
):
|
||||||
continue
|
continue
|
||||||
|
async_add_entities([DeconzNumber(sensor, gateway, description)])
|
||||||
known_entities = set(gateway.entities[DOMAIN])
|
|
||||||
for description in ENTITY_DESCRIPTIONS.get(type(sensor), []):
|
|
||||||
|
|
||||||
if (
|
|
||||||
not hasattr(sensor, description.key)
|
|
||||||
or description.value_fn(sensor) is None
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
|
|
||||||
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):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user