Python - Create Event

If you have a question or need help, this is the place to be.

Python - Create Event

Postby ijourneaux » Sat Apr 09, 2011 10:13 pm

I have a python application where I would like to generate an event that EventGhost could be configured to react to. I found a reference to TriggerEvent but wasn't able to come up with a function form that would work.

Would any one beable to point me in the correct direction.

Appreciate any help
ijourneaux
Experienced User
 
Posts: 94
Joined: Wed Feb 17, 2010 2:20 am

Re: Python - Create Event

Postby Pako » Sun Apr 10, 2011 6:18 am

Try this script:
Code: Select all
from win32com.client import Dispatch
eg = Dispatch("EventGhost")
eg.TriggerEvent(u"EventFromMyApplication")
eg.TriggerEvent(u"EventWithPayload", 12345)

Pako
User avatar
Pako
Plugin Developer
 
Posts: 1795
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic

Re: Python - Create Event

Postby ijourneaux » Sun Apr 10, 2011 2:56 pm

It appears that this creates a connection to EventGhost and sends the trigger directly to eventhost. I suppose this would show up in the EventHost log and from there I could create a EventChost Macro that would execute if the event was received. That should work but as I was thinking about how to implement it I realized I should ahve provided more info on what I was hoping to do.

Within XBMC, there is a script called Home Cinema Experience. This script allows you to create a more encompassing theater experience with first presenting some Trivia, then some trailers and finally the movie you had selected.

One of the more requested features is to tie that script in with a home automation system to dim the lights, close blinds, etc. at the appropriate times in the script. The developer of the script has added the code that allows the end user to write custom code for his home automation system.

Here is a code snippet

Code: Select all
def activate_on( trigger = "None" ):
    """
        Scripting to trigger almost anything(HA, other scripts, etc...) when videos start. 
       
        Usage:
            activate_on( "Movie" )
            will trigger code that is set under the Movie heading.
           
    """
    if trigger == "None":
        xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - No Trigger Sent, Returning", level=xbmc.LOGNOTICE )
        return
    xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - activate_on( %s ) Triggered" % trigger, level=xbmc.LOGNOTICE )
    # Script Start
    if trigger == _L_( 32613 ) and _S_( "ha_script_start" ) == "true":
        xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - %s Triggered" % _L_( 32613 ), level=xbmc.LOGNOTICE )
        # place code below this line
    # Trivia Intro
    elif trigger == _L_( 32609 ) and _S_( "ha_trivia_intro" ) == "true":
        xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - %s Triggered" % _L_( 32609 ), level=xbmc.LOGNOTICE )
        # place code below this line
    # Trivia
    elif trigger ==_L_( 32615 ) and _S_( "ha_trivia_start" ) == "true":


My thought was to generate events in each of these "triggers". That way the events could be observed by a home control software (I use EventGhost but I suppose people could use HomeSeer or other automation software). I am guessing this would require a plugin in Eventghost to recieve the event.
ijourneaux
Experienced User
 
Posts: 94
Joined: Wed Feb 17, 2010 2:20 am

Re: Python - Create Event

Postby Livin » Sun Apr 10, 2011 7:30 pm

With EG you could receive those events in a few ways:

A) send an HTTP command from XBMC ... EG can trigger Macros based on what the EG web server receives - very easily.

B) use the EG Network Receiver plugin ... I think may require a specific format/port be sent, not sure.

I suggest trying option A first, as it is likely the easiest. If you get it working let me know, I'd like to do this also.
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
User avatar
Livin
Experienced User
 
Posts: 781
Joined: Wed Oct 08, 2008 4:56 am

Re: Python - Create Event

Postby ijourneaux » Sun Apr 10, 2011 8:22 pm

Would you have any examples of your recommended approach?
ijourneaux
Experienced User
 
Posts: 94
Joined: Wed Feb 17, 2010 2:20 am

Re: Python - Create Event

Postby Livin » Sun Apr 10, 2011 9:17 pm

the web server is well documented but here's one simple way...

Create a Macro you want to trigger
Inside that macro...
Click Configuration > Add Event
Name the Event - begin the name with "HTTP." e.g. "HTTP.Movie_start"

Create a new page, or use an existing one in the EG web folder... does not matter.

Send command to the EG web server "index.html?HTTP.Movie_start" ... or whatever your web page name is.
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
User avatar
Livin
Experienced User
 
Posts: 781
Joined: Wed Oct 08, 2008 4:56 am

Re: Python - Create Event

Postby giftie » Fri May 06, 2011 1:43 am

Actually this should be fairly simple to do(after getting XBMC Event Receiver up and running)

