-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclimate.py
271 lines (221 loc) · 10.2 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
264
265
266
267
268
269
270
271
"""Support for Thermotec Aeroflow Heater."""
from __future__ import annotations
import logging
from abc import ABC
from datetime import datetime, timedelta
import voluptuous as vol
from thermotecaeroflowflexismart.client import Client
from thermotecaeroflowflexismart.data_object import HomeAssistantModuleData
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
HVACAction,
CURRENT_HVAC_ACTIONS,
HVACMode,
HVAC_MODES,
PRESET_HOME,
PRESET_AWAY,
PRESET_BOOST,
ClimateEntityFeature
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import (
entity_platform,
config_validation as cv
)
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from . import ThermotecAeroflowEntity
from .const import DOMAIN, MANUFACTURER
_LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0
SET_WINDOW_OPEN_DETECTION_SCHEMA = {
vol.Required("window_open_detection"): cv.boolean,
}
SET_ANTI_FREEZE_TEMPERATURE_SCHEMA = {
vol.Required("anti_freeze_temperature"): vol.All(vol.Coerce(int), vol.Range(min=0, max=17))
}
SET_TEMPERATURE_SCHEMA = {
vol.Required(ATTR_TEMPERATURE): vol.All(vol.Coerce(float), vol.Range(min=1, max=35))
}
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Thermotec Heater based on config_entry."""
entry = hass.data[DOMAIN][entry.entry_id]
client = entry["client"] # type: Client
coordinator = entry["coordinator"] # type: DataUpdateCoordinator
async_add_entities(
(ThermotecAeroflowClimateEntity(coordinator, client, identifier, entity)
for identifier, entity in coordinator.data.items())
)
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
"set_window_open_detection", SET_WINDOW_OPEN_DETECTION_SCHEMA, "set_window_open_detection"
)
platform.async_register_entity_service(
"set_anti_freeze_temperature", SET_ANTI_FREEZE_TEMPERATURE_SCHEMA, "set_anti_freeze_temperature"
)
platform.async_register_entity_service(
"set_temperature", SET_TEMPERATURE_SCHEMA, "async_set_temperature"
)
class ThermotecAeroflowClimateEntity(ThermotecAeroflowEntity, ClimateEntity, ABC):
"""Representation of a Thermotec Aeroflow heater."""
_boost_active: bool = False
_boost_time_left: str = "0 min"
_holiday_mode_active: bool = False
_sw_version: str = "Unknown"
_window_open_detection: bool = False
_anti_freeze_temperature: float = 0.0
_temperature_offset: float = 0.0
_attr_min_temp = 1
_attr_max_temp = 35
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_preset_modes = [PRESET_HOME, PRESET_AWAY, PRESET_BOOST]
_attr_hvac_mode = HVACMode.HEAT
_attr_hvac_modes = [HVAC_MODES]
def __init__(self, coordinator: DataUpdateCoordinator, client: Client, identifier: str,
entity: HomeAssistantModuleData):
"""Initialize the climate device."""
entity_type = "Heater"
super().__init__(coordinator=coordinator, client=client, entity_type=entity_type, zone=entity.get_zone_id(),
module=entity.get_module_id(), identifier=identifier)
self._update_attributes()
def _update_attributes(self) -> None:
_LOGGER.debug("Updating %s", self._identifier)
current_data = self.coordinator.data.get(self._identifier) # type: HomeAssistantModuleData
if current_data is None:
_LOGGER.debug("Could not find data for heater with identifier %s. Disabling", self._identifier)
self._attr_available = False
return
self._attr_available = True
module_data = current_data.get_module_data()
self._attr_target_temperature = module_data.get_target_temperature()
self._attr_current_temperature = module_data.get_current_temperature()
boost_time_left = module_data.get_boost_time_left_string()
if not module_data.is_boost_active():
boost_time_left = "0 min"
self._boost_time_left = boost_time_left
self._boost_active = module_data.is_boost_active()
self._temperature_offset = module_data.get_temperature_offset()
self._window_open_detection = module_data.is_window_open_detection_enabled()
self._sw_version = module_data.get_firmware_version().replace("v", "")
self._anti_freeze_temperature = current_data.get_anti_freeze_temperature()
self._holiday_mode_active = current_data.get_holiday_data().is_holiday_mode_active()
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._update_attributes()
super()._handle_coordinator_update()
@property
def available(self) -> bool:
"""Return if entity is available."""
return self.coordinator.last_update_success and self._attr_available
@property
def preset_mode(self):
"""Return the preset_mode."""
if self._boost_active:
return PRESET_BOOST
if self._holiday_mode_active:
return PRESET_AWAY
return PRESET_HOME
async def async_set_temperature(self, **kwargs):
"""Set new target temperature."""
target_temp = kwargs.get(ATTR_TEMPERATURE)
if target_temp is None:
_LOGGER.error("Missing target temperature %s", kwargs)
return
target_temp = float(target_temp)
_LOGGER.debug("Set %s temperature %s", self.entity_id, target_temp)
# Limit the target temperature into acceptable range.
target_temp = min(self.max_temp, target_temp)
target_temp = max(self.min_temp, target_temp)
await self._client.set_module_temperature(
zone=self._zone, module=self._module, temperature=target_temp
)
self._attr_target_temperature = target_temp
await self.async_update_ha_state()
async def async_set_preset_mode(self, preset_mode):
"""Set preset mode."""
if preset_mode == PRESET_AWAY:
_LOGGER.debug("Activate Holiday mode")
holiday_date = datetime.now() + timedelta(
days=240 # max holiday length is 240 days
)
if self.preset_mode == PRESET_BOOST:
await self._client.set_module_boost(self._zone, self._module, 0)
# target_temperature is the new temperature when holiday mode ends (manually or automatically)
await self._client.set_module_holiday_mode(
self._zone, self._module, holiday_date, self.target_temperature
)
elif preset_mode == PRESET_BOOST:
_LOGGER.debug("Activate Boost")
if self.preset_mode == PRESET_AWAY:
await self._client.disable_module_holiday_mode(self._zone, self._module)
boost_timer = 95 # 95 minutes is the max boost value
await self._client.set_module_boost(self._zone, self._module, boost_timer)
elif preset_mode == PRESET_HOME:
_LOGGER.debug("Go back to normal Mode")
if self.preset_mode == PRESET_BOOST:
_LOGGER.debug("Disable Boost mode")
await self._client.set_module_boost(self._zone, self._module, 0)
elif self.preset_mode == PRESET_AWAY:
_LOGGER.debug("Disable Holiday mode")
await self._client.disable_module_holiday_mode(self._zone, self._module)
def set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode."""
_LOGGER.debug("Set %s heat mode %s", self.entity_id, hvac_mode)
@property
def hvac_action(self):
"""Return the current running hvac operation if supported.
Need to be one of CURRENT_HVAC_*.
"""
if self.current_temperature is None or self.target_temperature is None:
return None
if self.current_temperature < self.target_temperature:
return HVACAction.HEATING
return HVACAction.IDLE
async def set_window_open_detection(self, window_open_detection: bool) -> None:
"""Enable or Disable Window Open Detection."""
_LOGGER.debug("Set %s Window Open Detection %s", self.name, window_open_detection)
if window_open_detection:
await self._client.enable_module_window_open_detection(zone=self._zone, module=self._module)
else:
await self._client.disable_module_window_open_detection(zone=self._zone, module=self._module)
self._window_open_detection = window_open_detection
await self.async_update_ha_state()
async def set_anti_freeze_temperature(self, anti_freeze_temperature: int) -> None:
"""Update Anti Freeze Temperature."""
_LOGGER.debug("Set %s Anti Freeze Temperature %s", self.name, anti_freeze_temperature)
float_anti_freeze_temperature = float(anti_freeze_temperature)
await self._client.set_module_anti_freeze_temperature(
zone=self._zone,
module=self._module,
temperature=float_anti_freeze_temperature
)
self._anti_freeze_temperature = float_anti_freeze_temperature
await self.async_update_ha_state()
@property
def extra_state_attributes(self):
"""Return the state attributes of the sun."""
return {
"window_open_detection": self._window_open_detection,
"anti_freeze_temperature": self._anti_freeze_temperature,
"boost_time_left": self._boost_time_left,
"temperature_offset": self._temperature_offset,
"zone": self._zone,
"module": self._module
}
@property
def device_info(self):
"""Return device specific attributes."""
return {
"identifiers": {(DOMAIN, f"thermotec-aeroflow_{self._identifier}")},
"name": self.name,
"manufacturer": MANUFACTURER,
"sw_version": self._sw_version,
}