Hi. This is still a work in progress but i’d like to share it as soon as possible. As some of you, I use to use hadashboard to write remote controllers frontends. Usually this means configuring the remote control component (xiaomi, apple tv, etc) in HA, writing a script for each command/button and then using the script widget in dashboard to call each script. Since you can just call remote/send_command thru the HA API I think the extra script step is unnecessary. So after a little tinkering, this is what I did:
configure your remote component on HA (mine is the xiaomi IR).
create the “custom_widgets” inside appdaemon conf folder (the same where is your appdaemon.yaml file)
create the “remote.yaml” component (I copied the script.yaml widget file and did a few mods). Here’s mine:
I did some progress in reproducing my “physical” remotes in virtual ones, including the apple tv remote! But I got a little more ambitious and want to send multiple remote commands with just one click (like the 3-digit channel number). The HA service “remote.send_command” accepts “command” as a single command or as a list of commands, something like { “command”: [ “up”, “up”, “enter” ] }. The problem is that I don’t know how to send a list and the widget try to send it just like a string, so when calling the ha service the dashboard sends something like { “command”: “up, up, enter”}. I tried to put the [] and things just got worse and saw something like “command[]”: “” or “command”: “[ up, up, enter ]”.
Maybe it will be easier to write a script on HA which receives such string and split it, but If it could be done directly in widget it will be nicer. Any ideas?
Yes, that’s what it should like. The problem is that appdaemon/dashboard encloses everything you put in as the command args with quotes, so you end with something like “[ up, up, enter ]”. My config:
Using just one command works fine, but more than one doesn’t. :-(. I couldn’t find other widget that make use of lists so I could see how they work. Light widget for example accepts RGB color, but it doesn’t seem to send it as a list.
tried that too. But the script doesnt seem to honor that. In fact, I looked into the javascript code and could not figure out how the json is built. at the OnButtonClick there’s a call to the call_service function using the dictionary “args” as one of the parameters. Even editing the args directly in the code doesnt work. Perhaps I should open a issue in appdaemon git. Another thing that i couldnt get to work is a multilevel or “nested” json/keys. I tried to modify the script widget to add script parameters. Homeassistant expects to receive a json like { ‘entity_id’: ‘myscript’, ‘service_data’ : { ‘parameter1’: ‘value1’, ‘parameter2’: ‘value2’ } }, but couldnt find a way to do that. It seems that the javascript can only build “flat” jsons and you end with something like ‘service_data[parameter1]’: ‘value1’.
good news. After a lot of struggling with javascript I found that I was looking at the wrong place and the culprit was the rundash.py :-). I did a patch so it now supports nested dictionaries and lists. I will submit a PR to the appdaemon so you may review it.
in the yaml, and in the script you need to call parameters[“my_command”] to get your wanted dict.
i really dont think there is a need to change the AD code itself.
i just tested to put that in the yaml and did send parameters.mycommand in the js to the cosole and got this:
so i guess you just didnt put the right things in the yaml.
if you want, but the code will not be merged as i see it, so dont forget that when there is a new release.
i did show you that there is no need to create and retranslate the yaml to a | sperated string if you present it the right way in the yaml.
Let’s start over, as I may be missing something. And let’s start with the lists. After I got my remote widget working I tried the lists. As you said before, lists should be done in yaml as:
my_command:
- ht_tv
- ht_bd
- ht_cd
I tried that and the dashboard didn’t load. I think that’s right, because the widget was expecting a string and not a list. I missed the js part. So, my second* try was to build the “command” arg inside the javascript part. So I copied the baseswitch widget, renamed it (and the references) and added to the function OnButtonClick(self) the following:
if (self.parameters.mycommand)
{
args["command"] = self.parameters.mycommand
}
console.log(args)
self.call_service(self, args);
From the web console I can see now that command is a array:
That was the hint that the js part was right and the problem was on the python part. The same translation is done for the xy_color too. I did tried to define the args[“command”] as you said, and just did it again to double check, but result is the same. It appears correctly on web console but not in the call to HA. I did the same for the nested dictionaries, but lets save this for later.
I think Andrew did it because the rgb_color is sent as a string, so a RE is used to parse the string into a list. It’s very similar to the way i did by splitting the string by the pipe symbol.