From 0bd6f5e8183b34d1023bccebf7a1bec2bbae470c Mon Sep 17 00:00:00 2001 From: Correl Date: Wed, 31 Jan 2024 22:45:14 -0500 Subject: [PATCH] Add MQTT authentication --- main.py | 16 ++++++++++++++-- settings.json | 15 +++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 2de1e1b..b5596d1 100644 --- a/main.py +++ b/main.py @@ -64,8 +64,20 @@ last_mqtt_attempt = 0 def mqtt_init(): - print("Starting MQTT client") - mqtt = MQTTClient(mqtt_client_id, mqtt_broker, keepalive=MQTT_KEEPALIVE) + print(f"Connecting MQTT client (host: {mqtt_broker}, prefix: {mqtt_prefix})") + username = settings["mqtt"].get("username") + password = settings["mqtt"].get("password") + if username and password: + print(f"Authenticating with MQTT as user '{username}") + mqtt = MQTTClient( + mqtt_client_id, + mqtt_broker, + keepalive=MQTT_KEEPALIVE, + user=username, + password=password, + ) + else: + mqtt = MQTTClient(mqtt_client_id, mqtt_broker, keepalive=MQTT_KEEPALIVE) mqtt.set_callback(on_message) mqtt.set_last_will(f"{mqtt_prefix}/status", b"offline", retain=True) mqtt.connect() diff --git a/settings.json b/settings.json index 9b01641..3a4d09f 100644 --- a/settings.json +++ b/settings.json @@ -4,11 +4,18 @@ "password": "job5638pit5614iced" }, "mqtt": { - "broker": "192.168.1.183", - "prefix": "desk-controls" + "broker": "192.168.1.13", + "prefix": "desk-controls", + "username": "desk-controls", + "password": "r9Yh]sKPa2Hw|d0G" }, "home-assistant": { - "url": "http://192.168.1.183:8123", + "url": "http://192.168.1.13:8123", "api_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI3MGIyZjk0MzZkZDk0ZjNlYTRhMWVlZGRlZWMzMTU5NCIsImlhdCI6MTY5NDUzNjY3NywiZXhwIjoyMDA5ODk2Njc3fQ.uetwuZ9_Szbnuf3vbKTLclBiF24YXLHOiDBswgT8drA" - } + }, + "scenes": [ + "scene.morning_office", + "scene.relaxed_office", + "scene.bi_office" + ] }