Start and Stop Hyperion using Python

Hyperion is an open source AmbiLight clone. Here are a couple of useful python scripts that can be called to start and stop the service. This serves as a convenient way to switch on or off the LED’s as required. These are currently saved on a Pi running OpenElec, so adjust the file paths as appropriate depending on your software setup.

Switch LEDs On

Save this file as 

/storage/hyperion/scripts/ledon.py

 

This will start the Hyperion process and reload the config.json file. Once loaded it will send a toast notification to OpenELEC to confirm that the process has been started. After 2 seconds it will then play the “Rainbow Swirl” effect to confirm the LEDs are working.

import subprocess
import time

subprocess.call('/storage/hyperion/bin/hyperiond.sh /storage/.config/hyperion.config.json </dev/null >/dev/null 2>&1 &', shell=True)

subprocess.call('curl -v -H "Accept: application/json" -H "Content-type: application/json" -d \'{"id":1,"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"Hyperion","message":"Enabled","image":"/storage/hyperion/hyperion.png"}}\' http://kodi:[email protected]:80/jsonrpc', shell=True)

time.sleep(2)

subprocess.call('/storage/hyperion/bin/hyperion-remote.sh --priority 100 --duration 400 --effect "Rainbow Swirl"', shell=True,)

Switch LEDs Off

Save this file as 

/storage/hyperion/scripts/ledoff.py

 

This will kill the Hyperion process and send a toast notification to OpenELEC to confirm that the process has been killed.

import subprocess

subprocess.call('killall hyperiond', shell=True)

subprocess.call('curl -v -H "Accept: application/json" -H "Content-type: application/json" -d \'{"id":1,"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"Hyperion","message":"Disabled","image":"/storage/hyperion/hyperion.png"}}\' http://kodi:[email protected]:80/jsonrpc', shell=True)

OpenHab Items Definition

I’ve currently got them as remotely executable via the Exec Binding so I can call them from any device capable of displaying the OpenHab control page. As per the guidance on the Exec Binding wiki page, you will need to first enable private key authentication on OpenElec first for this to work.

The required item definitions for OpenHab are as follows:

Switch XbmcBedroom_LEDsOff	"Turn LEDs Off"	{exec="ON:ssh root@openelec python /storage/hyperion/scripts/ledoff.py", autoupdate="false"}

Switch XbmcBedroom_LEDsOn	"Turn LEDs On"	{exec="ON:ssh root@openelec python /storage/hyperion/scripts/ledon.py", autoupdate="false"}

In addition to this they are also configured to be called by the coloured buttons on my remote control by adding the scripts to the Keymaps file in Kodi, as per the below snippet. This also doubles up as a convenient way to stop and restart the service when testing out new config.json files.

Kodi Remote Control Keymap

Edit or create the file

/storage/.kodi/userdata/keymaps/remote.xml

 

<keymap>
   <global>
     <remote>
        <red>RunScript("/storage/hyperion/scripts/ledon.py")</red>
        <green>RunScript("/storage/hyperion/scripts/ledoff.py")</green>
    </remote>
   </global>
</keymap>

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *