Page 1 of 1

How to fire an event in plugin.py?

PostPosted: Sun Dec 29, 2019 2:25 pm
by ZachBenz
Apologies for the fairly basic question, but I can't seem to track down an answer in the SDK docs or searching in this forum.

I've defined an event in Events.xml:

Code: Select all
<?xml version="1.0"?>
<Events>
    <Event id="videoDownloadCompleteEvent">
        <Name>Video Download Complete</Name>
    </Event>
</Events>


How do I fire off this event in plugin.py? I assume it is a call along the lines of indigo.event.fire, but can't find the correct syntax.

Thanks in advance!

Re: How to fire an event in plugin.py?

PostPosted: Sun Dec 29, 2019 3:14 pm
by FlyingDiver
Code: Select all
                        indigo.trigger.execute(trigger)

Re: How to fire an event in plugin.py?

PostPosted: Sun Dec 29, 2019 3:17 pm
by ZachBenz
FlyingDiver wrote:
Code: Select all
                        indigo.trigger.execute(trigger)


Thanks! So I take it I need to create a trigger - is there handy sample code for what that looks like?

Re: How to fire an event in plugin.py?

PostPosted: Sun Dec 29, 2019 3:28 pm
by matt (support)
The user creates/defines the Trigger instances in Indigo (which exposes your "Video Download Complete" trigger type in the UI). Normal flow then is to define both of these to track when Triggers are active/enabled in Indigo's UI:

Code: Select all
   def triggerStartProcessing(self, trigger):
      if trigger.pluginTypeId == "videoDownloadCompleteEvent":
         self.activeDownloadCompleteTriggers[trigger.id] = trigger

   def triggerStopProcessing(self, trigger):
      if trigger.pluginTypeId == "videoDownloadCompleteEvent":
         del self.activeDownloadCompleteTriggers[trigger.id]

Then internally when a download is complete you can traverse your self.activeDownloadCompleteTriggers dict and call indigo.trigger.execute() on each value (trigger). So basically, you manage which triggers you are interested in manually and then execute them whenever those trigger conditions are internally met in the plugin (download complete in this case). You can store all of your plugin triggers in a single list/dict and figure out which ones "match" the given event, or have specific list/dicts for each trigger type (model I illustrated above).

Re: How to fire an event in plugin.py?

PostPosted: Sun Dec 29, 2019 3:54 pm
by ZachBenz
matt (support) wrote:
The user creates/defines the Trigger instances in Indigo (which exposes your "Video Download Complete" trigger type in the UI). Normal flow then is to define both of these to track when Triggers are active/enabled in Indigo's UI:


Thanks, Matt - I had it backwards in my mind (that I would be publishing an event, vs. handling events in terms of subscribed triggers)

Re: How to fire an event in plugin.py?

PostPosted: Sun Dec 29, 2019 3:56 pm
by FlyingDiver
ZachBenz wrote:
Thanks, Matt - I had it backwards in my mind (that I would be publishing an event, vs. handling events in terms of subscribed triggers)


Yeah, that is a little confusing. Only the plugin code knows if the event criteria were met in order to "fire" (execute) the trigger. So the plugin has to manage maintaining a list of active triggers, and check the list every time something happens that COULD fire a trigger.

Re: How to fire an event in plugin.py?

PostPosted: Sun Dec 29, 2019 4:48 pm
by ZachBenz
matt (support) wrote:
Then internally when a download is complete you can traverse your self.activeDownloadCompleteTriggers dict and call indigo.trigger.execute() on each value (trigger).


And I just discovered the Triggers documentation in the Object Model Reference (now that I'm not searching on "Event" :D)