-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopat_p100.py
executable file
·156 lines (110 loc) · 4.59 KB
/
opat_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
#!/usr/bin/python3
import argparse
import sys
import transport
from bluez import Adapter
from http_p100 import HttpTransport, RequestSetDeviceInfo
TAPO_WRITE_CHAR_NAME = 'service000c/char0010'
TAPO_READ_CHAR_NAME = 'service000c/char000d'
TAPO_SERVICE_UUID = '8641'
def create_transport(device):
char_write = device.get_characteristic(TAPO_WRITE_CHAR_NAME)
char_read = device.get_characteristic(TAPO_READ_CHAR_NAME)
return transport.Transport(char_read, char_write)
def scan(args, adapter):
print('Scanning...')
adapter.discover(5)
devices = adapter.get_devices_by_uuid(TAPO_SERVICE_UUID)
if len(devices) == 0:
print("No devices found.")
return
print('Found:')
for d in devices:
print('\t' + d.get_address())
def info(args, adapter):
device = adapter.get_device_by_addr(args.baddr)
device.connect()
t = create_transport(device)
req = transport.RequestQuickSetup()
resp = t.request(req)
print('Type: ' + resp.get_type())
print('Model: ' + resp.get_model())
print('Components:')
for c in resp.get_components():
print('\t' + c['id'].ljust(16) + ' v' + str(c['ver_code']))
device.disconnect()
def wifi_scan(args, adapter):
device = adapter.get_device_by_addr(args.baddr)
device.connect()
t = create_transport(device)
req = transport.RequestWifiScanInfo()
resp = t.request(req)
ap_list = resp.get_ap_list()
index = resp.get_next_index()
while not resp.is_complete():
req = transport.RequestWifiScanInfo(index)
resp = t.request(req)
ap_list += resp.get_ap_list()
index = resp.get_next_index()
print('SSID'.ljust(32) + 'Key type'.ljust(16) + 'Signal level')
for w in ap_list:
print(w['ssid'].ljust(32) + w['key_type'].ljust(16) + str(w['signal_level']))
device.disconnect()
def wifi_set(args, adapter):
device = adapter.get_device_by_addr(args.baddr)
device.connect()
t = create_transport(device)
req = transport.RequestSetQsInfo(wifi_ssid=args.ssid, wifi_key=args.key,
web_user=args.email, web_password=args.password)
resp = t.request(req)
if resp.has_error():
print('Setting WIFI failed.')
return
print(resp.get_result())
device.disconnect()
def switch(args, adapter):
t = HttpTransport(args.ipaddr, args.email, args.password)
if args.onoff == 'on':
enable = True
elif args.onoff == 'off':
enable = False
else:
print(f'Argument onoff must be \'on\' or \'off\' but was \'{args.onoff}\'.')
return -1
req = RequestSetDeviceInfo(enable)
t.request(req)
def print_usage(parser: argparse.ArgumentParser):
parser.print_usage()
def main():
parser = argparse.ArgumentParser(description='Setup Tapo smart devices via bluetooth.')
subparsers = parser.add_subparsers(help='Available commands')
parser_scan = subparsers.add_parser('scan', help='scan for devices')
parser_scan.set_defaults(func=scan)
parser_info = subparsers.add_parser('info', help='info for device')
parser_info.add_argument('baddr', type=str, help='BT address of device')
parser_info.set_defaults(func=info)
parser_wifi_scan = subparsers.add_parser('wifi_scan', help='scan WiFis')
parser_wifi_scan.add_argument('baddr', type=str, help='BT address of device')
parser_wifi_scan.set_defaults(func=wifi_scan)
parser_wifi_set = subparsers.add_parser('wifi_setup', help='wifi settings')
parser_wifi_set.add_argument('baddr', type=str, help='BT address of device')
parser_wifi_set.add_argument('ssid', type=str, help='SSID of the WiFi network')
parser_wifi_set.add_argument('key', type=str, help='Key of the WiFi network (!SENT UNENCRYPTED!)')
parser_wifi_set.add_argument('email', type=str, default='edcrfv@edcujm.com', help='email')
parser_wifi_set.add_argument('password', type=str, default='Passw0rd', help='password')
parser_wifi_set.set_defaults(func=wifi_set)
parser_switch = subparsers.add_parser('switch', help='Turn the plug on or off')
parser_switch.add_argument('ipaddr', type=str, help='IP address of device')
parser_switch.add_argument('email', type=str, help='email')
parser_switch.add_argument('password', type=str, help='password')
parser_switch.add_argument('onoff', type=str, help='Must be \'on\' or \'off\'.')
parser_switch.set_defaults(func=switch)
if len(sys.argv) == 1:
print_usage(parser)
exit(-1)
args = parser.parse_args()
adapter = Adapter()
adapter.set_power(1)
args.func(args, adapter)
adapter.set_power(0)
main()