Emulating a Philips Hue light

Remote Control

Over the past couple of years I’ve been building out an Alexa skill for my media center. So now I can say things like “Alexa, tell media to play music”. That will turn my receiver on, switch it to the Mac input, and start iTunes playing. Similarly “Alexa, tell media to switch to TiVo”, “Alexa tell media to pause”.

The backend code running on the Mac asks the receiver what input is selected (TiVo, Mac, BluRay…) and if on the Mac it works out what application has focus (iTunes, DVD Player, Kodi, …) so when a command such as “pause” is received then it knows how to send the appropriate action.

This works pretty well. But there’s a problem. Skill actions can’t (currently, anyway) be used in routines. So I can not say “Alexa, I’m home” and have it turn on the lights and start playing music.

However, home automation devices can be used in routines. Since I have a Philips Hue setup (“Alexa lights on”) I wondered if I could create fake lights and have those lights map to actions.

Looking around I found a number of Hue emulators; some in python, some in perl, Java, Go… However they were all “monoliths”. They came with a core library and then you added your own routines (“create this light”). None of them really worked the way I wanted.

My plan was to have a “bridge” routine. This would handle all the low level API layer stuff (UPNP discovery, Hue API, etc) but wouldn’t do any real work itself. Instead there would be a child process that would do the hard work. This could be written in any language (e.g. bash) and would communicate with the bridge via stdio, using a very simple set of primitives.

So now when the virtual light was turned on the bridge code would send a “Turn on” message to the child, which can take any action it needs to, and send a response back. The bridge caches the response so that status requests can be handled quickly, but the child can send updates whenever it likes.

This now allows the real world to be modelled. If I turn the volume up on the receiver then the child can send an updated “brightness” value to the bridge, and anyone using the Hue API to get the status will see the new value. Similarly lights could be linked; if I switch to BluRay input then that virtual light would be turned on, and the Mac, TiVo lights automatically turned off.

The Alexa app makes it seem like a normal bulb with brightness

Bulb

The resulting code is HueBridge. I wrote the bridge code in Go. There’s a simple shell script example (test-media) that shows how this might be used to control a media system.

Using this code I’ve also created a virtual “Christmas Music” light. So in a month’s time I can say “Alexa, it’s Christmas!” and it’ll turn on the tree lights (via a TP-Link power switch) and start playing Slade’s “Merry Christmas Everyone”.

Now that’s a good use for Smart Home technology!