i read what you are doing and in your python code is so much wrong that it is clear that you have no clue what you are doing.
but thats no problem i will try to get some things clear for you:
ill take apart your app step by step.
first lets take a look at your yaml:
dash_app:
module: dash
class: simple_dash
dash_buttons:
- switch.dash_brabantia: study_light_on
- switch.dash_gillette: study_light_off
YAML is nothing more then a fancy way to write a dictionairy.
and you can call that dictionairy from inside your app.
self.args is a dictionairy that contains this:
module: dash
class: simple_dash
dash_buttons:
- switch.dash_brabantia: study_light_on
- switch.dash_gillette: study_light_off
now we can start to split that up:
self.args[âmoduleâ] = simple_dash
self.args[âdash_buttonsâ] contains a list with dictionairies. the list is because you use -.
so in that list we find [{âswitch.dash_brabantiaâ: âstudy_light_onâ},{âswitch.dash_gilletteâ: âstudy_light_offâ}]
we can splitt that out a bit more
self.args[âdash_buttonsâ][0] = {âswitch.dash_brabantiaâ: âstudy_light_onâ}
self.args[âdash_buttonsâ][0][âswitch.dash_brabantiaâ] = âstudy_light_onâ
i hope that clears a bit more from the translation from yaml to self.args
now the code
for control in self.args['dash_buttons']:
self.listen_state(self.run_action(control), control[0], new="on")
you try to give args in a callback and that isnt gonna work.
the callback needs to be the name from a function, without any args. if you want to give on some args you can do that after the needed settings as kwargs.
i dont think you can use getattr or eval at that place or a string, but i am not sure about that, because i never tried and dont know how Andrew translates that part to a function.
when i look at your function you can do a few things:
- give control as a kwarg
- use the entity to get the action from the args.
the 2nd is the most easy because we already have the arg list.
so your listen_state wil become
self.listen_state(self.run_action, control[0], new="on")
then the callback
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()
you try to get self.args[âcontrolâ] but as i explained you now know that that wont work.
control is nowhere in your yaml and even without quotes it wont work because you have no variable called control in your function also.
but we have the entity in the callback and we have your yaml.
so lets get the things you like from that.
for button in self.args["dash_buttons"]:
if button[0] == entity:
functionpart = button[1]
getattr(self, function)()
thats about the code you used above and why things cant work.
to get rid of the run_action function all together you could try if this will work:
for control in self.args['dash_buttons']:
self.listen_state(getattr(self,control[1]), control[0], new="on")
it can very well be that it will work but i never tried.