Skip to content

Instantly share code, notes, and snippets.

@chrisboyle
Created July 20, 2022 23:36
Show Gist options
  • Save chrisboyle/fe3eb5b1f82cb1969855f90df90ac2ea to your computer and use it in GitHub Desktop.
Save chrisboyle/fe3eb5b1f82cb1969855f90df90ac2ea to your computer and use it in GitHub Desktop.
A simple bridge from Hue switches to eWeLink/Sonoff relays
#!/usr/bin/env python3
import asyncio
import logging
from aiohue import HueBridgeV2
from aiohue.v2 import EventType
from aiohue.v2.models.button import Button, ButtonEvent
import ewelink
from ewelink import Client, DeviceOffline, Power
ASSIGNMENTS = {
"Front door switch 1": "Porch light",
"Living room switch 2": "Uplighters",
"Kitchen switch 1": "Kitchen ceiling lights",
}
HUE_IP = '192.168.1.123'
HUE_KEY = 'YOUR-HUE-APP-KEY' # see aiohue.create_app_key
EWELINK_USER = '[email protected]'
EWELINK_PASS = 'Your eWeLink password'
@ewelink.login(EWELINK_PASS, EWELINK_USER)
async def main(client: Client):
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)-15s %(levelname)-5s %(name)s -- %(message)s",
)
nameToEwelink = {device.name: device for device in client.devices}
async with HueBridgeV2(HUE_IP, HUE_KEY) as bridge:
hueIdToName = {device.id: device.metadata.name for device in bridge.devices}
async def handle_event(event_type, item):
if event_type != EventType.RESOURCE_UPDATED or not isinstance(item, Button) or item.button.last_event != ButtonEvent.INITIAL_PRESS:
return
if item.metadata.control_id == 1:
on = True
elif item.metadata.control_id == 4:
on = False
else:
return
name = hueIdToName.get(item.owner.rid, item.owner.rid)
if name in ASSIGNMENTS and ASSIGNMENTS[name] in nameToEwelink:
print("ON " if on else "OFF", name)
try:
await nameToEwelink[ASSIGNMENTS[name]].edit(Power.on if on else Power.off)
except DeviceOffline:
print("OFFLINE", ASSIGNMENTS[name])
bridge.subscribe(handle_event)
await asyncio.Event().wait() # forever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment