Skip to content

Instantly share code, notes, and snippets.

@shreram
Created December 22, 2017 02:41
Show Gist options
  • Save shreram/0cbd1a6ddc488ea06c791cf5d7277111 to your computer and use it in GitHub Desktop.
Save shreram/0cbd1a6ddc488ea06c791cf5d7277111 to your computer and use it in GitHub Desktop.
"""Define automations for evening-time lighting."""
import appdaemon.appapi as appapi
import time
from datetime import datetime, timedelta
class TurnOnLightsAtSunset(appapi.AppDaemon):
"""Define a class to represent the app."""
def initialize(self):
"""Initialize."""
self.log('Next sunset at : ' + str(self.sunset()))
# will return the instantiated object so we can access those functions
self.utils = self.get_app('utils')
# enable only to get addl info
# self.log(str(self.get_scheduler_entries()))
if "triggermins" in self.args:
triggertime = self.args["triggermins"]
offsetclock = self.sunset() + timedelta(minutes=triggertime)
self.log(' Trigger time : ' + offsetclock.time().strftime('%H:%M:%S'))
self.run_daily(self.run_daily_callback, offsetclock.time())
# Run at sunrise
self.run_at_sunrise(self.sunrise_cb)
# Run at sunset
self.run_at_sunset(self.sunset_cb)
# test this
# self.run_at_sunset(self.sun, offset = datetime.timedelta(minutes = self.args["triggermins"]).total_seconds(), "Sunset offsetclock mins"
def run_daily_callback(self, kwargs):
self.log('TurnOnLightsAtSunset: Daily Callback Function triggered : ' + self.name)
msg = ""
for entity_id in self.args["entities"]:
device, entity = self.split_entity(entity_id)
# self.log("EntityId : {} - Device : {} - Entity: {}".format(entity_id, device, entity))
if device != 'scene':
# if the lights are on then call turn_off
if self.get_state(entity_id).lower() == 'off':
self.turn_on(entity_id)
if len(self.args["entities"]) > 1:
msg += self.friendly_name(entity_id) + ", "
else:
msg += self.friendly_name(entity_id)
# self.log(msg)
else:
self.log("IGNORE - EntityId : {} - State : {}".format(self.friendly_name(entity_id), self.get_state(entity_id).lower()))
else:
# scene returns 'scening' for get_state
self.turn_on(entity_id)
msg += self.friendly_name(entity_id)
if msg != "":
# self.log(msg)
msg += " turned ON "
if self.sun_up():
msg += "before"
else:
msg += "after"
msg += " sunset "
self.log(msg)
self.set_state("sensor.appd_notify_message", state=msg, attributes={"ifttt":False, "frontend":True, "announce":False})
# if ad_dark_house_lights app flag turned off manually via GUI then next day at sunrise turn ON that flag again
def sunrise_cb(self, kwargs):
self.log("Sunrise Triggered")
# get current status
dh_trigger = self.get_state("input_boolean.ad_dark_house_lights")
self.log("Current ad_dark_house_lights trigger status = {}".format(self.dh_trigger))
if dh_trigger == "off":
# turn on the app
self.turn_on("input_boolean.ad_dark_house_lights")
# self.log("dark_house_lights trigger turned ON")
msg = "Good morning. AppDaemon dark house flag turned ON at sunrise\n"
self.log(msg)
self.set_state("sensor.appd_notify_message", state=msg, attributes={"ifttt":False, "frontend":True, "announce":False})
# Turn off is_it_cloudy_with_chance_of_meatballs app flag at sunset
def sunset_cb(self, kwargs):
self.log("Sunset Triggered")
# get current mode
isit_trigger = self.get_state("input_boolean.is_it_cloudy_with_chance_of_meatballs")
self.log("Current is_it_cloudy_with_chance_of_meatballs trigger status = {}".format(self.isit_trigger))
if isit_trigger == "on":
# turn on the app
self.turn_off("input_boolean.is_it_cloudy_with_chance_of_meatballs")
# self.log("dark_house_lights trigger turned ON")
msg = "Good evening. Is It Clody With A Change flag turned OFF at sunset\n"
self.log(msg)
self.set_state("sensor.appd_notify_message", state=msg, attributes={"ifttt":False, "frontend":True, "announce":False})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment