From a0f103570d97230683591134abcb2acbc62efbf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Mon, 10 Jun 2024 14:12:39 +0200 Subject: [PATCH] Allow reading mqtt broker from env --- huecli/__main__.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/huecli/__main__.py b/huecli/__main__.py index af76ef7..088121e 100644 --- a/huecli/__main__.py +++ b/huecli/__main__.py @@ -1,3 +1,4 @@ +import os from enum import Enum from typing_extensions import Annotated import typer @@ -28,7 +29,7 @@ COLOR_MAP = { "warm": (0.5056, 0.4152), "warmest": (0.5267, 0.4133), } -MQTT_BROKER = "mqtt://ha1:1183" +MQTT_BROKER = "ha1" class LightID(str, Enum): @@ -38,6 +39,12 @@ class LightID(str, Enum): all = "all" 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): completion = [] @@ -130,7 +137,7 @@ def set_color( xy = rgb_to_xy(float(c[0]), float(c[1]), float(c[2])) raw_payload = {"x": xy[0], "y": xy[1]} payload = json.dumps(raw_payload) - pub.single(topic, payload, hostname="ha1") + pub.single(topic, payload, hostname=get_mqtt_broker()) @app.command() @@ -147,7 +154,7 @@ def set_state( str_id = NAME_TO_ID[id] topic = f"zigbee2mqtt/{str_id}/set" payload = json.dumps({"state": state.value}) - pub.single(topic, payload, hostname="ha1") + pub.single(topic, payload, hostname=get_mqtt_broker()) @app.command() @@ -164,7 +171,7 @@ def set_brightness( str_id = NAME_TO_ID[id] topic = f"zigbee2mqtt/{str_id}/set" payload = json.dumps({"brightness": brightness}) - pub.single(topic, payload, hostname="ha1") + pub.single(topic, payload, hostname=get_mqtt_broker()) def main():