Fix MQTT: add connection delay and retain=True for state topics
This commit is contained in:
parent
59aa5544e9
commit
7d6d1146b6
BIN
__pycache__/main.cpython-314.pyc
Normal file
BIN
__pycache__/main.cpython-314.pyc
Normal file
Binary file not shown.
12
main.py
12
main.py
@ -95,7 +95,12 @@ class GadgetbridgeMQTT:
|
||||
|
||||
def connect_mqtt(self):
|
||||
"""Connect to MQTT broker"""
|
||||
self.mqtt_client = mqtt.Client()
|
||||
# Use callback API version 2 to avoid deprecation warning
|
||||
try:
|
||||
self.mqtt_client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
|
||||
except (AttributeError, TypeError):
|
||||
# Fallback for older paho-mqtt versions
|
||||
self.mqtt_client = mqtt.Client()
|
||||
|
||||
if self.config.get("mqtt_username") and self.config.get("mqtt_password"):
|
||||
self.mqtt_client.username_pw_set(
|
||||
@ -110,6 +115,8 @@ class GadgetbridgeMQTT:
|
||||
60
|
||||
)
|
||||
self.mqtt_client.loop_start()
|
||||
# Wait for connection to establish
|
||||
time.sleep(1)
|
||||
logger.info(f"Connected to MQTT broker: {self.config['mqtt_broker']}")
|
||||
return True
|
||||
except Exception as e:
|
||||
@ -305,7 +312,8 @@ class GadgetbridgeMQTT:
|
||||
"""Publish sensor data to MQTT"""
|
||||
for key, value in data.items():
|
||||
topic = f"gadgetbridge/{self.device_name}/{key}"
|
||||
self.mqtt_client.publish(topic, str(value), qos=1)
|
||||
# Use retain=True so HA gets values on restart
|
||||
self.mqtt_client.publish(topic, str(value), qos=1, retain=True)
|
||||
|
||||
logger.info(f"Published: steps={data.get('daily_steps', 'N/A')}, "
|
||||
f"hr={data.get('heart_rate', 'N/A')}, "
|
||||
|
||||
Loading…
Reference in New Issue
Block a user