-
Notifications
You must be signed in to change notification settings - Fork 0
/
tplink_test.py
118 lines (87 loc) · 2.84 KB
/
tplink_test.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
#
# TPLink smart plug device research
# Copyright © 2019 Dave Hocker
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# See the LICENSE file for more details.
#
from kasa import SmartDevice, SmartPlug, Discover
import asyncio
import logging
def enable_logging():
# Default overrides
logformat = '%(asctime)s, %(module)s, %(levelname)s, %(message)s'
logdateformat = '%Y-%m-%d %H:%M:%S'
# Logging level override
loglevel = logging.DEBUG
# Configure the root logger to cover all loggers
logger = logging.getLogger()
logger.setLevel(loglevel)
formatter = logging.Formatter(logformat, datefmt=logdateformat)
# Do we log to console?
# Covers the server and pyHS100 package
ch = logging.StreamHandler()
ch.setLevel(loglevel)
ch.setFormatter(formatter)
# logger.addHandler(ch)
logger.addHandler(ch)
# Controlled logging shutdown
def shutdown_logging():
logging.shutdown()
print("Logging shutdown")
def discover_single_by_class(loop, ip):
# plug = SmartPlug("192.168.1.181")
plug = SmartPlug(ip)
loop.run_until_complete(plug.update())
print("Plug:", plug.alias)
print(plug)
print()
print("Hardware")
print(plug.hw_info)
print()
print("Sys Info")
print(plug.sys_info)
print()
print("State:", plug.is_on)
def discover_single_by_address(loop, ip_address):
logger = logging.getLogger()
device = None
try:
device = loop.run_until_complete(Discover.discover_single(ip_address))
except Exception as ex:
logger.error("An exception occurred while discovering TPLink/Kasa device %s",
ip_address)
logger.error(str(ex))
if device is None:
return
print("Device:", device.alias)
print(device)
print()
print("Hardware")
print(device.hw_info)
print()
print("Sys Info")
print(device.sys_info)
print()
print("State:", device.is_on)
def discover_devices(loop):
# devices = loop.run_until_complete(Discover.discover(target="192.168.1.255"))
print("Discovering devices for 5 seconds...")
devices = loop.run_until_complete(Discover.discover(target="192.168.1.255", timeout=5, discovery_packets=3))
for ip, device in devices.items():
loop.run_until_complete(device.update())
print(f"{len(devices)} devices found")
for ip, device in devices.items():
print(ip, "=", device.alias, type(device))
if __name__ == '__main__':
enable_logging()
loop = asyncio.new_event_loop()
# discover_single_by_class(loop, "192.168.1.234")
discover_single_by_address(loop, "192.168.1.183")
# discover_devices(loop)
loop.stop()
loop.close()
shutdown_logging()