Use Python to change the script a trigger executes
Posted: Tue Feb 02, 2016 5:09 pm
First, a disclaimer. This:
1) uses an unsupported Python library, appscript,
2) to access some "legacy" functionality from a previous version of Indigo,
3) in order to accomplish something that hopefully will be built into a future version of Indigo.
But it is saving me time and effort and I'm not going to mind discarding it when #3 happens.
If you have a lot of devices and you want each one to have an associated schedule or trigger that runs a python script, and the python script needs the device number, there's no good way to get it there other than a lot of manual copying and pasting, or if you have a large number of action groups with python scripts that you need to pass a parameter into, currently the only way to do that is with a variable so you need a variable for each action group, more copying and pasting. Or if like me, you find yourself wanting to create a whole family of triggers for a large number of possible events, and leave them all disabled and have a python script that figures out which one to turn on, that means a lot of copy trigger, modify trigger, rinse, repeat, when wouldn't it just be easier to have the script build the triggers for you as needed?
The access to triggers in the IOM right now is limited and doesn't let you get at the actions attached to the trigger from Python. But I had noticed that Indigo Server's Applescript dictionary does include the actions attached to triggers, schedules and action groups. So I wrote an Applescript to change the Python script attached to one of my triggers and it worked! But I don't want to write anything in Applescript if I don't absolutely have to, and appscript theoretically lets you access anything in Applescript from Python, so I experimented with appscript until I found a working formula.
Here is my Python function for copying and editing triggers and optionally setting them up to automatically delete themselves after they execute. In order for it to work you need to create a trigger with one action that runs a Python script, called "trigger prototype". Mine is a Device State Becomes Equal To trigger and my function also changes the device id and state value.
Every time this runs it makes Indigo spit out an error to the log that is apparently the result of it feeding Python code to an Applescript syntax checker. But that doesn't stop it from working.
The Python scripts of schedules and action groups should also be accessible:
1) uses an unsupported Python library, appscript,
2) to access some "legacy" functionality from a previous version of Indigo,
3) in order to accomplish something that hopefully will be built into a future version of Indigo.
But it is saving me time and effort and I'm not going to mind discarding it when #3 happens.
If you have a lot of devices and you want each one to have an associated schedule or trigger that runs a python script, and the python script needs the device number, there's no good way to get it there other than a lot of manual copying and pasting, or if you have a large number of action groups with python scripts that you need to pass a parameter into, currently the only way to do that is with a variable so you need a variable for each action group, more copying and pasting. Or if like me, you find yourself wanting to create a whole family of triggers for a large number of possible events, and leave them all disabled and have a python script that figures out which one to turn on, that means a lot of copy trigger, modify trigger, rinse, repeat, when wouldn't it just be easier to have the script build the triggers for you as needed?
The access to triggers in the IOM right now is limited and doesn't let you get at the actions attached to the trigger from Python. But I had noticed that Indigo Server's Applescript dictionary does include the actions attached to triggers, schedules and action groups. So I wrote an Applescript to change the Python script attached to one of my triggers and it worked! But I don't want to write anything in Applescript if I don't absolutely have to, and appscript theoretically lets you access anything in Applescript from Python, so I experimented with appscript until I found a working formula.
Here is my Python function for copying and editing triggers and optionally setting them up to automatically delete themselves after they execute. In order for it to work you need to create a trigger with one action that runs a Python script, called "trigger prototype". Mine is a Device State Becomes Equal To trigger and my function also changes the device id and state value.
Every time this runs it makes Indigo spit out an error to the log that is apparently the result of it feeding Python code to an Applescript syntax checker. But that doesn't stop it from working.
- Code: Select all
from __future__ import unicode_literals
import appscript
def make_trigger(device_id, state_value, python_code, temporary=False):
trig = indigo.trigger.duplicate("trigger prototype")
trigfolder = next((f for f in indigo.triggers.folders
if f.name == "Script-Generated Triggers"), None)
if trigfolder is not None:
indigo.trigger.moveToFolder(trig.id, trigfolder.id)
trig.deviceId = device_id
trig.stateValue = state_value
trig.replaceOnServer()
if temporary:
python_code = python_code + "\nindigo.trigger.delete({0})\n".format(trig.id)
isapp = appscript.app("IndigoServer")
isapp.trigger_actions[trig.name].action_steps[1].script_code.set(python_code)
indigo.trigger.enable(trig.id, value=True)
return trig
The Python scripts of schedules and action groups should also be accessible:
- Code: Select all
isapp.time_date_actions[name].action_steps[1].script_code.set(python_code)
isapp.action_groups[name].action_steps[1].script_code.set(python_code)