-
Notifications
You must be signed in to change notification settings - Fork 10
/
melview.py
465 lines (357 loc) · 13.8 KB
/
melview.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/local/bin/python3
'''
Author: zacharyrs
How to install:
Refer to README.md
License:
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
'''
import logging
import time
import requests
from homeassistant.components.climate.const import (
HVAC_MODE_OFF,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_HEAT,
HVAC_MODE_FAN_ONLY,
FAN_AUTO,
FAN_LOW,
FAN_MEDIUM,
FAN_HIGH,
SUPPORT_FAN_MODE,
SUPPORT_TARGET_TEMPERATURE
)
_LOGGER = logging.getLogger(__name__)
APPVERSION = '5.3.1330'
APIVERSION = 3
HEADERS = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}
LOCAL_DATA = """<?xml version="1.0" encoding="UTF-8"?>
<CSV>
<CONNECT>ON</CONNECT>
<CODE>
<VALUE>{}</VALUE>
</CODE>
</CSV>"""
# ---------------------------------------------------------------
MODE = {
HVAC_MODE_AUTO: 8,
HVAC_MODE_HEAT: 1,
HVAC_MODE_COOL: 3,
HVAC_MODE_DRY: 2,
HVAC_MODE_FAN_ONLY: 7
}
FAN = {
FAN_AUTO: 0,
FAN_LOW: 2,
FAN_MEDIUM: 3,
FAN_HIGH: 5
}
# ---------------------------------------------------------------
class MelViewAuthentication:
""" Implementation to remember and refresh melview cookies.
"""
def __init__(self, email, password):
self._email = email
self._password = password
self._cookie = None
def is_login(self):
""" Return login status.
"""
return self._cookie is not None
def login(self):
""" Generate a new login cookie.
"""
_LOGGER.debug('trying to login')
self._cookie = None
req = requests.post('https://api.melview.net/api/login.aspx',
json={'user': self._email, 'pass': self._password,
'appversion': APPVERSION},
headers=HEADERS)
if req.status_code == 200:
cks = req.cookies
if 'auth' in cks:
self._cookie = cks['auth']
return True
_LOGGER.error('missing auth cookie -> cookies: %s', cks)
else:
_LOGGER.error('login status code: %d', req.status_code)
return False
def get_cookie(self):
""" Return authentication cookie.
"""
return {'auth': self._cookie}
# ---------------------------------------------------------------
class MelViewDevice:
""" Handler class for a melview unit.
"""
def __init__(self, deviceid, buildingid, friendlyname,
authentication, localcontrol=False):
self._deviceid = deviceid
self._buildingid = buildingid
self._friendlyname = friendlyname
self._authentication = authentication
self._caps = None
self._localip = localcontrol
self._info_lease_seconds = 30 # Data lasts for 30s.
self._json = None
self._rtemp_list = []
self._otemp_list = []
self._refresh_device_caps()
self._refresh_device_info()
def __str__(self):
return str(self._json)
def _refresh_device_caps(self, retry=True):
self._json = None
self._last_info_time_s = time.time()
req = requests.post('https://api.melview.net/api/unitcapabilities.aspx',
cookies=self._authentication.get_cookie(),
json={'unitid': self._deviceid, 'v': APIVERSION})
if req.status_code == 200:
self._caps = req.json()
if self._localip and 'localip' in self._caps:
self._localip = self._caps['localip']
return True
if req.status_code == 401 and retry:
_LOGGER.error('caps error 401 (trying to re-login)')
if self._authentication.login():
return self._refresh_device_caps(retry=False)
else:
_LOGGER.error('unable to retrieve caps ' \
'(invalid status code: %d)', req.status_code)
return False
def _refresh_device_info(self, retry=True):
self._json = None
self._last_info_time_s = time.time()
req = requests.post('https://api.melview.net/api/unitcommand.aspx',
cookies=self._authentication.get_cookie(),
json={'unitid': self._deviceid, 'v': APIVERSION})
if req.status_code == 200:
self._json = req.json()
if 'roomtemp' in self._json:
self._rtemp_list.append(float(self._json['roomtemp']))
# Keep only last 10 temperature values.
self._rtemp_list = self._rtemp_list[-10:]
if 'outdoortemp' in self._json:
self._otemp_list.append(float(self._json['outdoortemp']))
# Keep only last 10 temperature values.
self._otemp_list = self._otemp_list[-10:]
return True
if req.status_code == 401 and retry:
_LOGGER.error('info error 401 (trying to re-login)')
if self._authentication.login():
return self._refresh_device_info(retry=False)
else:
_LOGGER.error('unable to retrieve info (invalid status code: %d)',
req.status_code)
return False
def _is_info_valid(self):
if self._json is None:
return self._refresh_device_info()
if (time.time() - self._last_info_time_s) >= self._info_lease_seconds:
_LOGGER.debug('current settings out of date, refreshing')
return self._refresh_device_info()
return True
def _is_caps_valid(self):
if self._caps is None:
return self._refresh_device_caps()
return True
def _send_command(self, command, retry=True):
_LOGGER.debug('command issued %s', command)
if not self._is_info_valid():
_LOGGER.error('data outdated, command %s failed', command)
return False
req = requests.post('https://api.melview.net/api/unitcommand.aspx',
cookies=self._authentication.get_cookie(),
json={'unitid': self._deviceid, 'v': APIVERSION,
'commands': command, 'lc': 1})
if req.status_code == 200:
_LOGGER.debug('command sent to remote')
resp = req.json()
if self._localip:
if 'lc' in resp:
local_command = req.json()['lc']
req = requests.post('http://{}/smart'.format(self._localip),
data=LOCAL_DATA.format(local_command))
if req.status_code == 200:
_LOGGER.debug('command sent locally')
else:
_LOGGER.error('local submission failed')
else:
_LOGGER.error('missing local command key')
return True
if req.status_code == 401 and retry:
_LOGGER.error('command send error 401 (trying to relogin)')
if self._authentication.login():
return self._send_command(command, retry=False)
else:
_LOGGER.error('unable to send command (invalid status code: %d',
req.status_code)
return False
def force_update(self):
""" Force info refresh
"""
return self._refresh_device_info()
def get_id(self):
""" Get device ID.
"""
return self._deviceid
def get_friendly_name(self):
""" Get customised device name.
"""
return self._friendlyname
def get_precision_halves(self):
""" Get unit support for half degrees.
"""
if not self._is_caps_valid():
return False
return 'halfdeg' in self._caps and self._caps['halfdeg'] == 1
def get_temperature(self):
""" Get set temperature.
"""
if not self._is_info_valid():
return 0
return float(self._json['settemp'])
def get_room_temperature(self):
""" Get current room temperature.
"""
if not self._is_info_valid():
return 0
if not self._rtemp_list:
return 0 # Avoid div 0.
return round(sum(self._rtemp_list) / len(self._rtemp_list), 1)
def get_outside_temperature(self):
""" Get current outside temperature.
"""
if not 'hasoutdoortemp' in self._caps or self._caps['hasoutdoortemp'] == 0:
_LOGGER.error('outdoor temperature not supported')
return 0
if not self._is_info_valid():
return 0
if not self._otemp_list:
return 0 # Avoid div 0.
return round((sum(self._otemp_list) / len(self._otemp_list)), 1)
def get_speed(self):
""" Get the set fan speed.
"""
if not self._is_info_valid():
return 'Auto'
for key, val in FAN.items():
if self._json['setfan'] == val:
return key
return 'Auto'
def get_mode(self):
""" Get the set mode.
"""
if not self._is_info_valid():
return 'Auto'
if self.is_power_on():
for key, val in MODE.items():
if self._json['setmode'] == val:
return key
return 'Auto'
def is_power_on(self):
""" Check unit is on.
"""
if not self._is_info_valid():
return False
return self._json['power']
def set_temperature(self, temperature):
""" Set the target temperature.
"""
mode = self.get_mode()
min_temp = self._caps['max'][str(MODE[mode])]['min']
max_temp = self._caps['max'][str(MODE[mode])]['max']
if temperature < min_temp:
_LOGGER.error('temp %.1f lower than min %d for mode %d',
temperature, min_temp, mode)
return False
if temperature > max_temp:
_LOGGER.error('temp %.1f greater than max %d for mode %d',
temperature, max_temp, mode)
return False
return self._send_command('TS{:.2f}'.format(temperature))
def set_speed(self, speed):
""" Set the fan speed.
"""
if not self.is_power_on():
# Try turn on the unit if off.
if not self.power_on():
return False
if speed == 'Auto' and (not 'hasautofan' in self._caps or self._caps['hasautofan'] == 0):
_LOGGER.error('fan speed auto not supported')
return False
if speed not in FAN.keys():
_LOGGER.error('fan speed %d not supported', speed)
return False
return self._send_command('FS{:.2f}'.format(FAN[speed]))
def set_mode(self, mode):
""" Set operating mode.
"""
if not self.is_power_on():
# Try turn on the unit if off.
if not self.power_on():
return False
if mode == 'Auto' and (not 'hasautomode' in self._caps or self._caps['hasautomode'] == 0):
_LOGGER.error('auto mode not supported')
return False
if mode == 'Dry' and (not 'hasdrymode' in self._caps or self._caps['hasdrymode'] == 0):
_LOGGER.error('dry mode not supported')
return False
if mode != 'Cool' and ('hascoolonly' in self._caps and self._caps['hascoolonly'] == 1):
_LOGGER.error('only cool mode supported')
return False
if mode not in MODE.keys():
_LOGGER.error('mode %d not supported', mode)
return False
return self._send_command('MD{}'.format(MODE[mode]))
def power_on(self):
""" Turn on the unit.
"""
return self._send_command('PW1')
def power_off(self):
""" Turn off the unit.
"""
return self._send_command('PW0')
# ---------------------------------------------------------------
class MelView:
""" Handler for multiple melview devices under one user.
"""
def __init__(self, authentication, localcontrol=False):
self._authentication = authentication
self._unitcount = 0
self._localcontrol = localcontrol
def get_devices_list(self, retry=True):
""" Return all the devices found, as handlers.
"""
devices = []
req = requests.post('https://api.melview.net/api/rooms.aspx',
json={'unitid': 0},
headers=HEADERS,
cookies=self._authentication.get_cookie())
if req.status_code == 200:
reply = req.json()
for building in reply:
for unit in building['units']:
devices.append(MelViewDevice(unit['unitid'],
building['buildingid'],
unit['room'],
self._authentication,
self._localcontrol))
elif req.status_code == 401 and retry:
_LOGGER.error('device list error 401 (trying to re-login)')
if self._authentication.login():
return self.get_devices_list(retry=False)
else:
_LOGGER.error('failed to get device list (status code invalid: %d)',
req.status_code)
return devices