| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
This is an example script that can be used to set on and off timers based on the sunrise/sunset times. |
| 5 |
|
| 6 |
Specifically, it will set times on an outside porch light |
| 7 |
to turn on at dusk and off at dawn. It will set the timers for |
| 8 |
inside light to turn on at sunset, and off at a fixed time. |
| 9 |
|
| 10 |
A script like this is best used with an /etc/crontab entry that might |
| 11 |
run every day or every few days. For example: |
| 12 |
----------------- |
| 13 |
|
| 14 |
# Sync up the bulb clocks a few times a day, in case of manual power toggles |
| 15 |
00 3,12,17,22 * * * username /path/to/scripts/flux_led.py -Ss --setclock |
| 16 |
|
| 17 |
# Set the sun timers everyday at 3am |
| 18 |
00 3 * * * username /path/to/scripts/sun_timers.py |
| 19 |
|
| 20 |
|
| 21 |
----------------- |
| 22 |
|
| 23 |
The python file with the Flux LED wrapper classes should live in |
| 24 |
the same folder as this script |
| 25 |
""" |
| 26 |
|
| 27 |
import datetime |
| 28 |
import os |
| 29 |
import sys |
| 30 |
import syslog |
| 31 |
|
| 32 |
from flux_led import BulbScanner, LedTimer, WifiLedBulb |
| 33 |
|
| 34 |
try: |
| 35 |
from astral import Astral |
| 36 |
except ImportError: |
| 37 |
print("Error: Need to install python package: astral") |
| 38 |
sys.exit(-1) |
| 39 |
|
| 40 |
|
| 41 |
this_folder = os.path.dirname(os.path.realpath(__file__)) |
| 42 |
sys.path.append(this_folder) |
| 43 |
|
| 44 |
debug = False |
| 45 |
|
| 46 |
|
| 47 |
def main(): |
| 48 |
syslog.openlog(sys.argv[0]) |
| 49 |
|
| 50 |
|
| 51 |
location = "San Diego" |
| 52 |
|
| 53 |
|
| 54 |
a = Astral() |
| 55 |
a.solar_depression = "civil" |
| 56 |
city = a[location] |
| 57 |
timezone = city.timezone |
| 58 |
sun = city.sun(date=datetime.datetime.now(), local=True) |
| 59 |
|
| 60 |
if debug: |
| 61 |
print(f"Information for {location}/{city.region}\n") |
| 62 |
print(f"Timezone: {timezone}") |
| 63 |
|
| 64 |
print(f"Latitude: {city.latitude:.02f}; Longitude: {city.longitude:.02f}\n") |
| 65 |
|
| 66 |
print("Dawn: {}".format(sun["dawn"])) |
| 67 |
print("Sunrise: {}".format(sun["sunrise"])) |
| 68 |
print("Noon: {}".format(sun["noon"])) |
| 69 |
print("Sunset: {}".format(sun["sunset"])) |
| 70 |
print("Dusk: {}".format(sun["dusk"])) |
| 71 |
|
| 72 |
|
| 73 |
scanner = BulbScanner() |
| 74 |
scanner.scan(timeout=4) |
| 75 |
|
| 76 |
|
| 77 |
porch_info = scanner.getBulbInfoByID("ACCF235FFFEE") |
| 78 |
livingroom_info = scanner.getBulbInfoByID("ACCF235FFFAA") |
| 79 |
|
| 80 |
if porch_info: |
| 81 |
bulb = WifiLedBulb(porch_info["ipaddr"]) |
| 82 |
bulb.refreshState() |
| 83 |
|
| 84 |
timers = bulb.getTimers() |
| 85 |
|
| 86 |
|
| 87 |
syslog.syslog( |
| 88 |
syslog.LOG_ALERT, |
| 89 |
"Setting porch light to turn on at {}:{:02d}".format( |
| 90 |
sun["dusk"].hour, sun["dusk"].minute |
| 91 |
), |
| 92 |
) |
| 93 |
dusk_timer = LedTimer() |
| 94 |
dusk_timer.setActive(True) |
| 95 |
dusk_timer.setRepeatMask(LedTimer.Everyday) |
| 96 |
dusk_timer.setModeWarmWhite(35) |
| 97 |
dusk_timer.setTime(sun["dusk"].hour, sun["dusk"].minute) |
| 98 |
timers[0] = dusk_timer |
| 99 |
|
| 100 |
|
| 101 |
syslog.syslog( |
| 102 |
syslog.LOG_ALERT, |
| 103 |
"Setting porch light to turn off at {}:{:02d}".format( |
| 104 |
sun["dawn"].hour, sun["dawn"].minute |
| 105 |
), |
| 106 |
) |
| 107 |
dawn_timer = LedTimer() |
| 108 |
dawn_timer.setActive(True) |
| 109 |
dawn_timer.setRepeatMask(LedTimer.Everyday) |
| 110 |
dawn_timer.setModeTurnOff() |
| 111 |
dawn_timer.setTime(sun["dawn"].hour, sun["dawn"].minute) |
| 112 |
timers[1] = dawn_timer |
| 113 |
|
| 114 |
bulb.sendTimers(timers) |
| 115 |
|
| 116 |
else: |
| 117 |
print("Can't find porch bulb") |
| 118 |
|
| 119 |
if livingroom_info: |
| 120 |
bulb = WifiLedBulb(livingroom_info["ipaddr"]) |
| 121 |
bulb.refreshState() |
| 122 |
|
| 123 |
timers = bulb.getTimers() |
| 124 |
|
| 125 |
|
| 126 |
syslog.syslog( |
| 127 |
syslog.LOG_ALERT, |
| 128 |
"Setting LR light to turn on at {}:{:02d}".format( |
| 129 |
sun["sunset"].hour, sun["sunset"].minute |
| 130 |
), |
| 131 |
) |
| 132 |
sunset_timer = LedTimer() |
| 133 |
sunset_timer.setActive(True) |
| 134 |
sunset_timer.setRepeatMask(LedTimer.Everyday) |
| 135 |
sunset_timer.setModeWarmWhite(50) |
| 136 |
sunset_timer.setTime(sun["sunset"].hour, sun["sunset"].minute) |
| 137 |
timers[0] = sunset_timer |
| 138 |
|
| 139 |
|
| 140 |
off_timer = LedTimer() |
| 141 |
off_timer.setActive(True) |
| 142 |
off_timer.setRepeatMask(LedTimer.Everyday) |
| 143 |
off_timer.setModeTurnOff() |
| 144 |
off_timer.setTime(23, 30) |
| 145 |
timers[1] = off_timer |
| 146 |
|
| 147 |
bulb.sendTimers(timers) |
| 148 |
else: |
| 149 |
print("Can't find living room bulb") |
| 150 |
|
| 151 |
|
| 152 |
if __name__ == "__main__": |
| 153 |
main() |