From b8b7e6676f27721cb18eee2769257ffca570d649 Mon Sep 17 00:00:00 2001 From: Andrew Cockburn Date: Sat, 16 Jul 2016 16:30:10 -0400 Subject: [PATCH] Update conf examples --- conf/appdaemon.cfg | 34 +++++++++++++++--- conf/apps/__init__.py | 1 + conf/apps/log.py | 5 +-- conf/apps/mirror_light.py | 28 ++++++++------- conf/apps/motion_lights.py | 25 ++++++++++++++ conf/apps/schedule.py | 70 ++++++++++++++++++-------------------- conf/apps/service.py | 14 ++++---- conf/apps/state.py | 64 +++++++++++++++++++++++++--------- conf/apps/sun.py | 34 +++++++++++------- conf/apps/thread_starve.py | 15 ++++++++ conf/apps/trackers.py | 38 +++++++++++++++++++++ conf/apps/two_class.py | 21 ++++++++++++ 12 files changed, 257 insertions(+), 92 deletions(-) create mode 100755 conf/apps/motion_lights.py create mode 100755 conf/apps/thread_starve.py create mode 100755 conf/apps/trackers.py create mode 100755 conf/apps/two_class.py diff --git a/conf/appdaemon.cfg b/conf/appdaemon.cfg index 1b8fd0ed3..41db607d4 100755 --- a/conf/appdaemon.cfg +++ b/conf/appdaemon.cfg @@ -1,9 +1,9 @@ [appdaemon] -ha_url = +ha_url = ha_key = -logfile = /etc/appdaemon/appdaemon.log -errorfile = /etc/appdaemon/error.log -app_dir = /srv/hass/src/appdaemon/apps +logfile = /srv/hass/appdaemon_test/logs/appdaemon.log +errorfile = /srv/hass/appdaemon_test/logs/error.log +app_dir = /srv/hass/appdaemon_test/conf/apps threads = 10 # Apps [state] @@ -25,6 +25,30 @@ class = Sun [service] module = service class = Service - +[motion_lights] +module = motion_lights +class = MotionLights +[trackers] +module = trackers +class = Trackers +[class_1] +module = two_class +class = Class1 +param1 = This is class 1 +[class_1a] +module = two_class +class = Class1 +param1 = This is class 1a +[class_1b] +module = two_class +class = Class1 +param1 = This is class 1b +[class_2] +module = two_class +class = Class2 +param1 = This is class 2 +[thread_starve] +module = thread_starve +class = ThreadStarve diff --git a/conf/apps/__init__.py b/conf/apps/__init__.py index e69de29bb..89f2d6223 100644 --- a/conf/apps/__init__.py +++ b/conf/apps/__init__.py @@ -0,0 +1 @@ +# Placeholder - required to all app module loading \ No newline at end of file diff --git a/conf/apps/log.py b/conf/apps/log.py index d222eece4..aa4a99ef9 100755 --- a/conf/apps/log.py +++ b/conf/apps/log.py @@ -1,8 +1,9 @@ -import homeassistant as ha import appapi class Log(appapi.APPDaemon): def initialize(self): return - self.logger.info("Log Test: Parameter is {}".format(self.args["param1"])) + self.log("Log Test: Parameter is {}".format(self.args["param1"])) + self.error("Error Test") + diff --git a/conf/apps/mirror_light.py b/conf/apps/mirror_light.py index 71588a830..077193fa5 100755 --- a/conf/apps/mirror_light.py +++ b/conf/apps/mirror_light.py @@ -1,24 +1,26 @@ -import homeassistant as ha import appapi class MirrorLight(appapi.APPDaemon): def initialize(self): - ha.listen_state(self.name, self.light_changed, "light.andrew_bedside") + #return + self.listen_state(self.light_changed, "light.andrew_bedside") - state = ha.get_state("light.andrew_bedside", "state") - brightness = ha.get_state("light.andrew_bedside", "attributes.brightness") + state = self.get_state("light.andrew_bedside") + brightness = self.get_state("light.andrew_bedside", "brightness") - self.logger.info("MirrorLight: Current State is {}, current brightness is {}".format(state, brightness)) + self.log("MirrorLight: Current State is {}, current brightness is {}".format(state, brightness)) - if ha.get_state("light.andrew_bedside", "state") == "on": - ha.turn_on("light.office_lamp") + if state == "on": + self.call_service("light", "office_lamp", color_name = "red") + else: + self.turn_off("light.office_lamp") - def light_changed(self, entity, old_state, new_state): - self.logger.info("entity state changed, old: {}, new: {}".format(old_state["state"], new_state["state"])) + def light_changed(self, entity, attribute, old, new): + self.log("MirrorLight: entity {}.{} state changed, old: {}, new: {}".format(entity, attribute, old, new)) - if new_state["state"] == 'on': - #ha.turn_on("light.office_lamp") - ha.turn_on("light.office_lamp", color_name = "blue") + if new == 'on': + #self.turn_on("light.office_lamp") + print(self.call_service("light", "turn_on", entity_id = "light.office_lamp", color_name = "red")) else: - ha.turn_off("light.office_lamp") + print(self.turn_off("light.office_lamp")) diff --git a/conf/apps/motion_lights.py b/conf/apps/motion_lights.py new file mode 100755 index 000000000..852a5731a --- /dev/null +++ b/conf/apps/motion_lights.py @@ -0,0 +1,25 @@ +import appapi + +class MotionLights(appapi.APPDaemon): + + def initialize(self): + return + self.listen_state(self.motion, "binary_sensor.upstairs_sensor_28") + + def motion(self, entity, attribute, old, new): + if new == "on": + #if new == "on" and self.sun_state() == "below_horizon": + self.turn_on("light.office_1") + self.run_in(self.light_off, 60) + self.flashcount = 0 + self.run_in(self.flash_warning, 1) + + def light_off(self, args, kwargs): + self.turn_off("light.office_1") + + def flash_warning(self, args, kwargs): + self.toggle("light.office_2") + self.flashcount += 1 + if self.flashcount < 10: + self.run_in(self.flash_warning, 1) + diff --git a/conf/apps/schedule.py b/conf/apps/schedule.py index 1d9859c12..5ef0785f2 100755 --- a/conf/apps/schedule.py +++ b/conf/apps/schedule.py @@ -1,4 +1,3 @@ -import homeassistant as ha import appapi import datetime @@ -8,87 +7,86 @@ def initialize(self): return # Run a few timers and pass parameters - #ha.run_in(self.name, self.run_in, 5, 5, 10, title = "run_in5", test = "Another Param") - #ha.run_in(self.name, self.run_in, 10, 10, 15, title = "run_in10", test = "Another Param") - #ha.run_in(self.name, self.run_in, 15, 15, 20, title = "run_in15", test = "Another Param") - #ha.run_in(self.name, self.run_in, 20, 20, 25, title = "run_in20", test = "Another Param") + self.run_in(self.run_in_c, 5, 5, 10, title = "run_in5", test = "Another Param") + self.run_in(self.run_in_c, 10, 10, 15, title = "run_in10", test = "Another Param") + self.run_in(self.run_in_c, 15, 15, 20, title = "run_in15", test = "Another Param") + self.run_in(self.run_in_c, 20, 20, 25, title = "run_in20", test = "Another Param") # run_in with no params - #ha.run_in(self.name, self.run_innoargs, 5) + self.run_in(self.run_innoargs_c, 5) # Create a timer and then cancel it - #handle = ha.run_in(self.name, self.run_in, 15) - #ha.cancel_timer(self.name, handle) + handle = self.run_in(self.run_in_c, 15) + self.cancel_timer(handle) # Run at a specific time #runtime = datetime.time(11, 14, 0) - #runtime = (datetime.datetime.now() + datetime.timedelta(seconds=20)).time() - #handle = ha.run_once(self.name, self.run_once, runtime) + runtime = (datetime.datetime.now() + datetime.timedelta(seconds=20)).time() + handle = self.run_once(self.run_once_c, runtime) # Run every day at a specific time # e.g.time = datetime.time(12, 49, 0) - #runtime = (datetime.datetime.now() + datetime.timedelta(seconds=25)).time() - #ha.run_daily(self.name, self.run_daily, runtime) + runtime = (datetime.datetime.now() + datetime.timedelta(seconds=25)).time() + self.run_daily(self.run_daily_c, runtime) # Run Hourly starting 1 hour from now - #ha.run_hourly(self.name, self.run_everyhour, None) + self.run_hourly(self.run_hourly_c, None) # Run Hourly on the hour - #time = datetime.time(0, 0, 0) - #ha.run_hourly(self.name, self.run_everyhour, time) + time = datetime.time(0, 0, 0) + self.run_hourly(self.run_hourly_c, time) # Run Every Minute starting in 1 minute - #ha.run_minutely(self.name, self.run_minutely, None) + self.run_minutely(self.run_minutely_c, None) # Run Every Minute on the minute - #time = datetime.time(0, 0, 0) - #ha.run_minutely(self.name, self.run_minutely, time) + time = datetime.time(0, 0, 0) + self.run_minutely(self.run_minutely_c, time) # Run every 13 seconds starting in 10 seconds time - # time = datetime.datetime.now() + datetime.timedelta(seconds=10) - # ha.run_every(self.name, self.run_every, time, 10) + time = datetime.datetime.now() + datetime.timedelta(seconds=10) + self.run_every(self.run_every_c, time, 13) # Attempt some scheduler abuse ... #for x in range(1, 10000): - # handle = ha.run_in(self.name, self.run_innoargs, 5) + # handle = self.run_in(self.run_innoargs, 5) - def run_daily(self, args, kwargs): + def run_daily_c(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("Running daily at {}".format(now)) + self.log("Running daily at {}".format(now)) - def run_once(self, args, kwargs): + def run_once_c(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("Running once at {}".format(now)) + self.log("Running once at {}".format(now)) - def run_every(self, args, kwargs): + def run_every_c(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("Running once at {}".format(now)) + self.log("Running once at {}".format(now)) - def run_in(self, args, kwargs): + def run_in_c(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("run in {}, extra positional {}, title {}, test {}, at {}".format(args[0], args[1], kwargs["title"], kwargs["test"], now)) - self.error.info("Error Test") + self.log("run in {}, extra positional {}, title {}, test {}, at {}".format(args[0], args[1], kwargs["title"], kwargs["test"], now)) - def run_innoargs(self, args, kwargs): + def run_innoargs_c(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("run_innoargs at {}".format(now)) + self.log("run_innoargs at {}".format(now)) - def run_everyhour(self, args, kwargs): + def run_hourly_c(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("run hourly at {}".format(now)) + self.log("run hourly at {}".format(now)) - def run_minutely(self, args, kwargs): + def run_minutely_c(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("run every minute at {}".format(now)) + self.log("run every minute at {}".format(now)) diff --git a/conf/apps/service.py b/conf/apps/service.py index 0b28152d9..ff09c044f 100755 --- a/conf/apps/service.py +++ b/conf/apps/service.py @@ -1,22 +1,22 @@ -import homeassistant as ha import appapi class Service(appapi.APPDaemon): def initialize(self): return - ha.notify("", "Service initialized") + self.notify("", "Service initialized") # # turn_on and turn_off work with switches, lights, input_booleans, scenes and scripts # - ha.turn_on("light.office_1") - ha.run_in(self.name, self.toggle_light, 5) + self.turn_on("light.office_1") + self.run_in(self.toggle_light, 5) self.count = 0 def toggle_light(self, args, kwargs): - ha.toggle("light.office_1") + self.log("Toggling Light") + self.toggle("light.office_1") self.count += 1 if self.count < 6: - ha.run_in(self.name, self.toggle_light, 5) + self.run_in(self.toggle_light, 5) else: - ha.turn_off("light.office_1") + self.turn_off("light.office_1") diff --git a/conf/apps/state.py b/conf/apps/state.py index 7e5dc097c..5175eddbb 100755 --- a/conf/apps/state.py +++ b/conf/apps/state.py @@ -1,27 +1,59 @@ -import homeassistant as ha import appapi class State(appapi.APPDaemon): def initialize(self): - ha.listen_attr(self.name, self.attr, "input_select.house_mode", "state") - state = ha.get_state("input_select.house_mode", "state") - self.logger.info("State: Current State is {}".format(state)) return - ha.listen_attr(self.name, self.attr, "light.office_1", "state") - ha.listen_attr(self.name, self.attr, "light.office_1", "attributes.brightness") - #self.logger.info(self.args["param1"]) - ha.listen_state(self.name, self.all_state) - ha.listen_state(self.name, self.lights, "light") - ha.listen_state(self.name, self.lights, "light.office_1") - + # Set some callbacks + #self.handle = self.listen_state(self.all_state) + + # set timer to cancel above callback in 10 seconds + + #self.run_in(self.cancel, 10) - def all_state(self, entity, old, new): - self.logger.info("Entity {} changed state".format(entity)) + #self.listen_state(self.device, "light") + #self.listen_state(self.entity, "light.office_1") + #self.listen_state(self.attr, "light.office_1", "all") + #self.listen_state(self.attr, "light.office_1", "state") + #self.listen_state(self.attr, "light.office_1", "brightness") + + + # Check some state values + #state = self.get_state() + #self.log(state) + #state = self.get_state("media_player") + #self.log(state) + #state = self.get_state("light.office_1") + #self.log(state) + #state = self.get_state("light.office_1", "brightness") + #self.log(state) + #state = self.get_state("light.office_1", "all") + #self.log(state) + + # Invalid combination + + #state = self.get_state("media_player", "state") + #self.log(state) + + # Set a state + + #status = self.set_state("light.office_1", state = "on", attributes = {"color_name": "red"}) + #self.log(status) + + + + def all_state(self, entity, attribute, old, new): + self.log("Device {} went from {} to {}".format(entity, old, new)) + + def device(self, entity, attribute, old, new): + self.log("Device {} went from {} to {}".format(entity, old, new)) - def lights(self, entity, old, new): - self.logger.info("Light {} went from {} to {}".format(entity, old["state"], new["state"])) + def entity(self, entity, attribute, old, new): + self.log("Entity {} went from {} to {}".format(entity, old, new)) def attr(self, entity, attribute, old, new): - self.logger.info("ATTR {} {} {} {}".format(entity, attribute, old, new)) + self.log("Attr {} {} {} {}".format(entity, attribute, old, new)) + def cancel(self, args, kwargs): + self.log("Cancelling callback: {}".format(self.handle)) + self.cancel_listen_state(self.handle) \ No newline at end of file diff --git a/conf/apps/sun.py b/conf/apps/sun.py index 8037fa2f6..dd0548f0f 100755 --- a/conf/apps/sun.py +++ b/conf/apps/sun.py @@ -1,35 +1,43 @@ -import homeassistant as ha import appapi import datetime class Sun(appapi.APPDaemon): def initialize(self): - + #return self.get_sun_info() + # Test convert_utc() + sunset = self.get_state("sun.sun", "next_setting") + sunset_datetime = self.convert_utc(sunset) + self.log("Next sunset: {}, {}".format(sunset, sunset_datetime)) # Test - # ha.run_in(self.name, self.sun, 5, "Sunrise Test") + # self.run_in(self.name, self.sun, 5, "Sunrise Test") # Run at Sunrise + + # Example using timedelta + self.run_at_sunrise(self.sun, datetime.timedelta(minutes = 45).total_seconds(), "Sunrise +45 mins") + # or you can just do the math yourself + self.run_at_sunrise(self.sun, 30 * 60, "Sunrise +30 mins") - ha.run_at_sunrise(self.name, self.sun, -1, "Sunrise -1") - ha.run_at_sunrise(self.name, self.sun, 0, "Sunrise") - ha.run_at_sunrise(self.name, self.sun, 1, "Sunrise +1") + self.run_at_sunrise(self.sun, -1, "Sunrise -1 sec") + self.run_at_sunrise( self.sun, 0, "Sunrise") + self.run_at_sunrise(self.sun, 1, "Sunrise +1 sec") # Run at Sunset - ha.run_at_sunset(self.name, self.sun, -1, "Sunset -1") - ha.run_at_sunset(self.name, self.sun, 0, "Sunset") - ha.run_at_sunset(self.name, self.sun, 1, "Sunset +1") + self.run_at_sunset(self.sun, -1, "Sunset -1 sec") + self.run_at_sunset(self.sun, 0, "Sunset") + self.run_at_sunset(self.sun, 1, "Sunset +1 sec") def get_sun_info(self): - self.logger.info("Current sun state: {}".format(ha.sun_state())) - self.logger.info("Next sunrise: {}".format(ha.sunrise())) - self.logger.info("Next sunset: {}".format(ha.sunset())) + self.log("Current sun state: Up = {}, Down = {}".format(self.sun_up(), self.sun_down())) + self.log("Next sunrise: {}".format(self.sunrise())) + self.log("Next sunset: {}".format(self.sunset())) def sun(self, args, kwargs): now = datetime.datetime.now() - self.logger.info("{} {}".format(args[0], now)) + self.log("{} {}".format(args[0], now)) self.get_sun_info() diff --git a/conf/apps/thread_starve.py b/conf/apps/thread_starve.py new file mode 100755 index 000000000..c4040c7a2 --- /dev/null +++ b/conf/apps/thread_starve.py @@ -0,0 +1,15 @@ +import appapi +import datetime +import time + +class ThreadStarve(appapi.APPDaemon): + + def initialize(self): + return + time = datetime.datetime.now() + datetime.timedelta(seconds=5) + self.run_every(self.run_every_c, time, 1) + + def run_every_c(self, args, kwargs): + self.log("ThreadStarve: Running once") + time.sleep(15) + \ No newline at end of file diff --git a/conf/apps/trackers.py b/conf/apps/trackers.py new file mode 100755 index 000000000..7f5373256 --- /dev/null +++ b/conf/apps/trackers.py @@ -0,0 +1,38 @@ +import appapi + +class Trackers(appapi.APPDaemon): + + def initialize(self): + #return + self.log("Tracker: Anyone home is {}".format(self.anyone_home())) + trackers = self.get_trackers() + # + # Two ways to track individual state changes + + # Individual callbacks + for tracker in trackers: + self.log("{} ({}) is {}".format(tracker, self.friendly_name(tracker), self.get_tracker_state(tracker))) + self.listen_state(self.presence_change, tracker) + # Tracker state callbacks + self.listen_state(self.state_presence_change, "device_tracker") + + # Track Global state + self.listen_state(self.global_presence_change, "group.all_devices") + + def presence_change(self, entity, attribute, old, new): + self.log("{} went from {} to {}".format(self.friendly_name(entity), old, new)) + self.log("Tracker: Anyone home is {}".format(self.anyone_home())) + self.log("Tracker: Everyone home is {}".format(self.everyone_home())) + self.log("Tracker: Noone home is {}".format(self.noone_home())) + + def state_presence_change(self, entity, attribute, old, new): + self.log("{} went from {} to {}".format(self.friendly_name(entity), old["state"], new["state"])) + self.log("Tracker: Anyone home is {}".format(self.anyone_home())) + self.log("Tracker: Everyone home is {}".format(self.everyone_home())) + self.log("Tracker: Noone home is {}".format(self.noone_home())) + + def global_presence_change(self, entity, attribute, old, new): + self.log("{} went from {} to {}".format(entity, old, new)) + self.log("Tracker: Anyone home is {}".format(self.anyone_home())) + self.log("Tracker: Everyone home is {}".format(self.everyone_home())) + self.log("Tracker: Noone home is {}".format(self.noone_home())) diff --git a/conf/apps/two_class.py b/conf/apps/two_class.py new file mode 100755 index 000000000..9b6db26a4 --- /dev/null +++ b/conf/apps/two_class.py @@ -0,0 +1,21 @@ +import appapi + +class Class1(appapi.APPDaemon): + + def initialize(self): + return + self.log("Class 1: Parameter is {}".format(self.args["param1"])) + self.run_in(self.run_once_c, 5) + + def run_once_c(self, args, kwargs): + self.log("{}: Running once".format(self.args["param1"])) + +class Class2(appapi.APPDaemon): + + def initialize(self): + return + self.log("Class 2: Parameter is {}".format(self.args["param1"])) + self.run_in(self.run_once_c, 5) + + def run_once_c(self, args, kwargs): + self.log("{}: Running once".format(self.args["param1"])) \ No newline at end of file