Skip to content

Instantly share code, notes, and snippets.

@hnykda

hnykda/main.py Secret

Last active Oct 23, 2020
Embed
What would you like to do?
import requests
from yeelight import Bulb
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
"""IFTTT variant: takes about 3 seconds after the switch is toggled to toggle lights.
Unfortunately, Shelly cannot reach external IP addresses for some reason, then there wouldn't
be a need for this server :-(
"""
requests.get("https://maker.ifttt.com/trigger/bedroom_switch/with/key/XXX")
return "ok"
def toggle_bulb(bulb: Bulb):
return bulb.toggle()
@app.get("/yl")
async def yeelight():
"""Is close to immediate"""
a = Bulb("192.168.0.172") # you have to change these to your bulb addresses
b = Bulb("192.168.0.136")
with ThreadPoolExecutor(2) as executor: # just to switch them concurrently
res = executor.map(toggle_bulb, [a, b])
return res
@hnykda

This comment has been minimized.

Copy link
Owner Author

@hnykda hnykda commented Oct 23, 2020

The way to run it on e.g. 192.168.0.121

sudo uvicorn --reload main:app --port 80 --host 192.168.0.121
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment