From 3072331f8a9b7b0b12f6c8d39aadd373c2aaaf4f Mon Sep 17 00:00:00 2001 From: Tim Dithmer Date: Mon, 28 Oct 2024 21:15:14 +0100 Subject: [PATCH] Add username and password authentication as optional for mqtt broker and refine readme --- README | 14 ++++++++++++++ main.py | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/README b/README index fa7f379..c1280fb 100644 --- a/README +++ b/README @@ -1,3 +1,17 @@ +This is the docker container and script to publish the energy consumption of the Huette to the MQTT broker. + +The data is a json array with index 0 means today, index 1 one day in the past, index 2 two days in the past and so on. The data is in watt hours. + +e.g. [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000] + +# Environment variables + +MQTT_HOST: The host of the MQTT broker +MQTT_PORT: (Optional) The port of the MQTT broker +MQTT_TOPIC: The topic to publish to +MQTT_USERNAME: (Optional) The user for the MQTT broker +MQTT_PASSWORD: (Optional) The password for the MQTT broker + # Build ``` diff --git a/main.py b/main.py index a61c1e7..e8e2fcb 100644 --- a/main.py +++ b/main.py @@ -44,7 +44,17 @@ def main(): port = int(os.getenv("MQTT_PORT", 1883)) topic = os.getenv("MQTT_TOPIC") + username = os.getenv("MQTT_USERNAME") + password = os.getenv("MQTT_PASSWORD") + mqttc = mqtt.Client(mqtt_enums.CallbackAPIVersion.VERSION2) + + if username: + if not password: + raise ValueError("MQTT_USERNAME is set but MQTT_PASSWORD is not set") + + mqttc.username_pw_set(username, password) + mqttc.connect(host, port) mqttc.loop_start()