| Author |
Message |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
Your documentation refers to folder 2.5 in Python. On my server machine there is a version 2.6 as well. Should I use the latest, or is Indigo zoned into 2.5?
|
| Sat Dec 17, 2011 8:09 am |
|
 |
|
jay (support)
Site Admin
Joined: Mar 19, 2008 Posts: 6665 Location: Austin, Texas
|
 Re: Using variables in Python with DirectTV Plugin
Because of the library we're using to bridge C++ to Python, we have to target a single version of Python. 2.5 is the only commonly shipped Python version on Leopard, Snow Leopard, and Lion, so that's what we targeted. There are other low-level reasons as well.
_________________ Jay (Indigo Support)
|
| Sat Dec 17, 2011 8:26 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
Thanks....
|
| Sat Dec 17, 2011 8:30 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
OK, wrote a script, it all compiled, but when I went to run it I got an error in the embedded script saying "VariableCmds object is unindexible" on this line:
room = indigo.variable[334309571]
What am I doing wrong?
|
| Sat Dec 17, 2011 8:45 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
Nemmnind... saw I omitted the "s"
|
| Sat Dec 17, 2011 8:47 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
OK, got the script to run. One thing that is mildly amusing is actually I got an error when I used the syntax you originally suggested. In the call to the plug-in the attribute "value" wasn't accepted. Here is what I originally had:
dvrPlugin = indigo.server.getPlugin("com.perceptiveautomation.indigoplugin.directvdvrcontrol") if dvrPlugin.isEnabled(): room = indigo.variables[334309571] if room.value == "Study": address = '192.168.10.74' elif room.value == 'MBR': address = '192.168.10.73' elif room.value == 'Library': address = '192.168.10.72' elif room.value == 'Kitchen': address = '192.168.10.70' dvrPlugin.executeAction("sendKeyPress", props={'address': address.value, 'keyToPress':"menu"})
That generated an error that the str address didn't have the attribute "value". When I omitted the value attribute it worked like a charm.
|
| Sat Dec 17, 2011 9:21 am |
|
 |
|
jay (support)
Site Admin
Joined: Mar 19, 2008 Posts: 6665 Location: Austin, Texas
|
 Re: Using variables in Python with DirectTV Plugin
Look more closely at my example - I'm using the variable value directly in the executeAction call. You, on the other hand, are using the variable value (room.value) in the if statement to set the value of a Python variable "address" to the value of the Indigo variable (room).
Your original question was how to use a variable value as the address in the executeAction call - which is what my script does. It turns out that's not really what you wanted.
_________________ Jay (Indigo Support)
|
| Sat Dec 17, 2011 11:39 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
I've edited this message about 10 times as I've spent almost all weekend trying get some methods to access the DirecTV working from a python script called from an action group. I've encountered a bunch of strange behaviors which I'll ask you about another time, but I finally got it working. When I then went to implement it it stopped working and generated an error saying
"TypeError: No registered converter was able to produce a C++ rvalue of type CCString from this Python object of type Variable"
This is for a script that worked perfectly when I finally got everything figured out.
Not sure if it's relevant, but one of the weird behaviors I saw was at some point as I was editing the .py file the changes stopped being recognized by Indigo. It would act is if the file had never been modified and still processed it as if weren't changed. I even removed entire methods and it still gave errors for the method that was no longer in the file. No matter what I did it didn't recognize the changes, and ultimately I had to save the .py file under a new name change the action group script to import the new name. I had to do that about 10 times.... Maybe that has something to do with what I described above.
|
| Sat Dec 17, 2011 4:31 pm |
|
 |
|
jay (support)
Site Admin
Joined: Mar 19, 2008 Posts: 6665 Location: Austin, Texas
|
 Re: Using variables in Python with DirectTV Plugin
