Add gammy_mqtt.py
This commit is contained in:
83
gammy_mqtt.py
Normal file
83
gammy_mqtt.py
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user