XBMC add-on for Philips Hue

Note: this post old, see meethue.wordpress.com for the latest information.

Version 0.1 of the XBMC add-on is available on GitHub! Make sure you install the Python PIL library first (http://www.pythonware.com/products/pil/)

Download script.xbmc.hue at GitHub

It supports two modes of operations: “Theatre mode” and “Ambilight mode”.

plugin2

The “Theatre mode” is fully functional. It will dim the lights when a movie starts playing, and turn them back on once stopped. The “Ambilight mode” needs some more work. The biggest issue is that it takes approximately 1 second to adjust the lights. The time is spent by the bridge, and I don’t see any quick way to improve it. For now – “Ambilight mode” works but will be out of sync with the video streams for at least 1 second. [update 2013-03: a new version of the add-on is available on github and has better response times]

Download script.xbmc.hue at GitHub

If you have any feedback, please leave a reply!

26 thoughts on “XBMC add-on for Philips Hue

  1. Damian

    Having an issue with the addon. Excerpt from my debug log: XBMC Frodo RC3 / Windows 7 64bit

    Error Type:
    Error Contents: No module named pil
    Traceback (most recent call last):
    File “C:\Users\Damian\AppData\Roaming\XBMC\addons\script.xbmc.hue\default.py”, line 9, in
    from pil import Image
    ImportError: No module named pil
    –>End of Python script error report<–

    Reply
  2. Oskar Nijs

    Hi,
    Great initiative!! Was really looking for something like this.

    I believe the PIL library is available, at least on XBMCbuntu 11.0 Eden:

    >>> import Image
    # /usr/lib/python2.7/dist-packages/PIL/Image.pyc matches /usr/lib/python2.7/dist-packages/PIL/Image.py
    import Image # precompiled from /usr/lib/python2.7/dist-packages/PIL/Image.pyc
    … etc.

    >>> import _imaging
    dlopen(“/usr/lib/python2.7/dist-packages/PIL/_imaging.so”, 2);
    import _imaging # dynamically loaded from /usr/lib/python2.7/dist-packages/PIL/_imaging.so

    When I changed line 9: “from pil import Image” to “from PIL import Image” the plugin didn’t crash and I was able to run configure, but after the bridge was discovered the plugin crashed. Got the following from the log:

    ——
    13:30:12 T:3060788080 ERROR: Error Type:
    13:30:12 T:3060788080 ERROR: Error Contents:
    13:30:12 T:3060788080 ERROR: Traceback (most recent call last):
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/default.py”, line 203, in
    Hue()
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/default.py”, line 61, in __init__
    self.test_connection()
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/default.py”, line 85, in test_connection
    if not test_connection(settings.bridge_ip, settings.bridge_user):
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/resources/lib/tools.py”, line 67, in test_connection
    response = urllib2.urlopen(‘http://%s/api/%s/config’ % (bridge_ip, bridge_user))
    File “/usr/lib/python2.7/urllib2.py”, line 126, in urlopen
    return _opener.open(url, data, timeout)
    File “/usr/lib/python2.7/urllib2.py”, line 394, in open
    response = self._open(req, data)
    File “/usr/lib/python2.7/urllib2.py”, line 412, in _open
    ‘_open’, req)
    File “/usr/lib/python2.7/urllib2.py”, line 372, in _call_chain
    result = func(*args)
    File “/usr/lib/python2.7/urllib2.py”, line 1201, in http_open
    return self.do_open(httplib.HTTPConnection, req)
    File “/usr/lib/python2.7/urllib2.py”, line 1171, in do_open
    raise URLError(err)
    URLError:
    ——

    Hope this is helpful for you.

    Cheers,
    Oskar

    Reply
  3. Oskar Nijs

    Small update, after a reboot I noticed the Ambiight function is working. However, I only see Light 1 to Light 3 in the setup, while I would like to set the Ambilight function on light 5.

    Anyway, keep up the good work!

    Reply
  4. meethue Post author

    Hello Oskar,

    Thanks for the feedback.

    – I improved the import of the PIL library, it now checks for pil and PIL. If none are found, it shows an error in the XBMC interface
    – When using auto-disvory, the settings were not updated correctly. I fixed this in the code

    I updated the code on GitHub.

    The add-on does not check how many lights are available. It assumes there are 3. A (hacky) work-around is to dive into the code. If you edit the file script.xbmc.hue/default.py, on line number 123 (or line number 113 if you’re using yesterday’s version), change “lights.append(1)” to “lights.append(5)”. The configuration for light 1 will now be used for number 5.

    Enjoy! Let me know if it works for you ๐Ÿ™‚

    Reply
  5. Oskar Nijs

    Hi Cees,

    Thanks for the quick response. I installed the new code and set the light to number 5, it’s working.

    Did encounter some other things:
    – Unchecking light 2 and 3 in configure did not work, they still were being used. (I decided to remove them from settings.xml, which fixed the issue for me)
    – Is there a way to adjust the brightness? It seems it’s always 100%?
    – Upon getting a subtitle, and the video being paused I received an error:
    ——
    16:49:49 T:3041917808 ERROR: Error Contents: ”
    16:49:49 T:3041917808 ERROR: Traceback (most recent call last):
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/default.py”, line 214, in
    Hue(settings)
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/default.py”, line 81, in __init__
    self.run()
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/default.py”, line 141, in run
    set_light2(self.settings.bridge_ip, self.settings.bridge_user, light, h, s, v)
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/resources/lib/tools.py”, line 97, in set_light2
    request_url_put(“http://%s/api/%s/lights/%s/state” % (bridge_ip, bridge_user, light), data=data)
    File “/home/xbmc/.xbmc/addons/script.xbmc.hue/resources/lib/tools.py”, line 82, in request_url_put
    url = opener.open(request)
    File “/usr/lib/python2.7/urllib2.py”, line 394, in open
    response = self._open(req, data)
    File “/usr/lib/python2.7/urllib2.py”, line 412, in _open
    ‘_open’, req)
    File “/usr/lib/python2.7/urllib2.py”, line 372, in _call_chain
    result = func(*args)
    File “/usr/lib/python2.7/urllib2.py”, line 1201, in http_open
    return self.do_open(httplib.HTTPConnection, req)
    File “/usr/lib/python2.7/urllib2.py”, line 1174, in do_open
    r = h.getresponse(buffering=True)
    File “/usr/lib/python2.7/httplib.py”, line 1027, in getresponse
    response.begin()
    File “/usr/lib/python2.7/httplib.py”, line 407, in begin
    version, status, reason = self._read_status()
    File “/usr/lib/python2.7/httplib.py”, line 371, in _read_status
    raise BadStatusLine(line)
    BadStatusLine: ”
    16:50:00 T:10311232 ERROR: ClearBlock: Trying to clear non existent block /home/xbmc/.xbmc/addons/script.xbmc.subtitles
    16:50:24 T:10311232 ERROR: Control 30 in window 10124 has been asked to focus, but it can’t
    ——

    Reply
  6. meethue Post author

    Hello Oskar,

    The brightness is a quick fix: in scripts.xbmc.hue/resources/lib/tools.py, on lin 93, replace “bri: 254,” with “bri: bri,”. I disabled the effect because of the delay in controlling the lights. In my experience it’s a bit distracting if the movie turn dark, and the lights are on with full brightness and dim 1 second later.

    If you disable a light in the settings panel (or any other settings, except auto-discovery), you’ll have to restart XBMC before they are effective…

    I’ll get back to you on the other issue. I don’t think it’s related to the subtitles. It appears the bridge replied with an error.

    Reply
  7. Damian

    I have installed python and pil, but still getting unable to import pil. Downloaded python 2.7.3 and PIL 1.1.7. Any ideas for a windows user?

    Reply
  8. Bruno

    I’ve got it to work on Windows 7 and XBMC 12.
    Python is included in the XBMC, so you don’t need to install it.
    For PIL: I download v1.1.7, and opened the .exe with 7-Zip, to extract only the library without installing it. (it’s in PLATLIB\PIL\). Then place that PIL folder in your XBMC installation (C:\Users\Bruno\AppData\Roaming\XBMC\system\python\Lib).
    It should now work.
    Or if you used the PIL installer, you would have to copy the PIL folder to the XBMC lib folder.
    (Might not be the best method, but worked for me. I didn’t have python and PIL installed.)

    Great plug-in. Still needs some work: quicker reaction for ambilight mode, adjust brightness. But the idea is great. I was looking exactly for that kind of plug-in…

    Reply
  9. Rasmus

    Hi,

    First off, I love your script ๐Ÿ™‚
    I have some questions / requests.
    In theatre mode, I would like to turn on the lights when I pause a movie and turn them off when I unpause. But it seems to me that you are not able to catch the onPlayBackPaused and onPlayBackResumed events in your script due to the way you wrote it. (I tried to hack it a bit myself, but I am not a big Python / XBMC developer).
    Do you have any ideas on how to catch those events in your code?

    Reply
  10. meethue Post author

    Good ๐Ÿ™‚

    You’re right, the current implementation is no good to handle those events. The proper way to go is to subclass XBMC.Player, and handle the callback events.

    The code would like something like:

    class MyPlayer(xbmc.Player):
    def __init__(self):
    xbmc.Player.__init__(self)

    def onPlayBackStarted(self):
    pass # handle start

    def onPlaybackPaused(self):
    pass # handle pause

    The callback function should be there. See this issue report in TRAC: http://trac.xbmc.org/ticket/8548. I tried it before but somehow the function were never called, but didn’t really look into it.

    Hope this will get you started ๐Ÿ˜‰

    Reply
  11. Rasmus

    I managed to get that far myself ๐Ÿ™‚
    I also tried to add it to your script, but as you mention the functions never gets called.
    I made a “dummy script” and added it to autoexec.py and the functions are called. I think, that for now, I will do a lightweight static implementation communicating directly to the API via a script in autoexec.py, handling the pause events ๐Ÿ™‚ And hopefully you will someday find the time to look into implement it in your script ๐Ÿ˜‰

    Reply
  12. spike (@stripealipe)

    Thanks Bruno,

    I had it kind of working on Raspbmc, but it was ropey. Now that i’ve copied the PIL folder over from it’s initial installation and into the XBMC, it seems to work in Theatre mode. Ambilight still not working for me though. The lights fade up, then down, and then stay there, white.

    Not sure if it’s the installation (probably) or the underpowered Raspberry PI?

    Anyways, thanks for the initial script. This would be great if developed some more.
    Capturing the current state of the lights, and then fading down from there to a preset level and shade would be ace.
    The ambilight i can see less use for!

    Cheers!

    Reply
  13. meethue Post author

    Spike, I don’t think ambilight will work on a Raspberry Pi because the way it renders its video.

    Thanks for the feedback. I’m contemplating on releasing a “theatre mode” only version. You’re right that the ambilight isn’t very useful in the current state. Also, without the ambilight, the add-on doesn’t on depend PIL and would work out of the box. And with some little extras (fading from the current setting and turn them back on when paused) it would be kinda production ready ๐Ÿ™‚

    Reply
  14. Nitehawk

    I get the following error after installing hue for xbmc:

    ERROR: EXCEPTION Thrown (PythonToCppException) : –>Python callback/script returned the following error<–
    – NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
    Error Type:
    Error Contents: list indices must be integers, not str
    Traceback (most recent call last):
    File “C:\Users\nitehawk\AppData\Roaming\XBMC\addons\script.xbmc.hue\default.py”, line 239, in
    hue = Hue(settings, args)
    File “C:\Users\nitehawk\AppData\Roaming\XBMC\addons\script.xbmc.hue\default.py”, line 81, in __init__
    self.update_settings()
    File “C:\Users\nitehawk\AppData\Roaming\XBMC\addons\script.xbmc.hue\default.py”, line 142, in update_settings
    self.light = Light(self.settings.bridge_ip, self.settings.bridge_user, self.settings.light)
    File “C:\Users\nitehawk\AppData\Roaming\XBMC\addons\script.xbmc.hue\resources\lib\tools.py”, line 75, in __init__
    self.get_current_setting()
    File “C:\Users\nitehawk\AppData\Roaming\XBMC\addons\script.xbmc.hue\resources\lib\tools.py”, line 85, in get_current_setting
    “on”: j[‘state’][‘on’],
    TypeError: list indices must be integers, not str
    –>End of Python script error report<–

    Am I missing something?

    Hope anyone can help me, would love to play around with hue and xbmc

    Reply
  15. Tobias Borgen Nielsen

    Great work! ๐Ÿ™‚

    A few questions regarding functionality. I have installed the theatre mode script.

    If my lightbulbs are switched off when I start a movie the bulbs will switch on. Is there a way to check the status and not do this?

    I have bought the starter kit + 4 single bulbs. It would be great to choose a group of bulbs instead of just one or all. 4 bulbs are located in other rooms and should not be affected by XBMC.

    Thanks again – great addon for the hue bulbs ๐Ÿ™‚

    /Toby

    Reply
    1. meethue Post author

      Tobias, great to hear you enjoy the add-on.

      > If my lightbulbs are switched off when I start a movie the bulbs will switch on. Is there a way to check the status and not do this?

      Do you mean when you stop a movie the lights will switch on? That would make more sense. When stopping or pausing a movie, it sends a message to the bridge to put all lights in “group 0” (the group for all lights) on. If a light is turned off it will probably switch back on, even though if was off before playback started.

      There’s no quick fix. The plugin doesn’t know if the lights are turned on or off, it only communicates to the group. I think the best way would be to support different groups then “group 0” in the add-on, just like you mentioned. I might take a look into that.

      Reply
  16. Carl Nardell

    Does the PIL library have to be built prior to installing on OS X? I get some errors when I try to install it using the Python instructions. I got the extension to install into XBMC, and the Ambilight mode appears to be there.

    Reply
  17. mithrila

    Thanks for the great work!!! this is much appreciated.

    I uptaded the script from v0.3.2 to v 0.4.1 today and i can not seem to get it running. ๐Ÿ˜ฆ
    Am i overlooking somthing obvious?

    ERROR: EXCEPTION Thrown (PythonToCppException) : –>Python callback/script returned the following error<–
    – NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
    Error Type:
    Error Contents: (‘hue’,)
    Traceback (most recent call last):
    File “C:\Users\HTPC\AppData\Roaming\XBMC\addons\script.xbmc.hue.ambilight\default.py”, line 291, in
    hue = Hue(settings, args)
    File “C:\Users\HTPC\AppData\Roaming\XBMC\addons\script.xbmc.hue.ambilight\default.py”, line 86, in __init__
    self.update_settings()
    File “C:\Users\HTPC\AppData\Roaming\XBMC\addons\script.xbmc.hue.ambilight\default.py”, line 149, in update_settings
    self.settings.undim_hue,
    File “C:\Users\HTPC\AppData\Roaming\XBMC\addons\script.xbmc.hue.ambilight\resources\lib\tools.py”, line 163, in __init__
    undim_hue
    File “C:\Users\HTPC\AppData\Roaming\XBMC\addons\script.xbmc.hue.ambilight\resources\lib\tools.py”, line 85, in __init__
    self.get_current_setting()
    File “C:\Users\HTPC\AppData\Roaming\XBMC\addons\script.xbmc.hue.ambilight\resources\lib\tools.py”, line 103, in get_current_setting
    “hue”: j[‘state’][‘hue’],
    KeyError: (‘hue’,)
    –>End of Python script error report<–

    Reply

Leave a reply to Moss Palmer Cancel reply