From 2af34b30693d99316e971e78087b37f983baa68f Mon Sep 17 00:00:00 2001 From: Github GPG acces Date: Sun, 6 Oct 2024 15:12:45 +0200 Subject: [PATCH] Add 'Availability' topic to MQTT connection Addition of the 'availability' topic: Online or Offline when using the --export MQTT option in the case where other devices on the same broker are looking for our Glances instance --- glances/exports/glances_mqtt/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/glances/exports/glances_mqtt/__init__.py b/glances/exports/glances_mqtt/__init__.py index 6c3eaec27c..63bce38d1c 100755 --- a/glances/exports/glances_mqtt/__init__.py +++ b/glances/exports/glances_mqtt/__init__.py @@ -79,6 +79,19 @@ def init(self): client_id='glances_' + self.devicename, clean_session=False, ) + + def on_connect(client, userdata, flags, reason_code, properties): + """Action to perform when connecting.""" + self.client.publish(topic='/'.join([self.topic, self.devicename, "availability"]), payload="Online") + + def on_disconnect(client, userdata, flags, reason_code, properties): + """Action to perform when the connection is over.""" + self.client.publish(topic='/'.join([self.topic, self.devicename, "availability"]), payload="Offline") + + client.on_connect = on_connect + client.on_disconnect = on_disconnect + client.will_set(topic='/'.join([self.topic, self.devicename, "availability"]), payload="Offline") + client.username_pw_set(username=self.user, password=self.password) if self.tls: client.tls_set(certifi.where())