Skip to content
This repository was archived by the owner on Oct 26, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f2f44fb

Browse files
committedApr 19, 2019
Improve mqtt lwt on reconnect
1 parent 6561c2c commit f2f44fb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed
 

‎mqtt.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def __init__(self, config):
1111
self._config = config
1212
self._mqttc = mqtt.Client(client_id=self.client_id,
1313
clean_session=False,
14-
userdata = {'global_topic_prefix': self.topic_prefix})
14+
userdata={'global_topic_prefix': self.topic_prefix})
15+
16+
self.mqttc.on_connect = self.on_connect
1517

1618
if self.username and self.password:
1719
self.mqttc.username_pw_set(self.username, self.password)
@@ -61,6 +63,11 @@ def availability_topic(self):
6163
def mqttc(self):
6264
return self._mqttc
6365

66+
def on_connect(self, userdata, flags, rc):
67+
if self.availability_topic:
68+
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_ONLINE, retain=True)])
69+
70+
6471
def callbacks_subscription(self, callbacks):
6572
self.mqttc.connect(self.hostname, port=self.port)
6673

@@ -72,9 +79,6 @@ def callbacks_subscription(self, callbacks):
7279

7380
self.mqttc.loop_start()
7481

75-
if self.availability_topic:
76-
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_ONLINE, retain=True)])
77-
7882
def __del__(self):
7983
if self.availability_topic:
8084
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_OFFLINE, retain=True)])

0 commit comments

Comments
 (0)
This repository has been archived.