Skip to content

Commit

Permalink
Update conf examples
Browse files Browse the repository at this point in the history
  • Loading branch information
acockburn committed Jul 16, 2016
1 parent 17cdb76 commit b8b7e66
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 92 deletions.
34 changes: 29 additions & 5 deletions conf/appdaemon.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[appdaemon]
ha_url = <some_url>
ha_url = <some url>
ha_key = <some 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]
Expand All @@ -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


1 change: 1 addition & 0 deletions conf/apps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Placeholder - required to all app module loading
5 changes: 3 additions & 2 deletions conf/apps/log.py
Original file line number Diff line number Diff line change
@@ -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")

28 changes: 15 additions & 13 deletions conf/apps/mirror_light.py
Original file line number Diff line number Diff line change
@@ -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"))
25 changes: 25 additions & 0 deletions conf/apps/motion_lights.py
Original file line number Diff line number Diff line change
@@ -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)

70 changes: 34 additions & 36 deletions conf/apps/schedule.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import homeassistant as ha
import appapi
import datetime

Expand All @@ -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))

14 changes: 7 additions & 7 deletions conf/apps/service.py
Original file line number Diff line number Diff line change
@@ -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")
64 changes: 48 additions & 16 deletions conf/apps/state.py
Original file line number Diff line number Diff line change
@@ -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)
Loading

0 comments on commit b8b7e66

Please sign in to comment.