Which .py file? The one in the Python library directory or the one being run from your action? Where are you getting that error (event log, console, etc.)?
What does your library script look like? What about the script being executed from Indigo which uses the library?
Diagnosing problems is really hard when so little information is provided. I suspect that when it got into that state of not reloading the library script restarting the server may have solved the problem since it would have forced a restart of all Python interpreters.
_________________ Jay (Indigo Support)
|
| Sun Dec 18, 2011 11:06 pm |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
Sorry-- thought that message was something you might recognize.
I did restart the server; quit Indigo altogether, stopped the server, restarted, and it still did the same thing. I was looking for some way to kill Python but didn't see anything.
Here is the action script. One of the errors I was getting was I was passing 2 arguments to the function I was calling but it only accepted 1. After some research I see that is apparently just one of Python's peculiarities, so I put in the * parameter... Was also getting errors complaining that local variables were referenced before they were assigned, and str variable had no attribute 'value'. After much research and experimenting I got past all of those problems, and here is a pretty simplified action script and .py file containing the function.
Action group script:
keytopress ='guide' import dvrAttachments controlpage=indigo.variables[815606601] dvrAttachments.dtvKeyPress(keytopress, controlpage.value,'xxx')
.py script:
#! /usr/bin/env python2.5
try: import indigo except ImportError: print "Attachments can only be used from within Indigo" raise ImportError
def dtvKeyPress( a, b, c, *ignore ): if b == 'Study': c = indigo.variables[1547597612] elif b == 'MBR': c = indigo.variables[1749897041] elif b == 'Library': c = indigo.variables[1791505040] elif b == 'Kitchen': c = indigo.variables[631931047] dvrPlugin = indigo.server.getPlugin("com.perceptiveautomation.indigoplugin.directvdvrcontrol") if dvrPlugin.isEnabled(): dvrPlugin.executeAction("sendKeyPress", props={'address': c, 'keyToPress': a})
|
| Mon Dec 19, 2011 10:33 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
BTW, ignore the lack of indenting. That happened when I pasted the code into the message. It is properly indented in the actual file.
|
| Mon Dec 19, 2011 10:49 am |
|
 |
|
jay (support)
Site Admin
Joined: Mar 19, 2008 Posts: 6665 Location: Austin, Texas
|
 Re: Using variables in Python with DirectTV Plugin
Put your code inside code tags and the formatting will remain. And since Python is dependent on indentation for blocks it's more important than AppleScript (which has ending block tags).
The problem is in your dtvKeyPress handler - you're passing a Variable object ("c") as the address in the properties for the executeAction method - you should be passing the value of the variable ("c.value").
Some comments: having "c" as a parameter to your dtvKeyPress is unnecessary - you aren't actually passing in anything that's useful since 'xxx' isn't an Indigo Variable object. In fact, it's likely to throw an error if 'controlpage.value' turns out to be anything other than the 4 specified values.
Also, "*ignore" isn't needed either since you're not using any optional parameters that might be passed in.
_________________ Jay (Indigo Support)
|
| Mon Dec 19, 2011 11:01 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
Thanks on the c.value; I'll try that, but I'm curious as to why it worked initially....
As to the parameters, there seems to be an issue with Python where it misreports the number of parameters. If I simply put a and b as parameters, I get an error saying I'm passing 2 arguments but the method specifies exactly 1... After some web research I discovered this is a widely known issue, and the suggestion is to put in the optional parameter, which I did and it eliminated that error. The other miscellaneous value of 'xxx' was the only thing that stopped me from getting an error that the str variable was missing the attribute 'value'.
Any other ideas on why the revised python files stopped importing but worked if I renamed it and referred to a new name?
|
| Mon Dec 19, 2011 11:14 am |
|
 |
|
kennybroh
Joined: Dec 13, 2009 Posts: 192 Location: Baltimore
|
 Re: Using variables in Python with DirectTV Plugin
Made the change, but still getting this error:
embedded script, line 4, at top level File "/Library/Python/2.5/site-packages/dvrAttachments3.py", line 21, in dtvKeyPress dvrPlugin.executeAction("sendKeyPress", props={'address': c.value, 'keyToPress': a}) TypeError: No registered converter was able to produce a C++ rvalue of type CCString from this Python object of type Variable
|
| Mon Dec 19, 2011 11:19 am |
|
 |
