Skip to content

Instantly share code, notes, and snippets.

@chrisboyle
Last active November 4, 2019 12:51
Show Gist options
  • Select an option

  • Save chrisboyle/b445b9a1e22bf836a403d35a41f560d1 to your computer and use it in GitHub Desktop.

Select an option

Save chrisboyle/b445b9a1e22bf836a403d35a41f560d1 to your computer and use it in GitHub Desktop.
Quick hack to make my Hue switches control a Sonoff/eWeLink device as well.
#!/usr/bin/env python3
HUE_POLL_SECONDS = 5
HUE_IP = '192.168.1.123'
HUE_USERNAME = 'xyz123' # see https://github.com/quentinsf/qhue#creating-a-user
HUE_NAME_LOWER_SUBSTRINGS = ['living room', 'front door']
EWELINK_USERNAME = '[email protected]'
EWELINK_PASSWORD = 'password'
EWELINK_REGION = 'eu'
EWELINK_OUTLET = None
import qhue # pip3 install qhue
import sonoff # pip3 install sonoff-python
import subprocess
import sys
import time
import traceback
bridge = qhue.Bridge(HUE_IP, HUE_USERNAME)
states = {}
ewelink = sonoff.Sonoff(EWELINK_USERNAME, EWELINK_PASSWORD, EWELINK_REGION)
deviceid = ewelink.get_devices()[0]['deviceid']
while True:
try:
sensors = bridge.sensors()
except:
traceback.print_exc()
time.sleep(HUE_POLL_SECONDS)
continue
for s in sensors:
sensor = sensors[s]
state = sensor['state']
if 'buttonevent' not in state:
# daylight, motion, geofence, counters etc.
continue
name = sensor['name'].lower()
if not any(map(name.__contains__, HUE_NAME_LOWER_SUBSTRINGS)):
continue
if s not in states:
states[s] = state
# don't alter anything on startup
continue
if states[s] != state:
button = str(state['buttonevent'])[0]
try:
if button == '1':
# "on" pressed
ewelink.switch('on', deviceid, EWELINK_OUTLET)
elif button == '4':
# "off" pressed
ewelink.switch('off', deviceid, EWELINK_OUTLET)
states[s] = state
except:
traceback.print_exc()
time.sleep(HUE_POLL_SECONDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment