Skip to content

Commit b8b7e66

Browse files
committed
Update conf examples
1 parent 17cdb76 commit b8b7e66

12 files changed

+257
-92
lines changed

conf/appdaemon.cfg

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[appdaemon]
2-
ha_url = <some_url>
2+
ha_url = <some url>
33
ha_key = <some key>
4-
logfile = /etc/appdaemon/appdaemon.log
5-
errorfile = /etc/appdaemon/error.log
6-
app_dir = /srv/hass/src/appdaemon/apps
4+
logfile = /srv/hass/appdaemon_test/logs/appdaemon.log
5+
errorfile = /srv/hass/appdaemon_test/logs/error.log
6+
app_dir = /srv/hass/appdaemon_test/conf/apps
77
threads = 10
88
# Apps
99
[state]
@@ -25,6 +25,30 @@ class = Sun
2525
[service]
2626
module = service
2727
class = Service
28-
28+
[motion_lights]
29+
module = motion_lights
30+
class = MotionLights
31+
[trackers]
32+
module = trackers
33+
class = Trackers
34+
[class_1]
35+
module = two_class
36+
class = Class1
37+
param1 = This is class 1
38+
[class_1a]
39+
module = two_class
40+
class = Class1
41+
param1 = This is class 1a
42+
[class_1b]
43+
module = two_class
44+
class = Class1
45+
param1 = This is class 1b
46+
[class_2]
47+
module = two_class
48+
class = Class2
49+
param1 = This is class 2
50+
[thread_starve]
51+
module = thread_starve
52+
class = ThreadStarve
2953

3054

conf/apps/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Placeholder - required to all app module loading

conf/apps/log.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import homeassistant as ha
21
import appapi
32

43
class Log(appapi.APPDaemon):
54

65
def initialize(self):
76
return
8-
self.logger.info("Log Test: Parameter is {}".format(self.args["param1"]))
7+
self.log("Log Test: Parameter is {}".format(self.args["param1"]))
8+
self.error("Error Test")
9+

conf/apps/mirror_light.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
import homeassistant as ha
21
import appapi
32

43
class MirrorLight(appapi.APPDaemon):
54

65
def initialize(self):
7-
ha.listen_state(self.name, self.light_changed, "light.andrew_bedside")
6+
#return
7+
self.listen_state(self.light_changed, "light.andrew_bedside")
88

9-
state = ha.get_state("light.andrew_bedside", "state")
10-
brightness = ha.get_state("light.andrew_bedside", "attributes.brightness")
9+
state = self.get_state("light.andrew_bedside")
10+
brightness = self.get_state("light.andrew_bedside", "brightness")
1111

12-
self.logger.info("MirrorLight: Current State is {}, current brightness is {}".format(state, brightness))
12+
self.log("MirrorLight: Current State is {}, current brightness is {}".format(state, brightness))
1313

14-
if ha.get_state("light.andrew_bedside", "state") == "on":
15-
ha.turn_on("light.office_lamp")
14+
if state == "on":
15+
self.call_service("light", "office_lamp", color_name = "red")
16+
else:
17+
self.turn_off("light.office_lamp")
1618

17-
def light_changed(self, entity, old_state, new_state):
18-
self.logger.info("entity state changed, old: {}, new: {}".format(old_state["state"], new_state["state"]))
19+
def light_changed(self, entity, attribute, old, new):
20+
self.log("MirrorLight: entity {}.{} state changed, old: {}, new: {}".format(entity, attribute, old, new))
1921

20-
if new_state["state"] == 'on':
21-
#ha.turn_on("light.office_lamp")
22-
ha.turn_on("light.office_lamp", color_name = "blue")
22+
if new == 'on':
23+
#self.turn_on("light.office_lamp")
24+
print(self.call_service("light", "turn_on", entity_id = "light.office_lamp", color_name = "red"))
2325
else:
24-
ha.turn_off("light.office_lamp")
26+
print(self.turn_off("light.office_lamp"))

conf/apps/motion_lights.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import appapi
2+
3+
class MotionLights(appapi.APPDaemon):
4+
5+
def initialize(self):
6+
return
7+
self.listen_state(self.motion, "binary_sensor.upstairs_sensor_28")
8+
9+
def motion(self, entity, attribute, old, new):
10+
if new == "on":
11+
#if new == "on" and self.sun_state() == "below_horizon":
12+
self.turn_on("light.office_1")
13+
self.run_in(self.light_off, 60)
14+
self.flashcount = 0
15+
self.run_in(self.flash_warning, 1)
16+
17+
def light_off(self, args, kwargs):
18+
self.turn_off("light.office_1")
19+
20+
def flash_warning(self, args, kwargs):
21+
self.toggle("light.office_2")
22+
self.flashcount += 1
23+
if self.flashcount < 10:
24+
self.run_in(self.flash_warning, 1)
25+

conf/apps/schedule.py

+34-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import homeassistant as ha
21
import appapi
32
import datetime
43

@@ -8,87 +7,86 @@ def initialize(self):
87
return
98
# Run a few timers and pass parameters
109

