LedFx icon indicating copy to clipboard operation
LedFx copied to clipboard

ADD Magic Home device support

Open PPPAnimal opened this issue 7 months ago • 5 comments

Describe the solution you'd like Would like to be able to add my Magic Home devices and use LEDFX to make them react to music. Sure they are not addressable but could do some basic effects.

Describe alternatives you've considered Home Assistant i guess bridges this gap but i don't use that. Flashing with WLED is a solution I may attempt.

Additional context I guess i could try to do this myself in my app. I do capture music now to run a Spectrum Analyzer on screen. (currently CPU taxing but works) Having LEDFX support would make it available to far more users.

Possibly a point in the right direction to help me figure out how to capture LEDFX stream of Equalizer 2D or Beat Detection would allow me to just steam your eq and display in my app as well as trigger Magic Home color changes on beat detection and brightness changes based on volume level. Any help would be greatly appreciated.

PPPAnimal avatar Apr 07 '26 15:04 PPPAnimal

Well i have been working on this a bit and came up with this solution.
I don't think it would be hard for you guys to add support for this would it?

Make a virtual device, UDP, set to one pixel.
I am using RGB (HyperHDR).
Port 21324 Address 127.0.0.1

And then run this Python code or put it in your app etc to receive the data and send it to your Magic Home devices.

import socket import time from flux_led import WifiLedBulb

--- CONFIGURATION ---

Add all your Magic Home device IPs to this list

BULB_IPS = [ "192.168.0.64", "192.168.0.28", "192.168.0.225", "192.168.0.199", "192.168.0.155", "192.168.0.164" ] LISTEN_PORT = 21324 UPDATE_THROTTLE = 0.2 # Seconds between updates. Increase if lights flicker.

def start_bridge(): bulbs = [] print("Connecting to all Magic Home devices...") for ip in BULB_IPS: try: # Initialize each bulb and store it in our list bulbs.append(WifiLedBulb(ip)) print(f" + Connected to {ip}") except Exception as e: print(f" - Failed to connect to {ip}: {e}")

if not bulbs:
    print("No bulbs connected. Exiting.")
    return

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", LISTEN_PORT))
print(f"\nListening for LedFx data on port {LISTEN_PORT}...")

last_send_time = 0
last_color = (0, 0, 0)

try:
    while True:
        data, addr = sock.recvfrom(1024)
        
        if len(data) == 3:
            r, g, b = data[0], data[1], data[2]
            current_time = time.time()

            # Sync condition: Color changed AND throttle time passed
            if (r, g, b) != last_color and (current_time - last_send_time) > UPDATE_THROTTLE:
                # Loop through EVERY bulb and send the same color
                for bulb in bulbs:
                    try:
                        bulb.setRgb(r, g, b)
                    except Exception as e:
                        # If one bulb drops off, don't crash the whole script
                        pass
                
                last_color = (r, g, b)
                last_send_time = current_time
                print(f"Synced {len(bulbs)} devices -> R:{r:3} G:{g:3} B:{b:3}")

except KeyboardInterrupt:
    print("\nShutting down bridge...")
finally:
    sock.close()

if name == "main": start_bridge()

PPPAnimal avatar Apr 09 '26 19:04 PPPAnimal

EVEN BETTER CODE. USING THREADS TO SEND DATA TO ALL DEVICES AT SAME TIME. FASTER AND STAYS IN SYNC BETTER.

import socket import time import threading import queue from flux_led import WifiLedBulb

--- CONFIGURATION ---

BULB_IPS = [ "192.168.0.64", "192.168.0.28", "192.168.0.225", "192.168.0.199", "192.168.0.155", "192.168.0.164" ] LISTEN_PORT = 21324

UPDATE_THROTTLE = 0.2 # Seconds between updates. Increase if lights flicker.

bulb_queues = []

def bulb_worker(ip, q): """A dedicated thread for each light to prevent bottlenecks.""" print(f"Connecting to {ip}...") try: bulb = WifiLedBulb(ip) print(f" + Successful Connection: {ip}") except Exception as e: print(f" - Connection failed for {ip}: {e}") return

while True:
    r, g, b = q.get()
    try:
        bulb.setRgb(r, g, b)
    except Exception:
        pass  # If the bulb is unreachable, just skip this update
    q.task_done()

def start_bridge(): # Spawn all threads immediately for ip in BULB_IPS: # maxsize=1 ensures we only ever hold the NEWEST color q = queue.Queue(maxsize=1) bulb_queues.append(q) t = threading.Thread(target=bulb_worker, args=(ip, q), daemon=True) t.start()

# Setup UDP listener
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("127.0.0.1", LISTEN_PORT))
print(f"\nBridge Active. Listening on port {LISTEN_PORT}...")

last_send_time = 0
last_color = (0, 0, 0)

try:
    while True:
        data, addr = sock.recvfrom(1024)
        
        if len(data) == 3:
            r, g, b = data[0], data[1], data[2]
            current_time = time.time()

            # Global Throttle: Syncs the timing for all threads
            if (r, g, b) != last_color and (current_time - last_send_time) > UPDATE_THROTTLE:
                
                # Push color to all Devices at once
                for q in bulb_queues:
                    if q.full():
                        try: q.get_nowait() # Remove old color if thread is busy
                        except: pass
                    q.put_nowait((r, g, b))
                
                last_color = (r, g, b)
                last_send_time = current_time
                print(f"Pulse -> R:{r:3} G:{g:3} B:{b:3}")

except KeyboardInterrupt:
    print("\nShutting down...")
finally:
    sock.close()

if name == "main": start_bridge()

PPPAnimal avatar Apr 09 '26 20:04 PPPAnimal

We are an open source project. If you have the need and devices to test against. Implement a device instance and submit a PR.

bigredfrog avatar Apr 09 '26 21:04 bigredfrog

Looking into it. if i figure it out i will let you know.

PPPAnimal avatar Apr 10 '26 03:04 PPPAnimal

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 07 '26 22:07 stale[bot]