-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgetconfig.py
56 lines (44 loc) · 1.56 KB
/
getconfig.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
import sys
from io import StringIO
from custom_components.cozylife.tcp_client import tcp_client
from ipaddress import ip_address
def ips(start, end):
'''Return IPs in IPv4 range, inclusive. from stackoverflow'''
start_int = int(ip_address(start).packed.hex(), 16)
end_int = int(ip_address(end).packed.hex(), 16)
return [ip_address(ip).exploded for ip in range(start_int, end_int + 1)]
start = '192.168.1.193'
end = '192.168.1.254'
if len(sys.argv) == 2:
end = sys.argv[1]
start = sys.argv[1]
if len(sys.argv) > 2:
end = sys.argv[2]
start = sys.argv[1]
probelist = ips(start, end)
print("IP scan from {0}, end with {1}".format(probelist[0], probelist[-1]))
lights_buf = StringIO()
switches_buf = StringIO()
for ip in probelist:
a = tcp_client(ip, timeout=0.1)
a._initSocket()
if a._connect:
device_info = a._device_info()
device_info_str = f' - ip: {ip}\n'
device_info_str += f' did: {a._device_id}\n'
device_info_str += f' pid: {a._pid}\n'
device_info_str += f' dmn: {a._device_model_name}\n'
device_info_str += f' dpid: {a._dpid}\n'
# device_info_str += f' device_type: {a._device_type_code}\n'
if a._device_type_code == '01':
lights_buf.write(device_info_str)
elif a._device_type_code == '00':
switches_buf.write(device_info_str)
print(f'light:')
print(f'- platform: cozylife')
print(f' lights:')
print(lights_buf.getvalue())
print(f'switch:')
print(f'- platform: cozylife')
print(f' switches:')
print(switches_buf.getvalue())