Google Search

Custom Search

Powered by...

Find us on Facebook

Friday, 6 June 2014

XBMC OpenElec / Logitech Harmony / Micasaverde Vera integration – PART2

In part one here I demonstrated my method of using a Logitech Harmony universal remote control to send keyboard shortcut commands to my XBMC PC. The XBMC PC then runs a python script to send a http request to Vera the Home automation controller, to do something like turn on / off lights or to control a device in my home.
In Part 2 I am adding some feedback in to the XBMC user interface. For example if the light or device you want to control is not in the same room as you, you don’t really see any feedback that the script file has been run? and that the light or device has been controlled?

To try and eliminate this lack of feedback somewhat, I will be demonstrating adding XBMC pop-up notifications upon each run of the python scripts.

I’d like to thank @Montellese on the XBMC forum, for his assistance with the Unicode stuff and for pointing me in the right direction for getting this working.

To give you an idea what I am talking about, here are some screen shots of the pop-up notifications in XBMC.

“Harrison Home” is the title and “Turning Lounge Lights On” is the message

image

image

image

OK so now you get the idea if you’d like to add this functionality we need to edit our python script files that were created in part 1.

But first ensure you have the web server turned on in XBMC or you wont be able to send the notifications to your XBMC PC.

Go into XBMC Settings – Network – Webserver and ensure it is enabled and also make a note of the port number, I changed mine to 8080. The username is XBMC and I have not set any password.

image

You also need to know the IP address of your XBMC PC? I recommend you set a static IP address on your XBMC PC.

I also recommend using Notepad++ text editor as these http commands for the XBMC notifications needs to be in Unicode so we can run them using curl in the python script file.

Open your first python script file to be edited

Below is my Lounge-On.py file, you can see on line 3: I have the http command which is sent to Vera to turn on the lights this was created in part 1.
We need to add a new line of code (line 4) into our python script file, to also now send a popup notification to the XBMC PC as well.

So we need to construct the line of code to send the popup to the XBMC PC and this is done in two parts.

The first part of this line is always the same and can be seen below. We’ll say the IP address of my XBMC PC is 192.168.1.10 and the webserver is on port 8080

Line #4 – First Part
proc = subprocess.Popen(["curl", "http://192.168.1.10:8080/jsonrpc?request="])

The second part of the line is the bit that needs to be encoded into Unicode and we will use a website to assist us with this: http://www.url-encode-decode.com/

image

Above is a screen shot of the website, on the box on the left enter this code:
{"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"Your Title Here","message":"Your Message Here"},"id":1}
Change the Title and Message to suit your needs, select UTF-8 from the drop down list and then click the URL Encode button.

Then on the box on the right we get the encoded string that we need.

Line #4 – Second Part
%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22GUI.ShowNotification%22%2C%22params%22%3A%7B%22title%22%3A%22Harrison+Home%22%2C%22message%22%3A%22Turning+Lounge+Lights+On%22%7D%2C%22id%22%3A1%7D

Now we need to combine the first and second parts of the Line #4 in our python script file.

proc = subprocess.Popen(["curl", "http://192.168.1.10:8080/jsonrpc?request=ENTER-ENCODED-STRING-HERE"])

The fully combined line of code will then look like this:
proc = subprocess.Popen(["curl", "http://192.168.1.10:8080/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22GUI.ShowNotification%22%2C%22params%22%3A%7B%22title%22%3A%22Harrison+Home%22%2C%22message%22%3A%22Turning+Lounge+Lights+On%22%7D%2C%22id%22%3A1%7D"])

So now the fully completed Lounge-On.py script file looks like this:
#!/usr/bin/env python
import subprocess
proc = subprocess.Popen(["curl", "http://192.168.1.100:3480/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=1"])
proc = subprocess.Popen(["curl", "http://192.168.1.10:8080/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22GUI.ShowNotification%22%2C%22params%22%3A%7B%22title%22%3A%22Harrison+Home%22%2C%22message%22%3A%22Turning+Lounge+Lights+On%22%7D%2C%22id%22%3A1%7D"])

Click the links to download my sample Lounge-On.py and Lounge-Off.py scripts to have a look at them.

All of this might sound a bit complicated but its actually pretty simple to create these new lines of code to also send a pop-up notification to your XBMC PC, once you have done it a few times.

Now you need to repeat this entire process and add a new line of code into each of your existing python script files you created in part one changing the pop-up notification message for each.

Now when I click these buttons on my Harmony remote control handset to turn on /off my lights or fireplace etc I also get a feedback notification pop-up being displayed in XBMC, so I know the python script has been run and can therefore safely assume that the light or device I wanted to send the command too via Vera has received it.



Summary

A neat little way of adding some feedback notifications to my Logitech Harmony / XBMC / Vera Home Automation integration project.

UPDATE:

I migrated from XBMC Gotham to Kodi Helix, there seems to be some differences with the notifications in Kodi.

For example now when Sickbeard auto sends a notification to Kodi I see the Sickbeard logo image next to the notification message, this never happened in XBMC.

Then if after this I run one of my Vera commands the Sickbeard logo is still being displayed next to my Vera notifications, I have no idea why its doing this?

A fix / workaround: So I thought maybe I should update my code a bit to include and display my own icon image.

The directory on my OpenElec HTPC where all these python scripts are located is:

/storage/.kodi/userdata/vera

So I just put an icon of a house image file in this directory. To have this house.png image displayed in the popup notification this JSON code seems to work:

{"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"Your Title Here","message":"Your Message Here","image":"/storage/.kodi/userdata/vera/House.png"},"id":1}

I then had to encode it as per the instructions above before editing the python scripts to update them all with this change.

I am using a new Kodi skin called 1080XF but this is how the notifications now look with the new house image next to them.




1 comment:

Anonymous said...

Great site and post. I am send a similar alert to Kodi, but to make the process more flexible, I am writing a script to pass a variable message, ie. $msg. The problem is, although I can send text as a notification, when I try to use $msg, it shows up as "$msg" (the name of the variable instead of the value). I tested with echo $msg to make sure the value is stored, and it is... Any ideas? Your help would be greatly appreciated.