forked from Danielhiversen/flux_led
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sun_timers_example.py
executable file
·139 lines (106 loc) · 3.93 KB
/
sun_timers_example.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
"""
This is an example script that can be used to set on and off timers based
on the sunrise/sunset times.
Specifically, it will set times on an outside porch light
to turn on at dusk and off at dawn. It will set the timers for
inside light to turn on at sunset, and off at a fixed time.
A script like this is best used with an /etc/crontab entry that might
run every day or every few days. For example:
-----------------
# Sync up the bulb clocks a few times a day, in case of manual power toggles
00 3,12,17,22 * * * username /path/to/scripts/flux_led.py -Ss --setclock
# Set the sun timers everyday at 3am
00 3 * * * username /path/to/scripts/sun_timers.py
-----------------
The python file with the Flux LED wrapper classes should live in
the same folder as this script
"""
import datetime
import os
import sys
import syslog
try:
from astral import Astral
except:
print "Error: Need to install python package: astral"
sys.exit(-1)
this_folder = os.path.dirname(os.path.realpath(__file__))
sys.path.append(this_folder)
from flux_led import WifiLedBulb, BulbScanner, LedTimer
debug = False
def main():
syslog.openlog(sys.argv[0])
# Change location to nearest city.
location = 'San Diego'
# Get the local sunset/sunrise times
a = Astral()
a.solar_depression = 'civil'
city = a[location]
timezone = city.timezone
sun = city.sun(date=datetime.datetime.now(), local=True)
if debug:
print 'Information for {}/{}\n'.format(location, city.region)
print 'Timezone: {}'.format(timezone)
print 'Latitude: {:.02f}; Longitude: {:.02f}\n'.format(city.latitude, city.longitude)
print('Dawn: {}'.format(sun['dawn']))
print('Sunrise: {}'.format(sun['sunrise']))
print('Noon: {}'.format(sun['noon']))
print('Sunset: {}'.format(sun['sunset']))
print('Dusk: {}'.format(sun['dusk']))
# Find the bulbs on the LAN
scanner = BulbScanner()
scanner.scan(timeout=4)
# Specific ID/MAC of the bulbs to set
porch_info = scanner.getBulbInfoByID('ACCF235FFFEE')
livingroom_info = scanner.getBulbInfoByID('ACCF235FFFAA')
if porch_info:
bulb = WifiLedBulb(porch_info['ipaddr'])
bulb.refreshState()
timers = bulb.getTimers()
# Set the porch bulb to turn on at dusk using timer idx 0
syslog.syslog(syslog.LOG_ALERT,
"Setting porch light to turn on at {}:{:02d}".format(sun['dusk'].hour, sun['dusk'].minute))
dusk_timer = LedTimer()
dusk_timer.setActive(True)
dusk_timer.setRepeatMask(LedTimer.Everyday)
dusk_timer.setModeWarmWhite(35)
dusk_timer.setTime(sun['dusk'].hour, sun['dusk'].minute)
timers[0] = dusk_timer
# Set the porch bulb to turn off at dawn using timer idx 1
syslog.syslog(syslog.LOG_ALERT,
"Setting porch light to turn off at {}:{:02d}".format(sun['dawn'].hour, sun['dawn'].minute))
dawn_timer = LedTimer()
dawn_timer.setActive(True)
dawn_timer.setRepeatMask(LedTimer.Everyday)
dawn_timer.setModeTurnOff()
dawn_timer.setTime(sun['dawn'].hour, sun['dawn'].minute)
timers[1] = dawn_timer
bulb.sendTimers(timers)
else:
print "Can't find porch bulb"
if livingroom_info:
bulb = WifiLedBulb(livingroom_info['ipaddr'])
bulb.refreshState()
timers = bulb.getTimers()
# Set the living room bulb to turn on at sunset using timer idx 0
syslog.syslog(syslog.LOG_ALERT,
"Setting LR light to turn on at {}:{:02d}".format(sun['sunset'].hour, sun['sunset'].minute))
sunset_timer = LedTimer()
sunset_timer.setActive(True)
sunset_timer.setRepeatMask(LedTimer.Everyday)
sunset_timer.setModeWarmWhite(50)
sunset_timer.setTime(sun['sunset'].hour, sun['sunset'].minute)
timers[0] = sunset_timer
# Set the living room bulb to turn off at a fixed time
off_timer = LedTimer()
off_timer.setActive(True)
off_timer.setRepeatMask(LedTimer.Everyday)
off_timer.setModeTurnOff()
off_timer.setTime(23,30)
timers[1] = off_timer
bulb.sendTimers(timers)
else:
print "Can't find living room bulb"
if __name__ == '__main__':
main()