-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_smart_plug.py
61 lines (43 loc) · 1.77 KB
/
test_smart_plug.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
import sys
import os
import logging
script_loc_dir = os.path.split(os.path.realpath(__file__))[0]
if script_loc_dir not in sys.path: sys.path.append(script_loc_dir)
from scripts.SmartPlugController import SmartPlugController
from scripts.arg_parsing import get_args_from_config_in_sysargs
from scripts.functions import get_plug_password, get_log_format
logger = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(get_log_format())
logger.addHandler(handler)
logger.setLevel(logging.INFO)
def main():
config = get_args_from_config_in_sysargs()
args = sys.argv[1:]
if len(args) == 0 or args[0] in ('-h', '--help'):
print("<'on' or 'off'> [--no-tplink] [--no-pykasa]")
return 1
plug_ip = config.plugIP
plug_state = args[0]
if plug_state not in [ 'on', 'off' ]:
print('Plug state must either be "on" or "off"')
return 1
plug_on = (plug_state == 'on')
use_tplinkcmd = not ( '--no-tplink' in args )
use_pykasa = not ( '--no-pykasa' in args)
plc= SmartPlugController(
config.plugIP,
config.plugName,
config.wifi,
tplink_creds=(config.plugAccUsername, get_plug_password(config.plugAccUsername)),
TPLinkAvail=get_plug_password(config.plugAccUsername) is not None,
logger=logger)
print("Setting Kasa SmartPlug '{}' to '{}'".format(plug_ip, plug_state))
s = None
if use_pykasa: s = '{}, {}'.format(s, 'Python') if s is not None else 'Python'
if use_tplinkcmd: s = '{}, {}'.format(s, 'TPLinkCmd.exe') if s is not None else 'TPLinkCmd.exe'
print('Can use: {}'.format(s))
plc.set_plug(on=plug_on, off=(not plug_on), use_tplink=use_tplinkcmd, use_pykasa=use_pykasa)
return 0
if __name__ == '__main__':
sys.exit(main())