Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions homeassistant/components/deconz/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from typing import Any

from pydeconz.models.event import EventType
from pydeconz.models.sensor.thermostat import (
THERMOSTAT_FAN_MODE_AUTO,
THERMOSTAT_FAN_MODE_HIGH,
Expand Down Expand Up @@ -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] = []

if sensors is None:
sensors = list(gateway.api.sensors.thermostat.values())

for sensor in sensors:
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 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))
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)

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."""
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/deconz/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(

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}"
Expand Down Expand Up @@ -212,6 +213,7 @@ async def options_updated(self) -> None:

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 += [
Expand Down