Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Improve mqtt lwt on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
zewelor committed Apr 19, 2019
1 parent 6561c2c commit 074a783
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, config):
self._config = config
self._mqttc = mqtt.Client(client_id=self.client_id,
clean_session=False,
userdata = {'global_topic_prefix': self.topic_prefix})
userdata={'global_topic_prefix': self.topic_prefix})

if self.username and self.password:
self.mqttc.username_pw_set(self.username, self.password)
Expand Down Expand Up @@ -61,7 +61,14 @@ def availability_topic(self):
def mqttc(self):
return self._mqttc

def on_connect(self, client, userdata, flags, rc):
if self.availability_topic:
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_ONLINE, retain=True)])


def callbacks_subscription(self, callbacks):
self.mqttc.on_connect = self.on_connect

self.mqttc.connect(self.hostname, port=self.port)

for topic, callback in callbacks:
Expand All @@ -72,9 +79,6 @@ def callbacks_subscription(self, callbacks):

self.mqttc.loop_start()

if self.availability_topic:
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_ONLINE, retain=True)])

def __del__(self):
if self.availability_topic:
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_OFFLINE, retain=True)])
Expand Down

0 comments on commit 074a783

Please sign in to comment.