Add username and password authentication as optional for mqtt broker and refine readme

main
Tim Dithmer 2024-10-28 21:15:14 +01:00
parent 42480df5c1
commit 3072331f8a
2 changed files with 24 additions and 0 deletions

14
README
View File

@ -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
```

10
main.py
View File

@ -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()