-
-
Save bfritz/1479287 to your computer and use it in GitHub Desktop.
pylights/astral insteon light scheduler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/home/brad/ve/lights/bin/python | |
import pylights, sched, time | |
from astral import Astral | |
from datetime import datetime | |
from dateutil.relativedelta import relativedelta | |
from pytz import timezone | |
city = 'Indianapolis' | |
tz = timezone('America/Indianapolis') | |
depression_type = 'civil' | |
pylights.device_cfg_filename = '/home/brad/bin/insteon/devices.xml' | |
p = pylights.plm('/dev/ttyUSB0') | |
def safe_set_level(device, level): | |
try: | |
p.setLevel(device, level) | |
except IOError as (errno, strerror): | |
print "I/O error({0}): {1}".format(errno, strerror) | |
def lights_on(): | |
safe_set_level('porch', 100) | |
def lights_off(): | |
safe_set_level('porch', 0) | |
def seconds_until(dt): | |
delta = dt - datetime.now(tz) | |
seconds = delta.days * 86400 + delta.seconds | |
return seconds if seconds > 0 else seconds + 86400 | |
s = sched.scheduler(time.time, time.sleep) | |
a = Astral() | |
a.solar_depression = depression_type | |
c = a[city] | |
sun = c.sun(datetime.now(), local=True) | |
s.enter(seconds_until(sun['dawn']), 1, lights_off, ()) | |
s.enter(seconds_until(sun['dusk']), 1, lights_on, ()) | |
s.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment