Magic Home LedFx Bridge for LedFX

just add code in terminal in Home Assistant change names and ip and port numbers to your own.

then goto apps install check for updates! then install
then add lights to ledfx

add device use type ddpPreformatted text

.e.g.
Kitchen Right
IP: 192.168.1.123 (ip of your home assistant).
Pixels: 1
Port: 4048

Kitchen Left
IP: 192.168.1.123 (ip of your home assistant).
Pixels: 1
Port: 4049

Bedroom
IP: 192.168.1.123 (ip of your home assistant).
Pixels: 1
Port: 4050

mkdir -p /addons/magic_home_ledfx

cat > /addons/magic_home_ledfx/config.yaml <<'EOF'
name: "Magic Home LedFx Bridge"
version: "1.0.0"
slug: "magic_home_ledfx"
description: "Bridge LedFx DDP effects to Magic Home warm-white lights"
startup: application
boot: auto
init: false
arch:
  - amd64

ports:
  4048/udp: 4048
  4049/udp: 4049
  4050/udp: 4050

ports_description:
  4048/udp: "Bedroom LedFx DDP"
  4049/udp: "Kitchen Right LedFx DDP"
  4050/udp: "Kitchen Left LedFx DDP"

options:
  bedroom_ip: "192.168.1.136"
  kitchen_right_ip: "192.168.1.231"
  kitchen_left_ip: "192.168.1.193"
  max_fps: 12
  min_change: 3

schema:
  bedroom_ip: str
  kitchen_right_ip: str
  kitchen_left_ip: str
  max_fps: "int(1,30)"
  min_change: "int(0,50)"
EOF

cat > /addons/magic_home_ledfx/Dockerfile <<'EOF'
FROM ghcr.io/home-assistant/base:latest

ARG BUILD_VERSION
ARG BUILD_ARCH

LABEL \
  io.hass.version="${BUILD_VERSION}" \
  io.hass.type="app" \
  io.hass.arch="${BUILD_ARCH}"

RUN apk add --no-cache \
    python3 \
    py3-pip \
    py3-virtualenv \
  && python3 -m venv /opt/venv \
  && /opt/venv/bin/pip install --no-cache-dir flux_led

WORKDIR /app

COPY magic_home_ledfx.py /app/magic_home_ledfx.py

CMD ["/opt/venv/bin/python", "-u", "/app/magic_home_ledfx.py"]
EOF

cat > /addons/magic_home_ledfx/magic_home_ledfx.py <<'EOF'
import json
import socket
import threading
import time

from flux_led.device import WifiLedBulb


CONFIG_PATH = "/data/options.json"


def load_options():
    with open(CONFIG_PATH, "r", encoding="utf-8") as f:
        return json.load(f)


OPTIONS = load_options()

MAX_FPS = int(OPTIONS.get("max_fps", 12))
MIN_CHANGE = int(OPTIONS.get("min_change", 3))

LIGHTS = {
    4048: ("Bedroom", OPTIONS["bedroom_ip"]),
    4049: ("Kitchen Right", OPTIONS["kitchen_right_ip"]),
    4050: ("Kitchen Left", OPTIONS["kitchen_left_ip"]),
}


def run_light(port, name, ip):

    udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp.bind(("0.0.0.0", port))

    bulb = None
    last_level = -1
    last_send = 0.0
    powered_for_session = False

    print(
        f"{name}: listening for LedFx DDP on UDP {port} -> {ip}",
        flush=True,
    )

    while True:

        packet, _ = udp.recvfrom(2048)

        # LedFx DDP header = 10 bytes.
        # RGB pixel data begins at byte 10.
        if len(packet) < 13:
            continue

        payload = packet[10:]

        if len(payload) < 3:
            continue

        r, g, b = payload[0:3]

        # These Magic Home controllers are warm-white only.
        # Convert LedFx RGB output into brightness.
        level = max(r, g, b)

        now = time.monotonic()

        # Limit update speed so we don't overload
        # the Magic Home Wi-Fi controller.
        if now - last_send < (1 / MAX_FPS):
            continue

        if last_level >= 0 and abs(level - last_level) < MIN_CHANGE:
            continue

        try:

            if bulb is None:
                bulb = WifiLedBulb(ip)

            if not powered_for_session:
                bulb.turnOn()
                powered_for_session = True

            bulb.setWarmWhite255(
                level,
                persist=False,
                retry=0,
            )

            last_level = level
            last_send = now

        except Exception as err:

            print(
                f"{name}: controller error: {err}; reconnecting",
                flush=True,
            )

            try:
                if bulb is not None:
                    bulb.close()
            except Exception:
                pass

            bulb = None
            powered_for_session = False

            time.sleep(0.25)


def main():

    print("Magic Home LedFx Bridge starting", flush=True)
    print(
        f"Maximum rate: {MAX_FPS} FPS | "
        f"Minimum brightness change: {MIN_CHANGE}",
        flush=True,
    )

    for port, (name, ip) in LIGHTS.items():

        thread = threading.Thread(
            target=run_light,
            args=(port, name, ip),
            daemon=True,
        )

        thread.start()

    while True:
        time.sleep(3600)


if __name__ == "__main__":
    main()
EOF

echo "Magic Home LedFx add-on files created."