-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecora-cli.py
90 lines (74 loc) · 3.73 KB
/
Decora-cli.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
#!/usr/bin/python
from decora_wifi import DecoraWiFiSession
from decora_wifi.person import Person
from decora_wifi.residential_account import ResidentialAccount
from decora_wifi.residence import Residence
import sys
import time
req_switch_ids = False
execute_ON_OFF = False
if len(sys.argv) < 4:
print('\nUsage: {} [email] [pswd] [id#:ON|OFF|0-100] <[id#:ON|OFF|0-100]> <etc.>'.format(sys.argv[0]))
print( 'Usage: {} [email] [pswd] [id#:?] <[id#:?]> <etc.> ... list current status of switch(es)'.format(sys.argv[0]))
print( 'Usage: {} [email] [pswd] ? ... list all switch ids\n'.format(sys.argv[0]))
sys.exit(1)
decora_email = sys.argv[1]
decora_pass = sys.argv[2]
if sys.argv[3] == '?': # request a list of all switches with switch id's
req_switch_ids = True
else: # execute ON|OFF|0-100 and/or get status
execute_ON_OFF = True
session = DecoraWiFiSession()
session.login(decora_email, decora_pass)
perms = session.user.get_residential_permissions()
# print('{} premissions'.format(len(perms)))
all_residences = []
for permission in perms:
# print("Permission: {}".format(permission))
if permission.residentialAccountId is not None:
acct = ResidentialAccount(session, permission.residentialAccountId)
for res in acct.get_residences():
# print("Residence: {}".format(res))
all_residences.append(res)
elif permission.residenceId is not None:
res = Residence(session, permission.residenceId)
# print("Residence: {}".format(res))
all_residences.append(res)
all_switches = []
for residence in all_residences: # only tested with one residence
if execute_ON_OFF == True: # execute ON|OFF|0-100 for each switch designated on command line
i = 4
while i <= len(sys.argv):
decora_pair = sys.argv[i-1].split(":")
decora_id = decora_pair[0]
decora_cmd = decora_pair[1]
switch_num = residence.find_by_id_iot_switches(decora_id)
attribs = {}
if decora_cmd == 'ON':
attribs['power'] = 'ON'
print("{}. #{} {} ({})".format(i-3,switch_num.id,decora_cmd,switch_num.name))
switch_num.update_attributes(attribs) # perform command on the designated switch
elif decora_cmd == 'OFF':
attribs['power'] = 'OFF'
print("{}. #{} {} ({})".format(i-3,switch_num.id,decora_cmd,switch_num.name))
switch_num.update_attributes(attribs) # perform command on the designated switch
elif decora_cmd == '?':
print("{} ID#{} ({})".format(switch_num.power,switch_num.id,switch_num.name))
else:
decora_bright = int(decora_cmd)
attribs['brightness'] = decora_bright
print("{}. #{} {}% ({})".format(i-3,switch_num.id,decora_cmd,switch_num.name))
switch_num.update_attributes(attribs) # perform command on the designated switch
time.sleep(.300) # sleep 300 milliseconds
i += 1
elif req_switch_ids == True: # print residence and switch imformation
print("Permission id#{} (Accountid#{})".format(permission.id, permission.residentialAccountId))
# print("Permission: {}".format(permission)) # prints all information
print("Residence id#{} ({})".format(res.id,res.name))
# print("Residence: {}".format(res)) # prints all information
i = 1
for switch in residence.get_iot_switches():
print("Switch{} id#{} ({}) status: {}".format(i,switch.id,switch.name,switch.power))
# print("Switch: {}".format(switch)) # prints all information
i += 1
Person.logout(session)