Skip to content

Instantly share code, notes, and snippets.

@SergioSV96
Last active December 4, 2023 13:01
Show Gist options
  • Save SergioSV96/8e9370d054aee0a28328281b5375d3e6 to your computer and use it in GitHub Desktop.
Save SergioSV96/8e9370d054aee0a28328281b5375d3e6 to your computer and use it in GitHub Desktop.
A Hyperion UDP listener for Xiaomi Mi LED Smart Bulb bulbs! Just "pip install yeelight" and run with "python3 hyperion_mi_led.py PORT NAME"
import time
import yeelight
import sys
import socket
import colorsys
import argparse
from yeelight import BulbException
def search_xiaomi_bulb(name):
"""
Returns the Xiaomi device for the selected bulb name
"""
print("Discovering lights...")
# get devices
devices = yeelight.discover_bulbs()
print("\nFound {} light(s):\n".format(len(devices)))
for i, device in enumerate(devices):
try:
print(device["capabilities"]["name"])
if device["capabilities"]["name"] == name:
return yeelight.Bulb(device["ip"])
except:
pass
sys.exit("Device not found")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Connect a Xiaomi bulb with Hyperion.ng')
parser.add_argument('PORT', type=int, help='the hyperion.ng udpraw port selected')
parser.add_argument('NAME', type=str, help="the Xiaomi bulb's name")
args = parser.parse_args()
# Arguments
port = args.PORT
name = args.NAME
# Connection to Xiaomi Bulb
device = search_xiaomi_bulb(name)
"""
SOCKET UDP CLIENT
"""
IP = "127.0.0.1" # LOCAL IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((IP, port))
print("waiting on port:", port)
packet = bytearray(3)
# Stop/Start music mode, bypasses lamp rate limits, ensures that previous sockets close before starting
try:
device.stop_music()
except BulbException:
pass
time.sleep(1)
device.start_music()
time.sleep(1)
device.effect = "smooth"
device.set_color_temp(3500)
while True:
data = s.recvfrom_into(packet)
hsv = colorsys.rgb_to_hsv(packet[0], packet[1], packet[2])
hsvk = [hsv[0] * 359, hsv[1] * 100, hsv[2]/255 * 100]
device.set_hsv(hsvk[0], hsvk[1], hsvk[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment