forked from Riebart/vivint.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemoswitch.py
More file actions
141 lines (116 loc) · 5.38 KB
/
demoswitch.py
File metadata and controls
141 lines (116 loc) · 5.38 KB
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
import time
import vivint
from credentials import vivint_email, vivint_pwd
from datetime import datetime
# Set up the connection to the cloud session
session = vivint.VivintCloudSession(vivint_email,
vivint_pwd)
# List all panels (sites) that this user account has access to
panels = session.get_panels()
# Sensors and Switches
switch_one_name = "Living Room Main Lights"
switch_two_name = "Dining Room Lights"
switch_one_default_level = 40
switch_two_default_level = 80
motion_duration_in_seconds = 5
sensor_one_inactivity_in_sec = 600
sensor_one_max_inactivity_in_sec = 7200
sensor_two_inactivity_in_sec = 60
sensor_two_max_inactivity_in_sec = 3600
sensor_one_name = "Living Room Motion Detector"
sensor_two_name = "Dining Room Motion Detector"
timestamp_conv_factor = 18000
multiswitches = panels[0].get_devices(device_type_set=[
vivint.VivintCloudSession.VivintDevice.DEVICE_TYPE_LIGHT_MODULE
])
sensors = panels[0].get_devices(device_type_set=[
vivint.VivintCloudSession.VivintDevice.DEVICE_TYPE_WIRELESS_SENSOR
])
while True:
# Update every panel. Doing this also updates devices that
# were spawned from those panels in-place, unless you set
# devices' receive_updates property is set to False.
panels[0].update_devices()
for multiswitch in multiswitches:
state = multiswitch.multi_swtich_state()
if state.get("name") == switch_one_name:
switch_one_state = state.get("val")
switch_one = multiswitch
if state.get("name") == switch_two_name:
switch_two_state = state.get("val")
switch_two = multiswitch
#reset time state
sensor_one_state = datetime.now()
sensor_two_state = datetime.now()
for sensor in sensors:
state = sensor.sensor_state()
if state.get("name") == sensor_one_name:
sensor_one_secs = ((sensor_one_state - state.get("activitytime")).total_seconds())
if sensor_one_secs < motion_duration_in_seconds:
sensor_one_state = state.get("activitytime")
switch_one_turn_on = True
print("sensor one sec: {}".format(sensor_one_secs))
else:
switch_one_turn_on = False
if sensor_one_secs > sensor_one_inactivity_in_sec:
switch_one_turn_off = True
print("sensor one sec: {}".format(sensor_one_secs))
else:
switch_one_turn_off = False
#hard shutoff
if sensor_one_secs > sensor_one_max_inactivity_in_sec:
switch_one_max_turn_off = True
else:
switch_one_max_turn_off = False
#if motion is detected within motion_duration(typically 5 seconds), turn on light switch to default setting
if switch_one_turn_on == True and switch_one_state == 0:
switch_one.set_switch(switch_one_default_level)
print("turn on and sleep")
time.sleep(10)
print("turn off sleep has ended")
#turn off light switch if sensor has been inactive for inactivity timeout, only switch light off when it's not set to default
elif switch_one_turn_off == True and switch_one_state != 0 and switch_one_state != switch_one_default_level:
print("turn off and sleep")
switch_one.set_switch(0)
time.sleep(10)
print("turn off sleep has ended")
#hard shutoff
elif switch_one_max_turn_off == True and switch_one_state != 0:
print("turn off and sleep")
switch_one.set_switch(0)
time.sleep(10)
print("turn off sleep has ended")
if state.get("name") == sensor_two_name:
print(switch_two_state)
sensor_two_secs = ((sensor_two_state - state.get("activitytime")).total_seconds())
if sensor_two_secs < motion_duration_in_seconds:
sensor_two_state = state.get("activitytime")
switch_two_turn_on = True
print("sensor two sec: {}".format(sensor_two_secs))
else:
switch_two_turn_on = False
if sensor_two_secs > sensor_two_inactivity_in_sec:
switch_two_turn_off = True
print("sensor two sec: {}".format(sensor_two_secs))
else:
switch_two_turn_off = False
#hard shutoff
if sensor_two_secs > sensor_two_max_inactivity_in_sec:
switch_two_max_turn_off = True
else:
switch_two_max_turn_off = False
#if motion is detected within motion_duration(typically 5 seconds), turn on light switch to default setting
if switch_two_turn_on == True and switch_two_state == 0:
switch_two.set_switch(switch_two_default_level)
time.sleep(10)
#turn off light switch if sensor has been inactive for inactivity timeout
elif switch_two_turn_off == True and switch_two_state != 0:
switch_two.set_switch(0)
time.sleep(10)
#hard shutoff
elif switch_two_max_turn_off == True and switch_two_state != 0:
print("turn off and sleep")
switch_one.set_switch(0)
time.sleep(10)
print("turn off sleep has ended")
time.sleep(10)