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 pydeconz.models.event import EventType
from pydeconz.models.sensor.thermostat import (
THERMOSTAT_FAN_MODE_AUTO,
THERMOSTAT_FAN_MODE_HIGH,
@ -91,45 +92,42 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the deCONZ climate devices.
Thermostats are based on the same device class as sensors in deCONZ.
"""
"""Set up the deCONZ climate devices."""
gateway = get_gateway_from_config_entry(hass, config_entry)
gateway.entities[DOMAIN] = set()
@callback
def async_add_climate(sensors: list[Thermostat] | None = None) -> None:
"""Add climate devices from deCONZ."""
entities: list[DeconzThermostat] = []
def async_add_climate(_: EventType, climate_id: str) -> None:
"""Add climate from deCONZ."""
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:
sensors = list(gateway.api.sensors.thermostat.values())
config_entry.async_on_unload(
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:
if not gateway.option_allow_clip_sensor and sensor.type.startswith("CLIP"):
continue
if (
isinstance(sensor, Thermostat)
and sensor.unique_id not in gateway.entities[DOMAIN]
):
entities.append(DeconzThermostat(sensor, gateway))
if entities:
async_add_entities(entities)
@callback
def async_reload_clip_sensors() -> None:
"""Load clip climate sensors from deCONZ."""
for climate_id, climate in gateway.api.sensors.thermostat.items():
if climate.type.startswith("CLIP"):
async_add_climate(EventType.ADDED, climate_id)
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
gateway.signal_new_sensor,
async_add_climate,
gateway.signal_reload_clip_sensors,
async_reload_clip_sensors,
)
)
async_add_climate()
class DeconzThermostat(DeconzDevice, ClimateEntity):
"""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_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_sensor = f"deconz_new_sensor_{config_entry.entry_id}"
@ -212,6 +213,7 @@ class DeconzGateway:
if self.option_allow_clip_sensor:
self.async_add_device_callback(ResourceGroup.SENSOR.value)
async_dispatcher_send(self.hass, self.signal_reload_clip_sensors)
else:
deconz_ids += [