Controlling Hue Bulbs With WeMo Light Switch

Previously I wrote about controlling Philips Hue bulbs with a Harmony remote here:

I finally got around to installing a WeMo light switch to have a switch type device to control these lights.

It has to be wired as described here:

That is, you need to have power to the switch but not have the switch actually control the light fixture, the light fixture must have continuous power.

Once this is done, configure your new WeMo light switch in the WeMo smartphone app and give it a name.

I have a much nicer set of code than described here in a github repository here:

Now we can use a little daemon to detect switch activation, let's call it wemo_switch_daemon and put it in ~/bin.

#!/usr/bin/env python

import subprocess
from ouimeaux.environment import Environment
from ouimeaux.signals     import statechange, receiver

env = Environment();

switch = env.get_switch('rafael_living_wemo')

@receiver(statechange, sender=switch)
def switch_toggle(*args, **kwargs):
    if kwargs['state'] == 1:['~/bin/', 'on'],  shell=True)
    else:['~/bin/', 'off'], shell=True)


You'll need to install the Python library with:

pip install --upgrade ouimeaux

Make sure the daemon runs with no error, then add a crontab entry to launch it on startup, with e.g. crontab -e:

@reboot             exec ~/bin/wemo_switch_daemon >/dev/null 2>&1 &

On Mac you can use "LaunchD Task Scheduler" and making a job to run at "load and login." On Windows you'd use Task Scheduler.

If you are not using the system python, either change the shebang or prepend the full path to your python.

Here is the toggle_hue script:

#!/usr/bin/env python

import socket, os.path, sys
from phue import Bridge

bridge = Bridge('rafael_living_hue')

arg = sys.argv[1].lower() if len(sys.argv) > 1 else 'toggle'

    on_state = { 'on': True, 'off': False, 'toggle': not bridge.get_group(1, 'on') }[arg]
except KeyError:
    raise ValueError("command must be 'on', 'off' or 'toggle'")

bridge.set_group(1, { 'hue': 33862, 'sat': 50, 'bri': 254, 'on': on_state }, transitiontime=0)

You need phue for this script, install it with:

pip install --upgrade phue

UPDATE: I made some improvements to this post and made the toggle_hue script much faster, with some help from the phue author. Also a link to my new github repo with better code has been added.

Last modified: 2017-1-8 (日) at 9:14 pm