Even though I personally have not used EventGhost(at the moment, I don't have a reason to) I barely knew of it's existence in fact(I know, shame on me..)

But i have a solution for you that use Cinema Experience and want to integrate what I have provided with your Home Automation....

So here we go... :D

We will use HTTP-API(It is depreciated, but it should be around even for Eden still, if not, we'll conquer that when it happens)

When the script runs for the first time(I think since about version 1.0.40) the script will copy a folder and it's contents to your userdata\addon_data\script.cinema.experience folder. Inside the folder is a file called "home_automation.py". This file is the bread and butter of the integration. This file is a python script that Cinema Experience will trigger, for those who know programming, it is run as a Thread, so it does not interfere with Cinema Experiences timing, nor should it really affect CPU load. Here is were the fun coding begins(it's fairly simple)

Code: Select all
def activate_on( trigger = "None" ):
    """
        Scripting to trigger almost anything(HA, other scripts, etc...) when videos start. 
       
        Usage:
            activate_on( "Movie" )
            will trigger code that is set under the Movie heading.
           
    """
    if trigger == "None":
        xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - No Trigger Sent, Returning", level=xbmc.LOGNOTICE )
        return
    xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - activate_on( %s ) Triggered" % trigger, level=xbmc.LOGNOTICE )
    # Script Start
    if trigger == _L_( 32613 ) and _S_( "ha_script_start" ) == "true":
        xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - %s Triggered" % _L_( 32613 ), level=xbmc.LOGNOTICE )
        # place code below this line
    # Trivia Intro


This is a clip from the file. So we will start where it says "# place code below this line" - for those not familiar with python, '#' are comment lines

To get the information to EventGhost(EG from now on), we need the script to send a broadcast. The HTTP API is perfect for this. We will use the command xbmc.executeHTTPAPI() to create our broadcasts.

The broadcast we need to send from CE is something that we can easily identify and distinguish in EG. For this example we will use "CE_Automate".
We will also use <b> to start the broadcast and </b> to end the broadcast, as well as <li> to separate the trigger.

So each of the Event triggers will start with "CE_Automate", ie "<b>CE_Automate<li>script_start</b>", "<b>CE_Automate<li>trivia_start</b>"

Let's put it into the code.. But first a little tip about the indentation. Python really likes indentation, in fact it will not work and through a fit if the indentation is not correct. The script uses 4 spaces per indentation, these are also not tabs. The code we insert will need to line up with the other indented lines.

Here we really go...
Code: Select all
# Script Start
    if trigger == _L_( 32613 ) and _S_( "ha_script_start" ) == "true":
        xbmc.output( "[script.cinema.experience] - [ home_automation.py ] - %s Triggered" % _L_( 32613 ), level=xbmc.LOGNOTICE )
        # place code below this line
        xbmc.executehttpapi( "Broadcast(<b>CE_Automate<li>script_start</b>)" )


Now when ever the script starts, it broadcasts "<b>CE_Automate<li>script_start</b>".
Continue on, adding the triggers... or download this file - http://www.mediafire.com/file/zpx33v60i ... omation.py

Now we need to turn our attention to EG...

Install the XBMC Event Receiver plugin - viewtopic.php?f=10&t=2140&start=30#p18203 - follow aptalca's instructions if you don't have it working yet.

The triggers we need to look for are the messages that we have broadcasted from CE. The XBMC Event Receiver prefixes the broadcast with XBMC-Event. so the event we will be looking for looks like this: "XBMC-Event.<b>CE_Automate<li>script_start</b>"

So now create a Macro, use the "XBMC-Event.<b>CE_Automate<li>script_start</b>" as the Event, then add what you want EG to do when this Event happens.

That's it.... Not too painful :)

If the script sensed that the script has stopped playing(ie by pressing stop, video crash, etc) it will trigger the script_end trigger

The triggers in the file are the following:

Script Start - XBMC-Event.<b>CE_Automate<li>script_start</b>
Script End - XBMC-Event.<b>CE_Automate<li>script_end</b>
Trivia Intro - XBMC-Event.<b>CE_Automate<li>trivia_intro</b>
Trivia Start - XBMC-Event.<b>CE_Automate<li>trivia_start</b>
Trivia Outro - XBMC-Event.<b>CE_Automate<li>trivia_outro</b>
Movie Theatre Intro - XBMC-Event.<b>CE_Automate<li>movie_theatre_intro</b>
Coming Attraction Intro - XBMC-Event.<b>CE_Automate<li>coming_attractions_intro</b>
Trailer - XBMC-Event.<b>CE_Automate<li>trailer</b>
Coming Attraction Outro - XBMC-Event.<b>CE_Automate<li>coming_attractions_outro</b>
Feature Presentation Intro - XBMC-Event.<b>CE_Automate<li>feature_intro</b>
Countdown Video - XBMC-Event.<b>CE_Automate<li>countdown_video</b>
Audio Format Video - XBMC-Event.<b>CE_Automate<li>audio_video</b>
MPAA Rating Video - XBMC-Event.<b>CE_Automate<li>mpaa_rating</b>
Movie - XBMC-Event.<b>CE_Automate<li>movie_start</b>
Intermission - XBMC-Event.<b>CE_Automate<li>intermission</b>
Feature Presentation Outro - XBMC-Event.<b>CE_Automate<li>feature_outro</b>
Movie Theatre Outro - XBMC-Event.<b>CE_Automate<li>movie_theatre_outro</b>
Paused - XBMC-Event.<b>CE_Automate<li>paused</b>
Resumed - XBMC-Event.<b>CE_Automate<li>resumed</b>



**** NOTE *****

There is an error in the home_automation.py file. I've fixed the one here to be downloaded and in the source(ready for the next update)

If you want to fix the one you have it is quite easy.

line 48 reads:

Code: Select all
    # Trailer
    elif trigger ==_L_( 32605 ) and _S_( "" ) == "true":


it needs to be:

Code: Select all
    # Trailer
    elif trigger ==_L_( 32605 ) and _S_( "ha_trailer_start" ) == "true":
giftie
 
Posts: 2
Joined: Wed May 04, 2011 12:07 am

Re: Python - Create Event

Postby Livin » Sat Sep 10, 2011 10:11 pm

giftie

I'm just now getting time to want to set up the link between XBMC & EG so -- Is there a plan to add any other triggers?

I have a need to make it fully automated. Some of the things I'm looking for...

Alert EG when:
XBMC enters, and exits, specific sections: Music, Movies, TV Shows, etc.
XBMC starts/stops playing music
what audio encoding a video is (to enable correct mode on receiver)
... etc
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
User avatar
Livin
Experienced User
 
Posts: 781
Joined: Wed Oct 08, 2008 4:56 am


Return to General Support

Who is online

Users browsing this forum: No registered users and 4 guests