Skip to content

Instantly share code, notes, and snippets.

@hnykda

hnykda/main.py Secret

Last active October 23, 2020 19:43
Show Gist options
  • Save hnykda/126f682c7a0edad8c4f8cca714309d55 to your computer and use it in GitHub Desktop.
Save hnykda/126f682c7a0edad8c4f8cca714309d55 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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