Page 1 of 1

Execute Python script with args from call to URL?

PostPosted: Sun Mar 21, 2021 6:03 pm
by Espressomatic
1. I have an external python script I'd like to add to my Indigo install which can control my Samsung TV - it takes args to send Samsung's remote API commands.

2. I'd like to be able to trigger this script and pass arguments from a URL - sent from another device on the network - in my case, it's an IP-based hand-held remote control.

I know about Action -> Server Actions -> Script and File Actions -> Run Shell Script - but don't know if/how to include an argument that's passed to it externally.

I also know about the RESTful_URL support that allows me to call up devices and actions - I also don't know how to pass an argument to an action here.

Any suggestions on how to accomplish what I'm looking for? Are these built-in features the way to go?

I'd like to avoid making a dozen or more action groups each with a specific Samsung command built into it. Ideally I have the script defined once and be able to call it from my other device with any number of commands specified in the formatted URL.

Re: Execute Python script with args from call to URL?

PostPosted: Mon Mar 22, 2021 12:21 pm
by jay (support)
The Run Shell Script action allows substitutions in the path (which can also contain params). So, for example, this:

Code: Select all
/full/path/to/python /full/path/to/script.py -a %%v:12345%% -b %%v:54321%%


would substitute the values and pass them to a script run directly by the python interpreter. You would then populate those variables before performing the action. If it's only one variable, then you could trigger the action any time the variable gets updated.

There are also a variety of other ways to skin this cat, some using plugins (like using the HTTPd plugin).

It's timely that you mention this - just this morning Matt and I were brainstorming a way that might make this quite simple. Nothing to announce yet, but at least some of the plumbing will be in place in the next release if not a complete solution.

Re: Execute Python script with args from call to URL?

PostPosted: Mon Mar 22, 2021 4:49 pm
by Espressomatic
Thanks. On the way in to work this morning I thought about the possibility of using a variable, but also thought I'd have to make two URL calls per button on my remote - one to set the variable and one to fire the script. But if the variable can automatically trigger the script, that would make it a lot cleaner on the remote side.

Not terribly hackey this way and it should be easy to manage. I'm looking forward to anything you guys add to make this even more streamlined.

Re: Execute Python script with args from call to URL?

PostPosted: Fri Aug 06, 2021 2:33 pm
by ryanbuckner
I was looking to do something similar and noticed that Jay mentioned new functions for 2021.1. What would be the new preferred way for this?

Re: Execute Python script with args from call to URL?

PostPosted: Fri Aug 06, 2021 3:40 pm
by DaveL17
The new changes are for creating Web Server APIs for plugins. This allows URL calls to pass arguments to the plugin which then take action on the data passed. I'm playing around with this now to understand the ins and outs of how this can work. Short answer: it works really well!

I've created a custom web page that contains an HTML form. When a Submit button is pressed, the form values are passed back to the plugin and the plugin then updates an Indigo device value using data from the form (using standard HTML form controls).

Screen Shot 2021-08-06 at 4.26.02 PM.png
Screen Shot 2021-08-06 at 4.26.02 PM.png (64.5 KiB) Viewed 1566 times


I've also created a second page that contains an interactive "physical control" (using JavaScript) which updates the plugin whenever the control is changed (no submit needed). This image shows a control that's "on" and a control that's "off".

Screen Shot 2021-08-03 at 4.52.12 PM.png
Screen Shot 2021-08-03 at 4.52.12 PM.png (46.27 KiB) Viewed 1566 times


There are nuances to each approach (for example, in both cases the plugin can only take actions that it's authorized to take), but both approaches -- which in this case use totally different methods on the client side (HTML forms vs. JavaScript) -- have worked superbly. Both approaches use an API Key method for authentication supported by Indigo.

That said, you could also use a URL to update an Indigo variable using Indigo's REST API and then have a trigger fire a Python script anytime the variable value changes (and use said variable for the payload). This has been possible for a long while and is most likely the easiest way to do it.

Re: Execute Python script with args from call to URL?

PostPosted: Fri Aug 06, 2021 3:41 pm
by DaveL17
p.s. This is only two of many different ways that this can work. :D

Re: Execute Python script with args from call to URL?

PostPosted: Sun Oct 24, 2021 12:34 pm
by ryanbuckner
Espressomatic wrote:
1. I have an external python script I'd like to add to my Indigo install which can control my Samsung TV - it takes args to send Samsung's remote API commands.

2. I'd like to be able to trigger this script and pass arguments from a URL - sent from another device on the network - in my case, it's an IP-based hand-held remote control.

I know about Action -> Server Actions -> Script and File Actions -> Run Shell Script - but don't know if/how to include an argument that's passed to it externally.

I also know about the RESTful_URL support that allows me to call up devices and actions - I also don't know how to pass an argument to an action here.

Any suggestions on how to accomplish what I'm looking for? Are these built-in features the way to go?

I'd like to avoid making a dozen or more action groups each with a specific Samsung command built into it. Ideally I have the script defined once and be able to call it from my other device with any number of commands specified in the formatted URL.



How did you finally solve this? And can you share the script you use to control your Samsung TV?

Re: Execute Python script with args from call to URL?

PostPosted: Fri Oct 29, 2021 2:04 pm
by ryanbuckner
Another good tip. I used the print() function to return the value back to Indigo from the Run Shell Script action and had it stored in a variable. The Control page object was not recognizing "on" or "off" for the value.

Apparently the print() function appends "\n" to the end of a unicode string., so the actual value in the variable was on\n and off\n.

I was able to use the following override to fix it. I think this will be helpful to others.

Code: Select all
print(tvstatus, end="")