From 08f3163ee1dd95ed6c4ef08921e112dfef7f968e Mon Sep 17 00:00:00 2001 From: tonym Date: Wed, 1 Oct 2025 00:29:28 -0500 Subject: [PATCH] Add gammy_mqtt.py --- gammy_mqtt.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 gammy_mqtt.py diff --git a/gammy_mqtt.py b/gammy_mqtt.py new file mode 100644 index 0000000..df654e2 --- /dev/null +++ b/gammy_mqtt.py @@ -0,0 +1,83 @@ +import paho.mqtt.client as mqtt +import json, os +from pathlib import Path + +# MQTT settings +BROKER = "192.168.1.100" # <-- your HA MQTT broker IP +PORT = 1883 +USERNAME = "your_mqtt_user" +PASSWORD = "your_mqtt_pass" +TOPIC_SUB = "pc/gammy/mode" +TOPIC_PUB = "pc/gammy/state" + +# Path to Gammy config file +CONF = Path(os.getenv("APPDATA")) / "gammy" / "gammy.conf" + +# Fade presets (seconds) +FADE_TIMES = { + "snap": 0, + "fast": 1, + "slow": 30 +} + +def read_config(): + with open(CONF, "r") as f: + return json.load(f) + +def write_config(cfg): + with open(CONF, "w") as f: + json.dump(cfg, f, indent=2) + +def set_gammy(temp=None, bright=None, auto=False, fade="fast"): + cfg = read_config() + + if auto: + cfg["autotemperature"] = True + else: + cfg["autotemperature"] = False + if temp: + cfg["temperature"] = temp + + if bright is not None: + cfg["autobrightness"] = False + cfg["brightness"] = bright + + cfg["fade"] = FADE_TIMES.get(fade, 1) + + write_config(cfg) + return cfg + +def on_connect(client, userdata, flags, rc): + print("Connected:", rc) + client.subscribe(TOPIC_SUB) + +def on_message(client, userdata, msg): + payload = msg.payload.decode().strip().lower() + print("Received:", payload) + + parts = payload.split() + mode = parts[0] + fade = parts[1] if len(parts) > 1 else "fast" + + if mode == "ember": + cfg = set_gammy(temp=1900, bright=30, auto=False, fade=fade) + client.publish(TOPIC_PUB, f"ember {fade} (1900K, 30%)") + + elif mode == "movie": + cfg = set_gammy(temp=6500, bright=100, auto=False, fade=fade) + client.publish(TOPIC_PUB, f"movie {fade} (6500K, 100%)") + + elif mode == "auto": + cfg = set_gammy(auto=True, bright=80, fade=fade) + client.publish(TOPIC_PUB, f"auto {fade} (auto temp, 80%)") + + else: + print("Unknown command") + +client = mqtt.Client() +client.username_pw_set(USERNAME, PASSWORD) +client.on_connect = on_connect +client.on_message = on_message + +client.connect(BROKER, PORT, 60) +client.loop_forever() \ No newline at end of file