-
Notifications
You must be signed in to change notification settings - Fork 98
/
climate.py
358 lines (290 loc) · 10.8 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import logging
import voluptuous as vol
from homeassistant.components import climate
from homeassistant.const import CONF_REGION, CONF_TOKEN
import homeassistant.helpers.config_validation as cv
from homeassistant import const
import time
from homeassistant.components.climate import const as c_const
from custom_components.smartthinq import (
CONF_LANGUAGE, KEY_DEPRECATED_COUNTRY,
KEY_DEPRECATED_LANGUAGE, KEY_DEPRECATED_REFRESH_TOKEN)
try:
from homeassistant.components.climate import ClimateEntity
except ImportError:
from homeassistant.components.climate import ClimateDevice \
as ClimateEntity
REQUIREMENTS = ['wideq']
LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = climate.PLATFORM_SCHEMA.extend({
vol.Required(KEY_DEPRECATED_REFRESH_TOKEN): cv.string,
KEY_DEPRECATED_COUNTRY: cv.string,
KEY_DEPRECATED_LANGUAGE: cv.string,
})
MODES = {
'AI': c_const.HVAC_MODE_AUTO,
'ENERGY_SAVER': c_const.HVAC_MODE_AUTO,
'HEAT': c_const.HVAC_MODE_HEAT,
'COOL': c_const.HVAC_MODE_COOL,
'FAN': c_const.HVAC_MODE_FAN_ONLY,
'DRY': c_const.HVAC_MODE_DRY,
'ACO': c_const.HVAC_MODE_HEAT_COOL,
}
FAN_MODES = {
'LOW': c_const.FAN_LOW,
'LOW_MID': 'low-mid',
'MID': c_const.FAN_MEDIUM,
'MID_HIGH': 'mid-high',
'HIGH': c_const.FAN_HIGH,
'NATURE': 'nature',
'POWER': 'power',
}
def swing_modes_index():
from wideq import ACHSwingMode, ACVSwingMode
# name: [horz_key, vert_key]
return {'Off': [ACHSwingMode.OFF, ACVSwingMode.OFF],
'Vertical': [ACHSwingMode.OFF, ACVSwingMode.ALL] ,
'Horizontal': [ACHSwingMode.ALL, ACVSwingMode.OFF],
'Vertical and Horizontal': [ACHSwingMode.ALL, ACVSwingMode.ALL] ,
'Up Left': [ACHSwingMode.FIVE, ACVSwingMode.ONE] ,
'Up Right': [ACHSwingMode.ONE, ACVSwingMode.ONE] ,
'Up': [ACHSwingMode.ALL, ACVSwingMode.ONE]}
SWING_MODE_DEFAULT = "Unknown"
MAX_RETRIES = 5
TRANSIENT_EXP = 5.0 # Report set temperature for 5 seconds.
TEMP_MIN_F = 60 # Guessed from actual behavior: API reports are unreliable.
TEMP_MAX_F = 89
TEMP_MIN_C = 18 # Intervals read from the AC's remote control.
TEMP_MAX_C = 30
def setup_platform(hass, config, add_devices, discovery_info=None):
import wideq
refresh_token = config.get(KEY_DEPRECATED_REFRESH_TOKEN) or \
hass.data.get(CONF_TOKEN)
country = config.get(KEY_DEPRECATED_COUNTRY) or \
hass.data.get(CONF_REGION)
language = config.get(KEY_DEPRECATED_LANGUAGE) or \
hass.data.get(CONF_LANGUAGE)
fahrenheit = hass.config.units.temperature_unit != '°C'
client = wideq.Client.from_token(refresh_token, country, language)
add_devices(_ac_devices(hass, client, fahrenheit), True)
def _ac_devices(hass, client, fahrenheit):
"""Generate all the AC (climate) devices associated with the user's
LG account.
Log errors for devices that can't be used for whatever reason.
"""
import wideq
persistent_notification = hass.components.persistent_notification
for device in client.devices:
if device.type == wideq.DeviceType.AC:
try:
d = LGDevice(client, device, fahrenheit)
except wideq.NotConnectedError:
LOGGER.error(
'SmartThinQ device not available: %s', device.name
)
persistent_notification.async_create(
'SmartThinQ device not available: %s' % device.name,
title='SmartThinQ Error',
)
else:
yield d
class LGDevice(ClimateEntity):
def __init__(self, client, device, fahrenheit=True):
self._client = client
self._device = device
self._fahrenheit = fahrenheit
self._attrs = {}
self._has_power = "maybe"
import wideq
self._ac = wideq.ACDevice(client, device)
self._ac.monitor_start()
# The response from the monitoring query.
self._state = None
# Store a transient temperature when we've just set it. We also
# store the timestamp for when we set this value.
self._transient_temp = None
self._transient_time = None
self._swing_mode = SWING_MODE_DEFAULT
@property
def device_state_attributes(self):
return self._attrs
@property
def temperature_unit(self):
if self._fahrenheit:
return const.TEMP_FAHRENHEIT
else:
return const.TEMP_CELSIUS
@property
def name(self):
return self._device.name
@property
def available(self):
return True
@property
def supported_features(self):
return (
c_const.SUPPORT_TARGET_TEMPERATURE |
c_const.SUPPORT_FAN_MODE |
c_const.SUPPORT_SWING_MODE
)
@property
def min_temp(self):
if self._fahrenheit:
return TEMP_MIN_F
else:
return TEMP_MIN_C
@property
def max_temp(self):
if self._fahrenheit:
return TEMP_MAX_F
else:
return TEMP_MAX_C
@property
def current_temperature(self):
if self._state:
if self._fahrenheit:
return self._state.temp_cur_f
else:
return self._state.temp_cur_c
@property
def target_temperature(self):
# Use the recently-set target temperature if it was set recently
# (within TRANSIENT_EXP seconds ago).
if self._transient_temp:
interval = time.time() - self._transient_time
if interval < TRANSIENT_EXP:
return self._transient_temp
else:
self._transient_temp = None
# Otherwise, actually use the device's state.
if self._state:
if self._fahrenheit:
return self._state.temp_cfg_f
else:
return self._state.temp_cfg_c
@property
def hvac_modes(self):
import wideq
return [v for k, v in MODES.items()
if wideq.ACMode[k].value in
self._ac.model.value('SupportOpMode').options.values()] \
+ [c_const.HVAC_MODE_OFF]
@property
def fan_modes(self):
import wideq
return [v for k, v in FAN_MODES.items()
if wideq.ACFanSpeed[k].value in
self._ac.model.value('SupportWindStrength').options.values()]
@property
def swing_mode(self):
# try to find out if the (initial) state matches a known state actually
if self._swing_mode == SWING_MODE_DEFAULT:
for k, v in swing_modes_index().items():
if v[0] == self._state.horz_swing and v[1] == self._state.vert_swing:
self._swing_mode = k
break
else:
return SWING_MODE_DEFAULT
return self._swing_mode
def set_swing_mode(self, swing_mode):
self._swing_mode = swing_mode
LOGGER.info('Setting swing mode to %s...', self._swing_mode)
horiz_mode = swing_modes_index()[self._swing_mode][0]
vert_mode = swing_modes_index()[self._swing_mode][1]
LOGGER.info('Setting device horizontal swing mode to %s...', horiz_mode)
self._ac.set_horz_swing(horiz_mode)
LOGGER.info('Mode set.')
LOGGER.info('Setting device vertical swing mode to %s...', vert_mode)
self._ac.set_vert_swing(vert_mode)
LOGGER.info('Mode set.')
@property
def swing_modes(self):
return [k for k, v in swing_modes_index().items()]
@property
def hvac_mode(self):
if self._state:
if not self._state.is_on:
return c_const.HVAC_MODE_OFF
mode = self._state.mode
return MODES[mode.name]
@property
def fan_mode(self):
if self._state:
mode = self._state.fan_speed
return FAN_MODES[mode.name]
def set_hvac_mode(self, hvac_mode):
if hvac_mode == c_const.HVAC_MODE_OFF:
self._ac.set_on(False)
return
# Some AC units must be powered on before setting the mode.
if not self._state.is_on:
self._ac.set_on(True)
import wideq
# Invert the modes mapping.
modes_inv = {v: k for k, v in MODES.items()}
mode = wideq.ACMode[modes_inv[hvac_mode]]
LOGGER.info('Setting mode to %s...', mode)
self._ac.set_mode(mode)
LOGGER.info('Mode set.')
def set_fan_mode(self, fan_mode):
import wideq
# Invert the fan modes mapping.
fan_modes_inv = {v: k for k, v in FAN_MODES.items()}
mode = wideq.ACFanSpeed[fan_modes_inv[fan_mode]]
LOGGER.info('Setting fan mode to %s', fan_mode)
self._ac.set_fan_speed(mode)
LOGGER.info('Fan mode set.')
def set_temperature(self, **kwargs):
temperature = kwargs['temperature']
self._transient_temp = temperature
self._transient_time = time.time()
LOGGER.info('Setting temperature to %s...', temperature)
if self._fahrenheit:
self._ac.set_fahrenheit(temperature)
else:
self._ac.set_celsius(temperature)
LOGGER.info('Temperature set.')
def check_power(self):
"""Poll for power consumption. If it fails once,
assume it's not supported, and don't try again"""
import wideq
if not self._has_power:
return
try:
power = self._ac.get_power()
if power:
self._attrs['power'] = power
self._has_power = True
except wideq.InvalidRequestError:
LOGGER.info('Power consumption not available.')
self._has_power = False
def update(self):
"""Poll for updated device status.
Set the `_state` field to a new data mapping.
"""
import wideq
LOGGER.info('Updating %s.', self.name)
for iteration in range(MAX_RETRIES):
LOGGER.info('Polling...')
try:
state = self._ac.poll()
except wideq.NotLoggedInError:
LOGGER.info('Session expired. Refreshing.')
self._client.refresh()
self._ac.monitor_start()
continue
except wideq.NotConnectedError:
LOGGER.info('Device not available.')
return
self.check_power()
if state:
LOGGER.info('Status updated.')
self._state = state
return
LOGGER.info('No status available yet.')
time.sleep(2 ** iteration) # Exponential backoff.
# We tried several times but got no result. This might happen
# when the monitoring request gets into a bad state, so we
# restart the task.
LOGGER.warn('Status update failed.')
self._ac.monitor_start()