-
Notifications
You must be signed in to change notification settings - Fork 0
/
tplinkdevice.py
76 lines (50 loc) · 1.97 KB
/
tplinkdevice.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
import json
from typing import Any, Union
from protocol import TPLinkProtocol
class TPLinkDevice:
sysinfo = None
def __init__(self, protocol: TPLinkProtocol, sysinfo: dict = None):
self.protocol = protocol
if sysinfo is not None:
self.sysinfo = sysinfo
def __eq__(self, other):
return type(self) == type(other) and self.protocol == other.protocol
def __hash__(self):
return hash(type(self)) ^ hash(self.protocol)
def send(self, msg: Union[str, dict]) -> Any:
with self.protocol as conn:
# depending on the protocol there might not be a response
r = conn.send(msg)
if r is not None:
if r == 'Module not support':
if type(msg) is str:
msg = json.dumps(msg)
raise ValueError('Device does not support module "' + next(iter(msg)) + '"')
if isinstance(r, dict):
if r['err_code'] is not 0:
raise ValueError(r['err_code'], r['err_msg'])
del r['err_code']
return r
def get_sysinfo(self) -> dict:
self.sysinfo = self.send({
'system': {
'get_sysinfo': None
}
})
return self.sysinfo
def reboot(self, delay: int = None) -> Any:
return self.send({
'smartlife.iot.common.system': {
'reboot': {
'delay': delay
}
}
})
def set_dev_alias(self, alias: str) -> Any:
return self.send({
'system': {
'set_dev_alias': {
'alias': alias
}
}
})