forked from konradb3/homeassistant_salus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimate.py
225 lines (187 loc) · 7.23 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
"""Support for thermostats."""
from datetime import timedelta
import logging
import async_timeout
import voluptuous as vol
from homeassistant.components.climate import PLATFORM_SCHEMA, ClimateEntity
from homeassistant.components.climate.const import (
CURRENT_HVAC_COOL,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_IDLE,
FAN_OFF,
FAN_ON,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
PRESET_AWAY,
PRESET_HOME,
SUPPORT_FAN_MODE,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_HOST,
CONF_TOKEN,
PRECISION_HALVES,
STATE_ON,
TEMP_CELSIUS,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from pyit600.exceptions import IT600AuthenticationError, IT600ConnectionError
from pyit600.gateway import IT600Gateway
OPERATION_LIST = [HVAC_MODE_HEAT, HVAC_MODE_OFF, HVAC_MODE_AUTO]
SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_TOKEN): cv.string,
}
)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Salus thermostats from a config entry."""
await async_setup_platform(hass, config_entry.data, async_add_entities)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the sensor platform."""
gateway = IT600Gateway(host=config[CONF_HOST], euid=config[CONF_TOKEN])
try:
await gateway.connect()
except IT600ConnectionError as ce:
_LOGGER.error("Connection error: check if you have specified gateway's HOST correctly.")
return False
except IT600AuthenticationError as ae:
_LOGGER.error("Authentication error: check if you have specified gateway's TOKEN correctly.")
return False
async def async_update_data():
"""Fetch data from API endpoint.
This is the place to pre-process the data to lookup tables
so entities can quickly look up their data.
"""
async with async_timeout.timeout(10):
await gateway.poll_status()
return gateway.get_climate_devices()
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
# Name of the data. For logging purposes.
name="sensor",
update_method=async_update_data,
# Polling interval. Will only be polled if there are subscribers.
update_interval=timedelta(seconds=30),
)
# Fetch initial data so we have data when entities subscribe
await coordinator.async_refresh()
async_add_entities(SalusThermostat(coordinator, idx, gateway) for idx
in coordinator.data)
class SalusThermostat(ClimateEntity):
"""Representation of a Sensor."""
def __init__(self, coordinator, idx, gateway):
"""Initialize the thermostat."""
self._coordinator = coordinator
self._idx = idx
self._gateway = gateway
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._coordinator.async_request_refresh()
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
@property
def supported_features(self):
"""Return the list of supported features."""
return SUPPORT_FLAGS
@property
def available(self):
"""Return if entity is available."""
return self._coordinator.data.get(self._idx).available
@property
def device_info(self):
"""Return the device info."""
return {
"name": self._coordinator.data.get(self._idx).name,
"identifiers": {("salus", self._coordinator.data.get(self._idx).unique_id)},
"manufacturer": self._coordinator.data.get(self._idx).manufacturer,
"model": self._coordinator.data.get(self._idx).model,
"sw_version": self._coordinator.data.get(self._idx).sw_version
}
@property
def unique_id(self):
"""Return the unique id."""
return self._coordinator.data.get(self._idx).unique_id
@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
return False
@property
def name(self):
"""Return the name of the Radio Thermostat."""
return self._coordinator.data.get(self._idx).name
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def precision(self):
"""Return the precision of the system."""
return PRECISION_HALVES
@property
def current_temperature(self):
"""Return the current temperature."""
return self._coordinator.data.get(self._idx).current_temperature
@property
def current_humidity(self):
"""Return the current temperature."""
return None
@property
def hvac_mode(self):
"""Return the current operation. head, cool idle."""
if self._coordinator.data.get(self._idx).preset_mode == "Off":
return HVAC_MODE_OFF
elif self._coordinator.data.get(self._idx).preset_mode == "Permanent Hold":
return HVAC_MODE_HEAT
elif self._coordinator.data.get(self._idx).preset_mode == "Follow Schedule":
return HVAC_MODE_AUTO
else:
return None
@property
def hvac_modes(self):
"""Return the operation modes list."""
return OPERATION_LIST
@property
def hvac_action(self):
"""Return the current running hvac operation if supported."""
return self._coordinator.data.get(self._idx).hvac_action
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._coordinator.data.get(self._idx).target_temperature
@property
def max_temp(self):
return self._coordinator.data.get(self._idx).max_temp
@property
def min_temp(self):
return self._coordinator.data.get(self._idx).min_temp
async def async_set_temperature(self, **kwargs):
"""Set new target temperature."""
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is None:
return
await self._gateway.set_climate_device_temperature(self._idx, temperature)
await self._coordinator.async_request_refresh()
async def async_set_hvac_mode(self, hvac_mode):
"""Set operation mode (auto, heat, off)."""
if hvac_mode == HVAC_MODE_OFF:
preset = "Off"
elif hvac_mode == HVAC_MODE_HEAT:
preset = "Permanent Hold"
else:
preset = "Follow Schedule"
await self._gateway.set_climate_device_preset(self._idx, preset)
await self._coordinator.async_request_refresh()