forked from sampsyo/hass-smartthinq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimate.py
275 lines (222 loc) · 7.96 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
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, DEPRECATION_WARNING, KEY_DEPRECATED_COUNTRY,
KEY_DEPRECATED_LANGUAGE, KEY_DEPRECATED_REFRESH_TOKEN)
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 = {
'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',
}
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
if any(key in config for key in (
(KEY_DEPRECATED_REFRESH_TOKEN,
KEY_DEPRECATED_COUNTRY,
KEY_DEPRECATED_LANGUAGE))):
LOGGER.warning(DEPRECATION_WARNING)
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(climate.ClimateDevice):
def __init__(self, client, device, fahrenheit=True):
self._client = client
self._device = device
self._fahrenheit = fahrenheit
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
@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
)
@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):
return list(MODES.values()) + [c_const.HVAC_MODE_OFF]
@property
def fan_modes(self):
return list(FAN_MODES.values())
@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):
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 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
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()