OK, so I have tried to follow the discussion above and put a test together.
I have a yaml
dash_app:
module: dash
class: simple_dash
dash_buttons:
- switch.dash_brabantia: study_light_on
- switch.dash_gillette: study_light_off
And so I need code which
1- initialzes by running a self_listen_state
on every item in the list for an on
action
2 - calls via getattr
the second item on a line when one is found
Here is my attempt based on the discussion above. (I am struggling at the limits of my Python knowledge at this point, so please be gentle!)
import appdaemon.plugins.hass.hassapi as hass
class simple_dash(hass.Hass):
def initialize(self):
self.log("Hello from simple_dash")
for control in self.args['dash_buttons']:
self.listen_state(self.run_action(control), control[0], new="on")
def run_action(self, entity, attribute, old, new, kwargs):
self.log("Got an ON from {}, About to run {}".format(self.args["control"][0], self.args["control"][1] ))
function = getattr(self, self.args["control"][1])
function()
def study_light_on(self):
self.log("Running study_light_on")
self.turn_on("light.study__ian")
def study_light_off(self):
self.log("Running study_light_off")
self.turn_off("light.study__ian")
I am getting
File "/home/pi/appdaemon/apps/dash/dash.py", line 7, in initialize
for control in self.args['dash_buttons']:
KeyError: 'dash_buttons'
EDIT - now I look at this again I can see that the run_action
function is redundant - it would be better to reference the actual function to be run directly in self.listen_state
by reference to the control[1]
eg study_light_on
. The second argument of self.listen_state
should be the first item of the list element control[0] eg switch.dash_brabantia
So the line in initialize should be something like
self.listen_state(getattr(self, control[1])(), control[0], new="on")
but correctly syntax’ed!!