Skip to content

Instantly share code, notes, and snippets.

Created December 15, 2011 00:32
Show Gist options
  • Save anonymous/1479282 to your computer and use it in GitHub Desktop.
Save anonymous/1479282 to your computer and use it in GitHub Desktop.
pylights/astral insteon light scheduler
#!/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