-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp100.py
executable file
·252 lines (193 loc) · 6.23 KB
/
p100.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
#!/usr/bin/python3.9
import os, sys
import asyncio
import json
from typing import Any, Dict, List
from plugp100.api.plug_device import PlugDevice
from plugp100.api.tapo_client import TapoClient
from plugp100.common.credentials import AuthCredential
credentials = None
client = None
plug = None
deviceInfo = None
loop = None
username = None
password = None
ip = None
output = None
async def async_tapo_login():
global credentials, client, plug
credentials = AuthCredential(username, password)
client = TapoClient(credentials, ip)
await client.initialize()
plug = PlugDevice(client)
async def async_tapo_logout():
global client
await client.close()
async def async_tapo_on():
global plug
await plug.on()
async def async_tapo_off():
global plug
await plug.off()
async def async_tapo_info():
global plug, deviceInfo
deviceInfo = await plug.get_state()
def tapo_login():
global loop
loop = asyncio.new_event_loop()
loop.run_until_complete(async_tapo_login())
loop.run_until_complete(asyncio.sleep(0.1))
def tapo_logout():
global loop
loop.run_until_complete(async_tapo_logout())
loop.run_until_complete(asyncio.sleep(0.1))
loop.close()
def tapo_on():
global loop
tapo_login()
loop.run_until_complete(async_tapo_on())
loop.run_until_complete(asyncio.sleep(0.1))
tapo_logout()
def tapo_off():
global loop
tapo_login()
loop.run_until_complete(async_tapo_off())
loop.run_until_complete(asyncio.sleep(0.1))
tapo_logout()
def tapo_info():
global loop
tapo_login()
loop.run_until_complete(async_tapo_info())
loop.run_until_complete(asyncio.sleep(0.1))
tapo_logout()
def unwrap_object(any_object, level=0):
output = ""
object_type = type(any_object)
if str(object_type) == "<class 'plugp100.common.functional.tri.Success'>":
output+='{"Success": '
sub_object=any_object.__dict__
output+=unwrap_object(sub_object, level+1)
output+='}'
return output
elif str(object_type) == "<class 'plugp100.responses.device_state.PlugDeviceState'>":
output+='{"PlugDeviceState": '
sub_object=any_object.__dict__
output+=unwrap_object(sub_object, level+1)
output+='}'
return output
elif str(object_type) == "<class 'plugp100.responses.device_state.DeviceInfo'>":
output+='{"DeviceInfo": '
sub_object=any_object.__dict__
output+=unwrap_object(sub_object, level+1)
output+='}'
return output
elif str(object_type) == "<class 'tuple'>":
output+='{'
try:
items = any_object.items()
except (AttributeError, TypeError):
output+=unwrap_object(any_object, level+1)
pass
else:
item_count = len(items)
count = 0
for item in items:
output+=unwrap_object(item, level+1)
count+=1
if ( count < item_count ):
output+=', '
output+='}'
return output
elif str(object_type) == "<class 'dict'>":
#output+='{'
ok = True
try:
items = any_object.items()
except (AttributeError, TypeError):
output+=unwrap_object(any_object, level+1)
ok = False
pass
if ok:
output+='{'
keys_count = len(items)
key_count = 0
for key, values in items:
#output+='{"' + key + '": '
output+='"' + key + '": '
output+=unwrap_object(values, level+1)
#ok = True
#try:
# values_count = len(values)
#except (AttributeError, TypeError):
# print("DICT KEY ERROR")
# output+=unwrap_object(values, level+1)
# ok = False
# pass
#if ok:
# print(key, values)
# value_count = 0
# for value in values:
# output+=unwrap_object(value, level+1)
# value_count+=1
# if ( value_count < values_count ):
# output+=', '
key_count+=1
if ( key_count < keys_count ):
output+=', '
#output+='}'
output+='}'
#output+='}'
return output
elif str(object_type) == "<class 'str'>":
return '"'+any_object+'"'
elif str(object_type) == "<class 'bool'>":
if any_object:
return "1"
else:
return "0"
elif str(object_type) == "<type 'NoneType'>":
return '"None"'
else:
return '"'+str(any_object)+'"'
if __name__ == "__main__":
if len(sys.argv) == 5:
username = sys.argv[2]
password = sys.argv[3]
ip = sys.argv[4]
if sys.argv[1].lower() == "info":
tapo_info()
if deviceInfo.is_failure():
print(deviceInfo)
else:
result=unwrap_object(deviceInfo)
print(json.dumps(json.loads(result)["Success"]["value"]))
elif sys.argv[1].lower() == "on":
tapo_on()
elif sys.argv[1].lower() == "off":
tapo_off()
else:
print('{"error_code": -1}')
elif len(sys.argv) == 2:
username = os.environ.get('TAPO_USERNAME', None)
password = os.environ.get('TAPO_PASSWORD', None)
ip = os.environ.get('TAPO_DEVICEIP', None)
if username is None or password is None or ip is None:
print('{"error_code": -2}')
elif sys.argv[1].lower() == "info":
tapo_info()
if deviceInfo.is_failure():
print(deviceInfo)
else:
result=unwrap_object(deviceInfo)
print(json.dumps(json.loads(result)["Success"]["value"]))
elif sys.argv[1].lower() == "on":
tapo_on()
elif sys.argv[1].lower() == "off":
tapo_off()
else:
print('{"error_code": -1}')
else:
print('{"error_code": -2}')
else:
print('{"error_code": -3}')