-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathclimate.py
263 lines (211 loc) · 8.31 KB
/
climate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""Support for deCONZ climate devices."""
from __future__ import annotations
from typing import Any
from pydeconz.models.event import EventType
from pydeconz.models.sensor.thermostat import (
Thermostat,
ThermostatFanMode,
ThermostatMode,
ThermostatPreset,
)
from homeassistant.components.climate import (
DOMAIN as CLIMATE_DOMAIN,
FAN_AUTO,
FAN_HIGH,
FAN_LOW,
FAN_MEDIUM,
FAN_OFF,
FAN_ON,
PRESET_BOOST,
PRESET_COMFORT,
PRESET_ECO,
ClimateEntity,
ClimateEntityFeature,
HVACAction,
HVACMode,
)
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import DeconzConfigEntry
from .const import ATTR_LOCKED, ATTR_OFFSET, ATTR_VALVE
from .entity import DeconzDevice
from .hub import DeconzHub
DECONZ_FAN_SMART = "smart"
FAN_MODE_TO_DECONZ = {
DECONZ_FAN_SMART: ThermostatFanMode.SMART,
FAN_AUTO: ThermostatFanMode.AUTO,
FAN_HIGH: ThermostatFanMode.HIGH,
FAN_MEDIUM: ThermostatFanMode.MEDIUM,
FAN_LOW: ThermostatFanMode.LOW,
FAN_ON: ThermostatFanMode.ON,
FAN_OFF: ThermostatFanMode.OFF,
}
DECONZ_TO_FAN_MODE = {value: key for key, value in FAN_MODE_TO_DECONZ.items()}
HVAC_MODE_TO_DECONZ = {
HVACMode.AUTO: ThermostatMode.AUTO,
HVACMode.COOL: ThermostatMode.COOL,
HVACMode.HEAT: ThermostatMode.HEAT,
HVACMode.OFF: ThermostatMode.OFF,
}
DECONZ_PRESET_AUTO = "auto"
DECONZ_PRESET_COMPLEX = "complex"
DECONZ_PRESET_HOLIDAY = "holiday"
DECONZ_PRESET_MANUAL = "manual"
PRESET_MODE_TO_DECONZ = {
DECONZ_PRESET_AUTO: ThermostatPreset.AUTO,
PRESET_BOOST: ThermostatPreset.BOOST,
PRESET_COMFORT: ThermostatPreset.COMFORT,
DECONZ_PRESET_COMPLEX: ThermostatPreset.COMPLEX,
PRESET_ECO: ThermostatPreset.ECO,
DECONZ_PRESET_HOLIDAY: ThermostatPreset.HOLIDAY,
DECONZ_PRESET_MANUAL: ThermostatPreset.MANUAL,
}
DECONZ_TO_PRESET_MODE = {value: key for key, value in PRESET_MODE_TO_DECONZ.items()}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: DeconzConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the deCONZ climate devices."""
hub = config_entry.runtime_data
hub.entities[CLIMATE_DOMAIN] = set()
@callback
def async_add_climate(_: EventType, climate_id: str) -> None:
"""Add climate from deCONZ."""
climate = hub.api.sensors.thermostat[climate_id]
async_add_entities([DeconzThermostat(climate, hub)])
hub.register_platform_add_device_callback(
async_add_climate,
hub.api.sensors.thermostat,
)
class DeconzThermostat(DeconzDevice[Thermostat], ClimateEntity):
"""Representation of a deCONZ thermostat."""
TYPE = CLIMATE_DOMAIN
_attr_temperature_unit = UnitOfTemperature.CELSIUS
def __init__(self, device: Thermostat, hub: DeconzHub) -> None:
"""Set up thermostat device."""
super().__init__(device, hub)
self._attr_hvac_modes = [
HVACMode.HEAT,
HVACMode.OFF,
]
if device.mode:
self._attr_hvac_modes.append(HVACMode.AUTO)
if "coolsetpoint" in device.raw["config"]:
self._attr_hvac_modes.append(HVACMode.COOL)
self._deconz_to_hvac_mode = {
HVAC_MODE_TO_DECONZ[item]: item for item in self._attr_hvac_modes
}
self._attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON
)
if device.fan_mode:
self._attr_supported_features |= ClimateEntityFeature.FAN_MODE
self._attr_fan_modes = list(FAN_MODE_TO_DECONZ)
if device.preset:
self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
self._attr_preset_modes = list(PRESET_MODE_TO_DECONZ)
# Fan control
@property
def fan_mode(self) -> str:
"""Return fan operation."""
if self._device.fan_mode in DECONZ_TO_FAN_MODE:
return DECONZ_TO_FAN_MODE[self._device.fan_mode]
return FAN_ON if self._device.state_on else FAN_OFF
async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set new target fan mode."""
if fan_mode not in FAN_MODE_TO_DECONZ:
raise ValueError(f"Unsupported fan mode {fan_mode}")
await self.hub.api.sensors.thermostat.set_config(
id=self._device.resource_id,
fan_mode=FAN_MODE_TO_DECONZ[fan_mode],
)
# HVAC control
@property
def hvac_mode(self) -> HVACMode:
"""Return hvac operation ie. heat, cool mode."""
if self._device.mode in self._deconz_to_hvac_mode:
return self._deconz_to_hvac_mode[self._device.mode]
return HVACMode.HEAT if self._device.state_on else HVACMode.OFF
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode."""
if hvac_mode not in self._attr_hvac_modes:
raise ValueError(f"Unsupported HVAC mode {hvac_mode}")
if len(self._attr_hvac_modes) == 2: # Only allow turn on and off thermostat
await self.hub.api.sensors.thermostat.set_config(
id=self._device.resource_id,
on=hvac_mode != HVACMode.OFF,
)
else:
await self.hub.api.sensors.thermostat.set_config(
id=self._device.resource_id,
mode=HVAC_MODE_TO_DECONZ[hvac_mode],
)
@property
def hvac_action(self) -> HVACAction:
"""Return current hvac operation ie. heat, cool.
Preset 'BOOST' is interpreted as 'state_on'.
"""
if self._device.mode == ThermostatMode.OFF:
return HVACAction.OFF
if self._device.state_on or self._device.preset == ThermostatPreset.BOOST:
if self._device.mode == ThermostatMode.COOL:
return HVACAction.COOLING
return HVACAction.HEATING
return HVACAction.IDLE
# Preset control
@property
def preset_mode(self) -> str | None:
"""Return preset mode."""
if self._device.preset in DECONZ_TO_PRESET_MODE:
return DECONZ_TO_PRESET_MODE[self._device.preset]
return None
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
if preset_mode not in PRESET_MODE_TO_DECONZ:
raise ValueError(f"Unsupported preset mode {preset_mode}")
await self.hub.api.sensors.thermostat.set_config(
id=self._device.resource_id,
preset=PRESET_MODE_TO_DECONZ[preset_mode],
)
# Temperature control
@property
def current_temperature(self) -> float:
"""Return the current temperature."""
return self._device.scaled_temperature
@property
def target_temperature(self) -> float | None:
"""Return the target temperature."""
if self._device.mode == ThermostatMode.COOL and self._device.cooling_setpoint:
return self._device.scaled_cooling_setpoint
if self._device.heating_setpoint:
return self._device.scaled_heating_setpoint
return None
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
if ATTR_TEMPERATURE not in kwargs:
raise ValueError(f"Expected attribute {ATTR_TEMPERATURE}")
if self._device.mode == ThermostatMode.COOL:
await self.hub.api.sensors.thermostat.set_config(
id=self._device.resource_id,
cooling_setpoint=kwargs[ATTR_TEMPERATURE] * 100,
)
else:
await self.hub.api.sensors.thermostat.set_config(
id=self._device.resource_id,
heating_setpoint=kwargs[ATTR_TEMPERATURE] * 100,
)
@property
def extra_state_attributes(self) -> dict[str, bool | int]:
"""Return the state attributes of the thermostat."""
attr = {}
if self._device.offset is not None:
attr[ATTR_OFFSET] = self._device.offset
if self._device.valve is not None:
attr[ATTR_VALVE] = self._device.valve
if self._device.locked is not None:
attr[ATTR_LOCKED] = self._device.locked
return attr