11-
#ha.run_in(self.name, self.run_in, 5, 5, 10, title = "run_in5", test = "Another Param")
12-
#ha.run_in(self.name, self.run_in, 10, 10, 15, title = "run_in10", test = "Another Param")
13-
#ha.run_in(self.name, self.run_in, 15, 15, 20, title = "run_in15", test = "Another Param")
14-
#ha.run_in(self.name, self.run_in, 20, 20, 25, title = "run_in20", test = "Another Param")
10+
self.run_in(self.run_in_c, 5, 5, 10, title = "run_in5", test = "Another Param")
11+
self.run_in(self.run_in_c, 10, 10, 15, title = "run_in10", test = "Another Param")
12+
self.run_in(self.run_in_c, 15, 15, 20, title = "run_in15", test = "Another Param")
13+
self.run_in(self.run_in_c, 20, 20, 25, title = "run_in20", test = "Another Param")
1514

1615
# run_in with no params
1716

18-
#ha.run_in(self.name, self.run_innoargs, 5)
17+
self.run_in(self.run_innoargs_c, 5)
1918

2019
# Create a timer and then cancel it
2120

22-
#handle = ha.run_in(self.name, self.run_in, 15)
23-
#ha.cancel_timer(self.name, handle)
21+
handle = self.run_in(self.run_in_c, 15)
22+
self.cancel_timer(handle)
2423

2524
# Run at a specific time
2625

2726
#runtime = datetime.time(11, 14, 0)
28-
#runtime = (datetime.datetime.now() + datetime.timedelta(seconds=20)).time()
29-
#handle = ha.run_once(self.name, self.run_once, runtime)
27+
runtime = (datetime.datetime.now() + datetime.timedelta(seconds=20)).time()
28+
handle = self.run_once(self.run_once_c, runtime)
3029

3130
# Run every day at a specific time
3231

3332
# e.g.time = datetime.time(12, 49, 0)
34-
#runtime = (datetime.datetime.now() + datetime.timedelta(seconds=25)).time()
35-
#ha.run_daily(self.name, self.run_daily, runtime)
33+
runtime = (datetime.datetime.now() + datetime.timedelta(seconds=25)).time()
34+
self.run_daily(self.run_daily_c, runtime)
3635

3736
# Run Hourly starting 1 hour from now
3837

39-
#ha.run_hourly(self.name, self.run_everyhour, None)
38+
self.run_hourly(self.run_hourly_c, None)
4039

4140
# Run Hourly on the hour
4241

43-
#time = datetime.time(0, 0, 0)
44-
#ha.run_hourly(self.name, self.run_everyhour, time)
42+
time = datetime.time(0, 0, 0)
43+
self.run_hourly(self.run_hourly_c, time)
4544

4645
# Run Every Minute starting in 1 minute
4746

48-
#ha.run_minutely(self.name, self.run_minutely, None)
47+
self.run_minutely(self.run_minutely_c, None)
4948

5049
# Run Every Minute on the minute
5150

52-
#time = datetime.time(0, 0, 0)
53-
#ha.run_minutely(self.name, self.run_minutely, time)
51+
time = datetime.time(0, 0, 0)
52+
self.run_minutely(self.run_minutely_c, time)
5453

5554
# Run every 13 seconds starting in 10 seconds time
5655

57-
# time = datetime.datetime.now() + datetime.timedelta(seconds=10)
58-
# ha.run_every(self.name, self.run_every, time, 10)
56+
time = datetime.datetime.now() + datetime.timedelta(seconds=10)
57+
self.run_every(self.run_every_c, time, 13)
5958

6059
# Attempt some scheduler abuse ...
6160

6261
#for x in range(1, 10000):
63-
# handle = ha.run_in(self.name, self.run_innoargs, 5)
62+
# handle = self.run_in(self.run_innoargs, 5)
6463

6564

66-
def run_daily(self, args, kwargs):
65+
def run_daily_c(self, args, kwargs):
6766
now = datetime.datetime.now()
68-
self.logger.info("Running daily at {}".format(now))
67+
self.log("Running daily at {}".format(now))
6968

70-
def run_once(self, args, kwargs):
69+
def run_once_c(self, args, kwargs):
7170
now = datetime.datetime.now()
72-
self.logger.info("Running once at {}".format(now))
71+
self.log("Running once at {}".format(now))
7372

74-
def run_every(self, args, kwargs):
73+
def run_every_c(self, args, kwargs):
7574
now = datetime.datetime.now()
76-
self.logger.info("Running once at {}".format(now))
75+
self.log("Running once at {}".format(now))
7776

78-
def run_in(self, args, kwargs):
77+
def run_in_c(self, args, kwargs):
7978
now = datetime.datetime.now()
80-
self.logger.info("run in {}, extra positional {}, title {}, test {}, at {}".format(args[0], args[1], kwargs["title"], kwargs["test"], now))
81-
self.error.info("Error Test")
79+
self.log("run in {}, extra positional {}, title {}, test {}, at {}".format(args[0], args[1], kwargs["title"], kwargs["test"], now))
8280

