Streamline setup of deCONZ climate platform (#71708)

This commit is contained in:
Robert Svensson 2022-05-13 09:51:19 +02:00 committed by GitHub
parent 75058e63a4
commit bed2d1e37b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 26 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from typing import Any from typing import Any
from pydeconz.models.event import EventType
from pydeconz.models.sensor.thermostat import ( from pydeconz.models.sensor.thermostat import (
THERMOSTAT_FAN_MODE_AUTO, THERMOSTAT_FAN_MODE_AUTO,
THERMOSTAT_FAN_MODE_HIGH, THERMOSTAT_FAN_MODE_HIGH,
@ -91,45 +92,42 @@ async def async_setup_entry(
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the deCONZ climate devices. """Set up the deCONZ climate devices."""
Thermostats are based on the same device class as sensors in deCONZ.
"""
gateway = get_gateway_from_config_entry(hass, config_entry) gateway = get_gateway_from_config_entry(hass, config_entry)
gateway.entities[DOMAIN] = set() gateway.entities[DOMAIN] = set()
@callback @callback
def async_add_climate(sensors: list[Thermostat] | None = None) -> None: def async_add_climate(_: EventType, climate_id: str) -> None:
"""Add climate devices from deCONZ.""" """Add climate from deCONZ."""
entities: list[DeconzThermostat] = [] climate = gateway.api.sensors.thermostat[climate_id]
if not gateway.option_allow_clip_sensor and climate.type.startswith("CLIP"):
return
async_add_entities([DeconzThermostat(climate, gateway)])
if sensors is None: config_entry.async_on_unload(
sensors = list(gateway.api.sensors.thermostat.values()) gateway.api.sensors.thermostat.subscribe(
async_add_climate,
EventType.ADDED,
)
)
for climate_id in gateway.api.sensors.thermostat:
async_add_climate(EventType.ADDED, climate_id)
for sensor in sensors: @callback
def async_reload_clip_sensors() -> None:
if not gateway.option_allow_clip_sensor and sensor.type.startswith("CLIP"): """Load clip climate sensors from deCONZ."""
continue for climate_id, climate in gateway.api.sensors.thermostat.items():
if climate.type.startswith("CLIP"):
if ( async_add_climate(EventType.ADDED, climate_id)
isinstance(sensor, Thermostat)
and sensor.unique_id not in gateway.entities[DOMAIN]
):
entities.append(DeconzThermostat(sensor, gateway))
if entities:
async_add_entities(entities)
config_entry.async_on_unload( config_entry.async_on_unload(
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
gateway.signal_new_sensor, gateway.signal_reload_clip_sensors,
async_add_climate, async_reload_clip_sensors,
) )
) )
async_add_climate()
class DeconzThermostat(DeconzDevice, ClimateEntity): class DeconzThermostat(DeconzDevice, ClimateEntity):
"""Representation of a deCONZ thermostat.""" """Representation of a deCONZ thermostat."""

View File

@ -63,6 +63,7 @@ class DeconzGateway:
self.signal_reachable = f"deconz-reachable-{config_entry.entry_id}" self.signal_reachable = f"deconz-reachable-{config_entry.entry_id}"
self.signal_reload_groups = f"deconz_reload_group_{config_entry.entry_id}" self.signal_reload_groups = f"deconz_reload_group_{config_entry.entry_id}"
self.signal_reload_clip_sensors = f"deconz_reload_clip_{config_entry.entry_id}"
self.signal_new_light = f"deconz_new_light_{config_entry.entry_id}" self.signal_new_light = f"deconz_new_light_{config_entry.entry_id}"
self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}" self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}"
@ -212,6 +213,7 @@ class DeconzGateway:
if self.option_allow_clip_sensor: if self.option_allow_clip_sensor:
self.async_add_device_callback(ResourceGroup.SENSOR.value) self.async_add_device_callback(ResourceGroup.SENSOR.value)
async_dispatcher_send(self.hass, self.signal_reload_clip_sensors)
else: else:
deconz_ids += [ deconz_ids += [