-
Notifications
You must be signed in to change notification settings - Fork 13
/
dehumi_control.py
389 lines (324 loc) · 9.65 KB
/
dehumi_control.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
import sys
import getopt
import time
import datetime
import requests
import json
import logging
from midea_inventor_lib import MideaClient
#from midea_inventor_lib import MideaSecurity
#from midea_inventor_lib import DataBodyDeHumiQuery
#from midea_inventor_lib import DataBodyDeHumiResponse
#from midea_inventor_lib import MideaDehumidificationDevice
#from midea_inventor_lib import DataBodyDeHumiRequest
client = None
sessionId = ""
deviceId = ""
def do_login():
global client
global sessionId
res = client.login()
if res == -1:
print("Login error: please check log messages.")
else:
sessionId = client.current["sessionId"]
def list_appliances():
global client
if client.security.access_token is None:
print("Please login first!\n")
return None
appliances = {}
appliances = client.listAppliances()
for a in appliances:
print("[id="+a["id"]+" type="+a["type"]+" name="+a["name"]+"]")
if a["onlineStatus"] == "1":
print("is online")
else:
print("is offline")
if a["activeStatus"] == "1":
print("is active")
else:
print("is not active")
#The first appliance having type="0xA1" is returned for default (otherwise, 'Set deviceId' option can be used)
return appliances
def setDeviceId():
global client
global deviceId
id = str(input("deviceId ["+deviceId+"]: "))
if id:
deviceId = id
def getDeviceStatus():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
else:
res = client.get_device_status(deviceId)
if res == 1:
print(client.deviceStatus.toString())
def power_on():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
elif client.deviceStatus is None:
print("Please get device status first!\n")
elif client.deviceStatus.powerMode == 1:
print("Device is already on.\n")
else:
res = client.send_poweron_command(deviceId)
if res is not None:
print(client.deviceStatus.toString())
def power_off():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
elif client.deviceStatus is None:
print("Please get device status first!\n")
elif client.deviceStatus.powerMode == 0:
print("Device is already off.\n")
else:
res = client.send_poweroff_command(deviceId)
if res:
print(client.deviceStatus.toString())
def ion_on():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
elif client.deviceStatus is None:
print("Please get device status first!\n")
elif client.deviceStatus.powerMode == 0:
print("Device is off.\n")
elif client.deviceStatus.ionSetSwitch == 1:
print("Ion is already on.\n")
else:
res = client.send_ion_on_command(deviceId)
if res:
print(client.deviceStatus.toString())
def ion_off():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
elif client.deviceStatus is None:
print("Please get device status first!\n")
elif client.deviceStatus.powerMode == 0:
print("Device is off.\n")
elif client.deviceStatus.ionSetSwitch == 0:
print("Ion is already off.\n")
else:
res = client.send_ion_off_command(deviceId)
if res:
print(client.deviceStatus.toString())
def set_speed():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
elif client.deviceStatus is None:
print("Please get device status first!\n")
elif client.deviceStatus.powerMode == 0:
print("Device is off.\n")
else:
loop = True
while loop:
print("Please remember that speed value cannot be changed in DRY MODE");
speedStr = str(input("Speed [0..100] (silent:40, medium:60, high:80):"))
try:
speed = int(speedStr)
except ValueError:
print("input value is not a valid integer, please retry.")
else:
if speed > 0 and speed < 100:
loop = False
else:
print("input value is not in the valid range, please retry.")
if speed > 0 and speed < 100:
res = client.send_fan_speed_command(deviceId, speed)
if res:
print(client.deviceStatus.toString())
def set_humidity():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
elif client.deviceStatus is None:
print("Please get device status first!\n")
elif client.deviceStatus.powerMode == 0:
print("Device is off.\n")
else:
loop = True
while loop:
humStr = str(input("Target Humidity [30..70]:"))
try:
hum = int(humStr)
except ValueError:
print("input value is not a valid integer, retry.")
else:
if hum >= 30 and hum <= 70:
loop = False
else:
print("input value is not in the valid range, please retry.")
if hum >= 30 and hum <= 70:
res = client.send_target_humidity_command(deviceId, hum)
if res is not None:
print(client.deviceStatus.toString())
def set_mode():
global client
global deviceId
if client.security.access_token is None:
print("Please login first!\n")
elif not deviceId:
print("Please set deviceId first!\n")
elif client.deviceStatus is None:
print("Please get device status first!\n")
elif client.deviceStatus.powerMode == 0:
print("Device is off.\n")
else:
loop = True
while loop:
modeStr = str(input("Mode [1..4] (1: TARGET_MODE, 2: CONTINUOUS_MODE, 3: SMART_MODE, 4: DRYER_MODE): "))
try:
mode= int(modeStr)
except ValueError:
print("input value is not a valid integer, retry.")
else:
loop = False
if mode > 0 and mode < 5:
res = client.send_mode_command(deviceId, mode)
if res:
print(client.deviceStatus.toString())
def menu():
global sessionId
global deviceId
global deviceStatus
print("=============================")
if not sessionId:
print("0: Login")
else:
print("0: Login [Logged in: sessionId="+sessionId+"]")
print("1: List configured appliances")
if not deviceId:
print("2: Set deviceID")
else:
print("2: Set deviceID [deviceId="+deviceId+"]")
if client.deviceStatus is None:
print("3: Get device status")
else:
print("3: Get device status ["+client.deviceStatus.toString()+"]")
print("4: Send power on command")
print("5: Send power off command")
print("6: Send fan speed command")
print("7: Send Mode command")
print("8: Send Ion switch on command")
print("9: Send Ion switch off command")
print("10: Set humidity point")
print("11: Set on-off timer")
print("99: Exit")
def main(argv):
global client
global deviceId
_email = ""
_password = ""
_sha256password = ""
_log = False
_logfile = ""
_verbose = False
_debug = False
try:
opts, args = getopt.getopt(sys.argv[1:], "e:p:s:l:hvd", ["email=", "password=", "md5_password="])
except getopt.GetoptError:
print("Usage:"+sys.argv[0]+" -e <email_address> -p <cleartext password> -s <sha256_password> -l <logfile> [-h] [-v] [-d]")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("Usage:"+sys.argv[0]+" -e <email_address> -p <cleartext password> -s <sha256_password> -l <logfile> [-h] [-v] [-d]")
sys.exit()
elif opt == '-d':
_debug = True
elif opt == '-v':
_verbose = True
elif opt in ("-e", "--email"):
_email = arg
elif opt in ("-p", "--password"):
_password = arg
elif opt in ("-s", "--sha256_password"):
_sha256password = arg
elif opt in ("-l", "--logfile"):
_logfile = arg
_log = True
if _debug:
print("---ARGS---")
print("- email: "+_email)
print("- password: "+_password)
print("- sha256password: "+_sha256password)
print("- log: "+str(_log))
print("- logfile: "+_logfile)
print("- debug: "+str(_debug))
print("- verbose: "+str(_verbose))
print("----------")
if not _email:
print("Error: you must specify -e input parameter")
sys.exit(2)
if not _password and not _sha256password:
print("Error: you must specify -p or -s input parameter")
sys.exit(2)
else:
#client = MideaClient(_email, _password, _sha256password)
client = MideaClient(_email, _password, _sha256password, _debug, _verbose, _logfile)
while True:
menu()
cmd = str(input("Choose a command: "))
if cmd == "0":
#Perform login
do_login()
elif cmd == "1":
appliances = list_appliances()
if appliances is not None:
for a in appliances:
if a["type"] == "0xA1":
deviceId = str(a["id"])
print("deviceID="+deviceId)
elif cmd == "2":
setDeviceId()
elif cmd == "3":
getDeviceStatus()
elif cmd == "4":
power_on()
elif cmd == "5":
power_off()
elif cmd == "6":
set_speed()
elif cmd == "7":
set_mode()
elif cmd == "8":
ion_on()
elif cmd == "9":
ion_off()
elif cmd == "10":
set_humidity()
elif cmd == "11":
print("TODO\n")
elif cmd == "99":
sys.exit(0)
else:
print("Unrecognized command!\n")
#Call main() function
if __name__ == "__main__":
main(sys.argv)