83-
def run_innoargs(self, args, kwargs):
81+
def run_innoargs_c(self, args, kwargs):
8482
now = datetime.datetime.now()
85-
self.logger.info("run_innoargs at {}".format(now))
83+
self.log("run_innoargs at {}".format(now))
8684

87-
def run_everyhour(self, args, kwargs):
85+
def run_hourly_c(self, args, kwargs):
8886
now = datetime.datetime.now()
89-
self.logger.info("run hourly at {}".format(now))
87+
self.log("run hourly at {}".format(now))
9088

91-
def run_minutely(self, args, kwargs):
89+
def run_minutely_c(self, args, kwargs):
9290
now = datetime.datetime.now()
93-
self.logger.info("run every minute at {}".format(now))
91+
self.log("run every minute at {}".format(now))
9492

conf/apps/service.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import homeassistant as ha
21
import appapi
32

43
class Service(appapi.APPDaemon):
54

65
def initialize(self):
76
return
8-
ha.notify("", "Service initialized")
7+
self.notify("", "Service initialized")
98
#
109
# turn_on and turn_off work with switches, lights, input_booleans, scenes and scripts
1110
#
12-
ha.turn_on("light.office_1")
13-
ha.run_in(self.name, self.toggle_light, 5)
11+
self.turn_on("light.office_1")
12+
self.run_in(self.toggle_light, 5)
1413
self.count = 0
1514

1615
def toggle_light(self, args, kwargs):
17-
ha.toggle("light.office_1")
16+
self.log("Toggling Light")
17+
self.toggle("light.office_1")
1818
self.count += 1
1919
if self.count < 6:
20-
ha.run_in(self.name, self.toggle_light, 5)
20+
self.run_in(self.toggle_light, 5)
2121
else:
22-
ha.turn_off("light.office_1")
22+
self.turn_off("light.office_1")

conf/apps/state.py

+48-16
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
1-
import homeassistant as ha
21
import appapi
32

43
class State(appapi.APPDaemon):
54

65
def initialize(self):
7-
ha.listen_attr(self.name, self.attr, "input_select.house_mode", "state")
8-
state = ha.get_state("input_select.house_mode", "state")
9-
self.logger.info("State: Current State is {}".format(state))
106
return
11-
ha.listen_attr(self.name, self.attr, "light.office_1", "state")
12-
ha.listen_attr(self.name, self.attr, "light.office_1", "attributes.brightness")
13-
#self.logger.info(self.args["param1"])
14-
ha.listen_state(self.name, self.all_state)
15-
ha.listen_state(self.name, self.lights, "light")
16-
ha.listen_state(self.name, self.lights, "light.office_1")
17-
7+
# Set some callbacks
8+
#self.handle = self.listen_state(self.all_state)
9+
10+
# set timer to cancel above callback in 10 seconds
11+
12+
#self.run_in(self.cancel, 10)
1813

19-
def all_state(self, entity, old, new):
20-
self.logger.info("Entity {} changed state".format(entity))
14+
#self.listen_state(self.device, "light")
15+
#self.listen_state(self.entity, "light.office_1")
16+
#self.listen_state(self.attr, "light.office_1", "all")
17+
#self.listen_state(self.attr, "light.office_1", "state")
18+
#self.listen_state(self.attr, "light.office_1", "brightness")
19+
20+
21+
# Check some state values
22+
#state = self.get_state()
23+
#self.log(state)
24+
#state = self.get_state("media_player")
25+
#self.log(state)
26+
#state = self.get_state("light.office_1")
27+
#self.log(state)
28+
#state = self.get_state("light.office_1", "brightness")
29+
#self.log(state)
30+
#state = self.get_state("light.office_1", "all")
31+
#self.log(state)
32+
33+
# Invalid combination
34+
35+
#state = self.get_state("media_player", "state")
36+
#self.log(state)
37+
38+
# Set a state
39+
40+
#status = self.set_state("light.office_1", state = "on", attributes = {"color_name": "red"})
41+
#self.log(status)
42+
43+
44+
45+
def all_state(self, entity, attribute, old, new):
46+
self.log("Device {} went from {} to {}".format(entity, old, new))
47+
48+
def device(self, entity, attribute, old, new):
49+
self.log("Device {} went from {} to {}".format(entity, old, new))
2150

22-
def lights(self, entity, old, new):
23-
self.logger.info("Light {} went from {} to {}".format(entity, old["state"], new["state"]))
51+
def entity(self, entity, attribute, old, new):
52+
self.log("Entity {} went from {} to {}".format(entity, old, new))
2453

2554
def attr(self, entity, attribute, old, new):
26-
self.logger.info("ATTR {} {} {} {}".format(entity, attribute, old, new))
55+
self.log("Attr {} {} {} {}".format(entity, attribute, old, new))
2756

57+
def cancel(self, args, kwargs):
58+
self.log("Cancelling callback: {}".format(self.handle))
59+
self.cancel_listen_state(self.handle)

0 commit comments

Comments
 (0)