|
jay (support)
Site Admin
Joined: Mar 19, 2008 Posts: 6665 Location: Austin, Texas
|
 Re: Using variables in Python with DirectTV Plugin
Ok, I've modified your library script so that it works: - Code: Select all
#! /usr/bin/python2.5 try: import indigo except ImportError: print "Attachments can only be used from within Indigo" raise ImportError
def dtvKeyPress(keyToPress, room): # set the local variable to None which indicates that a matching # indigo Variable object hasn't been found addressVar = None
# look for a matching indigo Variable object - inside a try block in case the variable # gets deleted some day try: if room == 'Study': addressVar = indigo.variables[1547597612] elif room == 'MBR': addressVar = indigo.variables[1749897041] elif room == 'Library': addressVar = indigo.variables[1791505040] elif room == 'Kitchen': addressVar = indigo.variables[631931047] except: pass
# test addressVar to see if you found a matching variable if addressVar: indigo.server.log("found variable: %s" % (addressVar.name), type="kennyBroLibrary") dvrPlugin = indigo.server.getPlugin("com.perceptiveautomation.indigoplugin.directvdvrcontrol") if dvrPlugin.isEnabled(): indigo.server.log("Sending %s to %s" % (keyToPress, addressVar.value), type="kennyBroLibrary") dvrPlugin.executeAction("sendKeyPress", props={'address': addressVar.value, 'keyToPress': keyToPress}) else: indigo.server.log("The room (%s) specified doesn't map to an Indigo variable" % (room), type="kennyBroLibrary", isError=True)
Here are the changes I made and why: - Passing only the parameters that you need - the keyToPress (used to be a) string and the room (used to be b) string. You don't need anything else.
- I set the local addressVar (used to be c) to None - indicating that it hasn't found a matching Indigo Variable object yet.
- Put the room lookup into a try statement so that if a variable gets deleted at some point in the future you'll see a nice error rather than an ugly stack trace in the Indigo log.
- Added a log entry just before the executeAction call that shows the value and key being sent.
- Send the value of the variable found along with the keypress
Here's the embedded script in the execute script action: - Code: Select all
import kennyBroLibrary
kennyBroLibrary.dtvKeyPress('menu', 'Study')
The name of my library script is "kennyBroLibrary.py", YMMV. The reason why the library isn't getting reloaded when you make changes is because Indigo starts up a persistent background process when the first embedded script is launched and all subsequent embedded scripts are run in that process. Unfortunately, it caches all the associated library files at that point so your changes aren't used until that process is restarted - it always happens when the Indigo server is restarted. We're modifying the "Reload AppleScript Attachments" menu item so that it will also restart that background process which will solve the problem. In the meantime, rather than restarting the server every time (which can be time consuming during development), you can just kill the background process in a terminal window: - Code: Select all
FatMac:~ jay$ ps -ax | grep "-e_background_" 78385 ?? 0:00.56 /Library/Application Support/Perceptive Automation/Indigo 5/IndigoPluginHost_d.app/Contents/MacOS/IndigoPluginHost_d -p1176 -e_background_ 78478 ttys002 0:00.00 grep -e_background_ FatMac:~ jay$ kill 78385 FatMac:~ jay$
So, you do the 'ps -ax | grep "-e_background_"' command first and that will show the background process and it's PID (the number in the first column). Then, issue the 'kill' command with that number and that will kill the background process. The next time you run your embedded script Indigo will restart the background process and it will reload your library script. Just make sure that you've saved any changes you made to the script in the meantime. Aside from the reload issue, I think all of your issues have been because you don't yet have a good grasp of Python. I'd highly recommend working your way through a couple of python tuturials. Just do a net search on "python tutorial" and you should see a bunch. Here's the one for Python 2.5 on the python doc site, it's a good one to start with.
_________________ Jay (Indigo Support)
|
| Mon Dec 19, 2011 12:06 pm |
|
|