forked from ksya/ha-nefiteasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
climate.py
199 lines (161 loc) · 6.38 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
"""
Support for Bosch home thermostats.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/xxxxxx/
"""
import asyncio
import concurrent
import logging
from homeassistant.components.climate import ClimateDevice
from homeassistant.components.climate.const import (SUPPORT_TARGET_TEMPERATURE, SUPPORT_PRESET_MODE,
CURRENT_HVAC_IDLE, CURRENT_HVAC_HEAT,
HVAC_MODE_HEAT)
from homeassistant.const import TEMP_CELSIUS, ATTR_TEMPERATURE
from homeassistant.const import STATE_UNKNOWN, EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DOMAIN, CONF_NAME, CONF_MIN_TEMP, CONF_MAX_TEMP, DISPATCHER_ON_DEVICE_UPDATE
_LOGGER = logging.getLogger(__name__)
SUPPORT_FLAGS = (SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE)
# supported operating modes (preset mode)
OPERATION_MANUAL = "Manual"
OPERATION_CLOCK = "Clock"
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
entities = []
for device in hass.data[DOMAIN]["devices"]:
entities.append(NefitThermostat(device))
async_add_entities(entities, update_before_add=True)
_LOGGER.debug("climate: async_setup_platform done")
class NefitThermostat(ClimateDevice):
"""Representation of a NefitThermostat device."""
def __init__(self, device):
"""Initialize the thermostat."""
self._client = device['client']
self._config = device['config']
self._key = 'uistatus'
self._url = '/ecus/rrc/uiStatus'
self._unique_id = "%s_%s" % (self._client.nefit.serial_number, 'climate')
self._client.events[self._key] = asyncio.Event()
self._client.keys[self._url] = self._key
self._unit_of_measurement = TEMP_CELSIUS
self._data = {}
self._hvac_modes = [HVAC_MODE_HEAT]
self._remove_callbacks: List[Callable[[], None]] = []
async def async_added_to_hass(self) -> None:
"""Register callbacks when entity is added."""
kwargs = {
"key": self._key,
}
self._remove_callbacks.append(
async_dispatcher_connect(
self.hass, DISPATCHER_ON_DEVICE_UPDATE.format(**kwargs), self.async_schedule_update_ha_state
)
)
async def async_will_remove_from_hass(self) -> None:
"""Unregister callbacks."""
for remove_callback in self._remove_callbacks:
remove_callback()
self._remove_callbacks = []
@property
def supported_features(self):
"""Return the list of supported features.
"""
return SUPPORT_FLAGS
@property
def target_temperature_step(self):
return 0.5
async def async_update(self):
"""Get latest data."""
_LOGGER.debug("async_update called for climate device")
event = self._client.events[self._key]
event.clear() #clear old event
self._client.nefit.get(self._url)
try:
await asyncio.wait_for(event.wait(), timeout=9)
except concurrent.futures._base.TimeoutError:
_LOGGER.debug("Did not get an update in time for %s %s.", self._client.serial, 'climate')
event.clear() #clear event
@property
def name(self):
"""Return the name of the ClimateDevice.
"""
return self._config.get(CONF_NAME)
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id
@property
def temperature_unit(self):
"""Return the unit of measurement.
"""
return self._unit_of_measurement
@property
def current_temperature(self):
"""Return the current temperature.
"""
return self._client.data.get('inhouse_temperature')
@property
def target_temperature(self):
return self._client.data.get('temp_setpoint')
@property
def hvac_modes (self):
"""List of available modes."""
return self._hvac_modes
@property
def hvac_mode(self):
return HVAC_MODE_HEAT
@property
def hvac_action(self):
"""Return the current running hvac operation if supported."""
if self._client.data.get('boiler_indicator') == 'CH': #HW (hot water) is not for climate
return CURRENT_HVAC_HEAT
return CURRENT_HVAC_IDLE
@property
def preset_modes(self):
"""Return available preset modes."""
return [
OPERATION_MANUAL,
OPERATION_CLOCK
]
@property
def preset_mode(self):
"""Return the current preset mode."""
if self._client.data.get('user_mode') == 'manual':
return OPERATION_MANUAL
elif self._client.data.get('user_mode') == 'clock':
return OPERATION_CLOCK
else:
return OPERATION_MANUAL
@property
def device_state_attributes(self):
"""Return the device specific state attributes."""
return {
'last_update': self._client.data.get('last_update'),
'boiler_indicator': self._client.data.get('boiler_indicator')
}
@property
def min_temp(self):
"""Return the minimum temperature."""
return self._config.get(CONF_MIN_TEMP)
@property
def max_temp(self):
"""Return the maximum temperature."""
return self._config.get(CONF_MAX_TEMP)
async def async_set_preset_mode(self, preset_mode):
"""Set new target operation mode."""
_LOGGER.debug("set_preset_mode called mode={}.".format(preset_mode))
if preset_mode == OPERATION_CLOCK:
new_mode = "clock"
else:
new_mode = "manual"
self._client.nefit.set_usermode(new_mode)
await asyncio.wait_for(self._client.nefit.xmppclient.message_event.wait(), timeout=9)
self._client.nefit.xmppclient.message_event.clear()
self._client.data['user_mode'] = new_mode
async def async_set_temperature(self, **kwargs):
"""Set new target temperature."""
temperature = kwargs.get(ATTR_TEMPERATURE)
_LOGGER.debug("set_temperature called (temperature={}).".format(temperature))
self._client.nefit.set_temperature(temperature)
await asyncio.wait_for(self._client.nefit.xmppclient.message_event.wait(), timeout=9)
self._client.nefit.xmppclient.message_event.clear()