forked from sampsyo/hass-smartthinq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartthinq.py
204 lines (163 loc) · 5.55 KB
/
smartthinq.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
import logging
import voluptuous as vol
from homeassistant.components import climate
import homeassistant.helpers.config_validation as cv
from homeassistant import const
import time
REQUIREMENTS = ['wideq']
LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = climate.PLATFORM_SCHEMA.extend({
vol.Required('refresh_token'): cv.string,
})
MODES = {
'HEAT': climate.STATE_HEAT,
'COOL': climate.STATE_COOL,
'DRY': climate.STATE_DRY,
'FAN': climate.STATE_FAN_ONLY,
'ENERGY_SAVING': climate.STATE_ECO,
'ACO': climate.STATE_AUTO
}
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
def setup_platform(hass, config, add_devices, discovery_info=None):
import wideq
refresh_token = config.get('refresh_token')
client = wideq.Client.from_token(refresh_token)
add_devices(
LGDevice(client, device)
for device in client.devices
if device.type == wideq.DeviceType.AC
)
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
self.update()
@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 (
climate.SUPPORT_TARGET_TEMPERATURE |
climate.SUPPORT_OPERATION_MODE |
climate.SUPPORT_ON_OFF
)
@property
def min_temp(self):
if self._fahrenheit:
return TEMP_MIN_F
return climate.ClimateDevice.min_temp.fget(self)
@property
def max_temp(self):
if self._fahrenheit:
return TEMP_MAX_F
return climate.ClimateDevice.max_temp.fget(self)
@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 operation_list(self):
return list(MODES.values())
@property
def current_operation(self):
if self._state:
mode = self._state.mode
return MODES[mode.name]
@property
def is_on(self):
if self._state:
return self._state.is_on
def set_operation_mode(self, operation_mode):
import wideq
# Invert the modes mapping.
modes_inv = {v: k for k, v in MODES.items()}
mode = wideq.ACMode[modes_inv[operation_mode]]
LOGGER.info('Setting mode to %s...', mode)
self._ac.set_mode(mode)
LOGGER.info('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 turn_on(self):
LOGGER.info('Turning on...')
self._ac.set_on(True)
LOGGER.info('...done.')
def turn_off(self):
LOGGER.info('Turning off...')
self._ac.set_on(False)
LOGGER.info('...done.')
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
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()