Wednesday, July 2, 2014

Phillips Hue: Python API

I recently got myself some of those Phillips Hue lightbulbs and started with a little hobby project of mine in order to become better at programming.

I present to you the Hue Python API 0.1

At the moment the implementation allows you to do basic things like turning lights on and off or changing their colours.

Get started

Here is how to get it:

Head over to my github repository and download the module. Once you imported the module you first have to create an instance of your bridge.

In order to access your lights you have first find the bridge and then create a user for it. With this user we will execute all commands later on. This is pretty straightforward.

When creating a first time user, the API will tell you to press the link button on your bridge and execute the function again within the next 30 seconds. If for some reason API can't find your bridge, you can also set it manually (might be the case with very old firmware). Same goes for the user, we dont have to create it everytime we execute our code. You can do that like this:

The only thing left to do now is to tell the bridge to look for your lights. You can use this function Now we can start with the fun stuff. The bridge holds all our lights in a list. Access it like this In my case this returns this
[Light: ID: 1 NAME: Kitchen, Light: ID: 3 NAME: Entrance, Light: ID: 2 NAME: Bedroom] 

Simple examples to control lights

Turning lights on and off
Light intensity
Accepts a float between 0.0 and 1.0

Light saturation
Accepts an integer between 0 and 255

Light color
This is a tricky one, the bulbs have a limited color range. However I implemented a method that allows you to assign it an RGB value (which will never be correct, but its an approximation. I am still working on improving this in the future, but for the moment its alright. Here is how you do that (in this case the light will be green)

Alert a light
This will flash the light quickly

This will put the light in a constant color cycle There are a few more things in the API and a whole lot more things are planned for the next version. Have a look through the methods and if you want to keep up to date, check my github page every now and then. If you have any feedback, hints, or wishes, please let me know.