Allow reading mqtt broker from env

This commit is contained in:
Torjus Håkestad 2024-06-10 14:12:39 +02:00
parent 1affe0fde2
commit a0f103570d

View File

@ -1,3 +1,4 @@
import os
from enum import Enum from enum import Enum
from typing_extensions import Annotated from typing_extensions import Annotated
import typer import typer
@ -28,7 +29,7 @@ COLOR_MAP = {
"warm": (0.5056, 0.4152), "warm": (0.5056, 0.4152),
"warmest": (0.5267, 0.4133), "warmest": (0.5267, 0.4133),
} }
MQTT_BROKER = "mqtt://ha1:1183" MQTT_BROKER = "ha1"
class LightID(str, Enum): class LightID(str, Enum):
@ -38,6 +39,12 @@ class LightID(str, Enum):
all = "all" all = "all"
infuse = "infuse" infuse = "infuse"
def get_mqtt_broker():
if "HUECLI_BROKER" in os.environ:
return os.environ["HUECLI_BROKER"]
else:
return MQTT_BROKER
def complete_color(incomplete: str): def complete_color(incomplete: str):
completion = [] completion = []
@ -130,7 +137,7 @@ def set_color(
xy = rgb_to_xy(float(c[0]), float(c[1]), float(c[2])) xy = rgb_to_xy(float(c[0]), float(c[1]), float(c[2]))
raw_payload = {"x": xy[0], "y": xy[1]} raw_payload = {"x": xy[0], "y": xy[1]}
payload = json.dumps(raw_payload) payload = json.dumps(raw_payload)
pub.single(topic, payload, hostname="ha1") pub.single(topic, payload, hostname=get_mqtt_broker())
@app.command() @app.command()
@ -147,7 +154,7 @@ def set_state(
str_id = NAME_TO_ID[id] str_id = NAME_TO_ID[id]
topic = f"zigbee2mqtt/{str_id}/set" topic = f"zigbee2mqtt/{str_id}/set"
payload = json.dumps({"state": state.value}) payload = json.dumps({"state": state.value})
pub.single(topic, payload, hostname="ha1") pub.single(topic, payload, hostname=get_mqtt_broker())
@app.command() @app.command()
@ -164,7 +171,7 @@ def set_brightness(
str_id = NAME_TO_ID[id] str_id = NAME_TO_ID[id]
topic = f"zigbee2mqtt/{str_id}/set" topic = f"zigbee2mqtt/{str_id}/set"
payload = json.dumps({"brightness": brightness}) payload = json.dumps({"brightness": brightness})
pub.single(topic, payload, hostname="ha1") pub.single(topic, payload, hostname=get_mqtt_broker())
def main(): def main():