-
Notifications
You must be signed in to change notification settings - Fork 52
/
airctrl.py
executable file
·473 lines (425 loc) · 17.2 KB
/
airctrl.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
466
467
468
469
470
471
472
473
#!/usr/bin/env python3
import argparse
import sys
import pprint
from pyairctrl.status_transformer import STATUS_TRANSFORMER
from pyairctrl.coap_client import CoAPAirClient
from pyairctrl.http_client import HTTPAirClient
from pyairctrl.plain_coap_client import PlainCoAPAirClient
class CoAPCli:
def __init__(self, host, port=5683, debug=False):
self._client = CoAPAirClient(host, port, debug)
def _get_info_for_key(self, key, current_value):
if key in STATUS_TRANSFORMER:
info = STATUS_TRANSFORMER[key]
if not info[1] is None:
current_value = info[1](current_value)
if current_value is None:
return None
return info[0].format(current_value)
return "{}: {}".format(key, current_value)
def _dump_status(self, status, debug=False):
if debug:
print("Raw status:")
pprint.pprint(status)
for key in status:
current_value = status[key]
name_and_value = self._get_info_for_key(key, current_value)
if name_and_value is None:
continue
print(
"[{key}]\t{name_and_value}".format(
key=key, name_and_value=name_and_value
).expandtabs(30)
)
def get_status(self, debug=False):
status = self._client.get_status(debug)
if status is not None:
return self._dump_status(status, debug=debug)
def set_values(self, values, debug=False):
self._client.set_values(values, debug)
def get_firmware(self):
status = self._client.get_firmware()
if status is None:
print("No version-info found")
return
print(
self._get_info_for_key(
"swversion", status["swversion"] if "swversion" in status else "nA"
)
)
print(self._get_info_for_key("ota", status["ota"] if "ota" in status else "nA"))
def get_filters(self):
status = self._client.get_filters()
if status is None:
print("No version-info found")
return
if "fltsts0" in status:
print(self._get_info_for_key("fltsts0", status["fltsts0"]))
if "fltsts1" in status:
print(self._get_info_for_key("fltsts1", status["fltsts1"]))
if "fltsts2" in status:
print(self._get_info_for_key("fltsts2", status["fltsts2"]))
if "wicksts" in status:
print(self._get_info_for_key("wicksts", status["wicksts"]))
def get_wifi(self):
print(
"Getting wifi credentials is currently not supported when using CoAP. Use the app instead."
)
def set_wifi(self, ssid, pwd):
print(
"Setting wifi credentials is currently not supported when using CoAP. Use the app instead."
)
class HTTPAirCli:
@staticmethod
def ssdp(timeout=1, repeats=3, debug=False):
response = HTTPAirClient.ssdp(timeout, repeats)
if debug:
pprint.pprint(response)
return response
def __init__(self, host):
self._client = HTTPAirClient(host)
def set_values(self, values, debug=False):
try:
values = self._client.set_values(values)
self._dump_status(values, debug=debug)
except urllib.error.HTTPError as e:
print("Error setting values (response code: {})".format(e.code))
def set_wifi(self, ssid, pwd):
values = {}
if ssid:
values["ssid"] = ssid
if pwd:
values["password"] = pwd
pprint.pprint(values)
wifi = self._client.set_wifi(ssid, pwd)
pprint.pprint(wifi)
def load_key(self):
self._client.load_key()
def _dump_status(self, status, debug=False):
if debug:
pprint.pprint(status)
print()
if "pwr" in status:
pwr = status["pwr"]
pwr_str = {"1": "ON", "0": "OFF"}
pwr = pwr_str.get(pwr, pwr)
print("[pwr] Power: {}".format(pwr))
if "pm25" in status:
pm25 = status["pm25"]
print("[pm25] PM25: {}".format(pm25))
if "rh" in status:
rh = status["rh"]
print("[rh] Humidity: {}".format(rh))
if "rhset" in status:
rhset = status["rhset"]
print("[rhset] Target humidity: {}".format(rhset))
if "iaql" in status:
iaql = status["iaql"]
print("[iaql] Allergen index: {}".format(iaql))
if "temp" in status:
temp = status["temp"]
print("[temp] Temperature: {}".format(temp))
if "func" in status:
func = status["func"]
func_str = {"P": "Purification", "PH": "Purification & Humidification"}
func = func_str.get(func, func)
print("[func] Function: {}".format(func))
if "mode" in status:
mode = status["mode"]
mode_str = {
"P": "auto",
"A": "allergen",
"S": "sleep",
"M": "manual",
"B": "bacteria",
"N": "night",
}
mode = mode_str.get(mode, mode)
print("[mode] Mode: {}".format(mode))
if "om" in status:
om = status["om"]
om_str = {"s": "silent", "t": "turbo"}
om = om_str.get(om, om)
print("[om] Fan speed: {}".format(om))
if "aqil" in status:
aqil = status["aqil"]
print("[aqil] Light brightness: {}".format(aqil))
if "uil" in status:
uil = status["uil"]
uil_str = {"1": "ON", "0": "OFF"}
uil = uil_str.get(uil, uil)
print("[uil] Buttons light: {}".format(uil))
if "ddp" in status:
ddp = status["ddp"]
ddp_str = {"1": "PM2.5", "0": "IAI"}
ddp = ddp_str.get(ddp, ddp)
print("[ddp] Used index: {}".format(ddp))
if "wl" in status:
wl = status["wl"]
print("[wl] Water level: {}".format(wl))
if "cl" in status:
cl = status["cl"]
print("[cl] Child lock: {}".format(cl))
if "dt" in status:
dt = status["dt"]
if dt != 0:
print("[dt] Timer: {} hours".format(dt))
if "dtrs" in status:
dtrs = status["dtrs"]
if dtrs != 0:
print("[dtrs] Timer: {} minutes left".format(dtrs))
if "err" in status:
err = status["err"]
if err != 0:
err_str = {
49408: "no water",
32768: "water tank open",
49155: "pre-filter must be cleaned",
}
err = err_str.get(err, err)
print("-" * 20)
print("Error: {}".format(err))
def get_status(self, debug=False):
status = self._client.get_status()
self._dump_status(status, debug=debug)
def get_wifi(self):
wifi = self._client.get_wifi()
pprint.pprint(wifi)
def get_firmware(self):
firmware = self._client.get_firmware()
pprint.pprint(firmware)
def get_filters(self):
filters = self._client.get_filters()
print("Pre-filter and Wick: clean in {} hours".format(filters["fltsts0"]))
if "wicksts" in filters:
print("Wick filter: replace in {} hours".format(filters["wicksts"]))
print("Active carbon filter: replace in {} hours".format(filters["fltsts2"]))
print("HEPA filter: replace in {} hours".format(filters["fltsts1"]))
class PlainCoAPAirCli:
def __init__(self, host, port=5683):
self._client = PlainCoAPAirClient(host, port)
def _dump_status(self, status, debug=False):
if debug:
print("Raw status: " + str(status))
if "name" in status:
name = status["name"]
print("[name] Name: {}".format(name))
if "modelid" in status:
modelid = status["modelid"]
print("[modelid] ModelId: {}".format(modelid))
if "swversion" in status:
swversion = status["swversion"]
print("[swversion] Version: {}".format(swversion))
if "StatusType" in status:
statustype = status["StatusType"]
print("[StatusType] StatusType: {}".format(statustype))
if "ota" in status:
ota = status["ota"]
print("[ota] Over the air updates: {}".format(ota))
if "Runtime" in status:
runtime = status["Runtime"]
print(
"[Runtime] Runtime: {} hours".format(
round(((runtime / (1000 * 60 * 60)) % 24), 2)
)
)
if "pwr" in status:
pwr = status["pwr"]
pwr_str = {"1": "ON", "0": "OFF"}
pwr = pwr_str.get(pwr, pwr)
print("[pwr] Power: {}".format(pwr))
if "pm25" in status:
pm25 = status["pm25"]
print("[pm25] PM25: {}".format(pm25))
if "rh" in status:
rh = status["rh"]
print("[rh] Humidity: {}".format(rh))
if "rhset" in status:
rhset = status["rhset"]
print("[rhset] Target humidity: {}".format(rhset))
if "iaql" in status:
iaql = status["iaql"]
print("[iaql] Allergen index: {}".format(iaql))
if "temp" in status:
temp = status["temp"]
print("[temp] Temperature: {}".format(temp))
if "func" in status:
func = status["func"]
func_str = {"P": "Purification", "PH": "Purification & Humidification"}
func = func_str.get(func, func)
print("[func] Function: {}".format(func))
if "mode" in status:
mode = status["mode"]
mode_str = {
"P": "auto",
"A": "allergen",
"S": "sleep",
"M": "manual",
"B": "bacteria",
"N": "night",
}
mode = mode_str.get(mode, mode)
print("[mode] Mode: {}".format(mode))
if "om" in status:
om = status["om"]
om_str = {"s": "silent", "t": "turbo"}
om = om_str.get(om, om)
print("[om] Fan speed: {}".format(om))
if "aqil" in status:
aqil = status["aqil"]
print("[aqil] Light brightness: {}".format(aqil))
if "uil" in status:
uil = status["uil"]
uil_str = {"1": "ON", "0": "OFF"}
uil = uil_str.get(uil, uil)
print("[uil] Buttons light: {}".format(uil))
if "ddp" in status:
ddp = status["ddp"]
ddp_str = {"3": "Humidity", "1": "PM2.5", "0": "IAI"}
ddp = ddp_str.get(ddp, ddp)
print("[ddp] Used index: {}".format(ddp))
if "wl" in status:
wl = status["wl"]
print("[wl] Water level: {}".format(wl))
if "cl" in status:
cl = status["cl"]
print("[cl] Child lock: {}".format(cl))
if "dt" in status:
dt = status["dt"]
if dt != 0:
print("[dt] Timer: {} hours".format(dt))
if "dtrs" in status:
dtrs = status["dtrs"]
if dtrs != 0:
print("[dtrs] Timer: {} minutes left".format(dtrs))
if "fltsts0" in status:
fltsts0 = status["fltsts0"]
print(
"[fltsts0] Pre-filter and Wick: clean in {} hours".format(fltsts0)
)
if "fltsts1" in status:
fltsts1 = status["fltsts1"]
print("[fltsts1] HEPA filter: replace in {} hours".format(fltsts1))
if "fltsts2" in status:
fltsts2 = status["fltsts2"]
print(
"[fltsts2] Active carbon filter: replace in {} hours".format(
fltsts2
)
)
if "wicksts" in status:
wicksts = status["wicksts"]
print("[wicksts] Wick filter: replace in {} hours".format(wicksts))
if "err" in status:
err = status["err"]
if err != 0:
err_str = {
49408: "no water",
32768: "water tank open",
49155: "pre-filter must be cleaned",
}
err = err_str.get(err, err)
print("-" * 20)
print("[ERROR] Message: {}".format(err))
def set_values(self, values, debug=False):
self._client.set_values(values, debug)
def get_status(self, debug=False):
status = self._client.get_status(debug)
return self._dump_status(status, debug=debug)
def get_wifi(self):
print(
"Getting wifi credentials is currently not supported when using CoAP. Use the app instead."
)
def set_wifi(self, ssid, pwd):
print(
"Setting wifi credentials is currently not supported when using CoAP. Use the app instead."
)
def get_firmware(self):
status = self._client.get_firmware()
print("Software version: {}".format(status["swversion"]))
print("Over the air updates: {}".format(status["ota"]))
def get_filters(self):
status = self._client.get_filters()
print("Pre-filter and Wick: clean in {} hours".format(status["fltsts0"]))
print("HEPA filter: replace in {} hours".format(status["fltsts1"]))
print("Active carbon filter: replace in {} hours".format(status["fltsts2"]))
print("Wick filter: replace in {} hours".format(status["wicksts"]))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--ipaddr', help='IP address of air purifier')
parser.add_argument('--protocol', help='set the communication protocol', choices=['http', 'coap', 'plain_coap'], default='http')
parser.add_argument('-d', '--debug', help='show debug output', action='store_true')
parser.add_argument('--om', help='set fan speed', choices=['1','2','3','s','t'])
parser.add_argument('--pwr', help='power on/off', choices=['0','1'])
parser.add_argument('--mode', help='set mode', choices=['P','A','S','M','B','N'])
parser.add_argument('--rhset', help='set target humidity', choices=['40','50','60','70'])
parser.add_argument('--func', help='set function', choices=['P','PH'])
parser.add_argument('--aqil', help='set light brightness', choices=['0','25','50','75','100'])
parser.add_argument('--uil', help='set button lights on/off', choices=['0','1'])
parser.add_argument('--ddp', help='set indicator pm2.5/IAI/Humidity', choices=['0','1','3'])
parser.add_argument('--dt', help='set timer', choices=['0','1','2','3','4','5','6','7','8','9','10','11','12'])
parser.add_argument('--cl', help='set child lock', choices=['True','False'])
parser.add_argument('--wifi', help='read wifi options', action='store_true')
parser.add_argument('--wifi-ssid', help='set wifi ssid')
parser.add_argument('--wifi-pwd', help='set wifi password')
parser.add_argument('--firmware', help='read firmware', action='store_true')
parser.add_argument('--filters', help='read filters status', action='store_true')
args = parser.parse_args()
if args.ipaddr:
devices = [ {'ip': args.ipaddr} ]
else:
if args.protocol in ['coap', 'plain_coap']:
print('Autodetection is not supported when using CoAP. Use --ipaddr to set an IP address.')
sys.exit(1)
devices = HTTPAirCli.ssdp(debug=args.debug)
if not devices:
print('Air purifier not autodetected. Try --ipaddr option to force specific IP address.')
sys.exit(1)
for device in devices:
if args.protocol == 'http':
c = HTTPAirCli(device['ip'])
c.load_key()
elif args.protocol == 'plain_coap':
c = PlainCoAPAirCli(device['ip'])
elif args.protocol == 'coap':
c = CoAPCli(device['ip'], debug=args.debug)
if args.wifi:
c.get_wifi()
sys.exit(0)
if args.firmware:
c.get_firmware()
sys.exit(0)
if args.wifi_ssid or args.wifi_pwd:
c.set_wifi(args.wifi_ssid, args.wifi_pwd)
sys.exit(0)
if args.filters:
c.get_filters()
sys.exit(0)
values = {}
if args.om:
values['om'] = args.om
if args.pwr:
values['pwr'] = args.pwr
if args.mode:
values['mode'] = args.mode
if args.rhset:
values['rhset'] = int(args.rhset)
if args.func:
values['func'] = args.func
if args.aqil:
values['aqil'] = int(args.aqil)
if args.ddp:
values['ddp'] = args.ddp
if args.uil:
values['uil'] = args.uil
if args.dt:
values['dt'] = int(args.dt)
if args.cl:
values['cl'] = (args.cl == 'True')
if values:
c.set_values(values, debug=args.debug)
else:
c.get_status(debug=args.debug)
if __name__ == '__main__':
main()