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
This commit is contained in:
Github GPG acces 2024-10-06 15:12:45 +02:00
parent 10cc1dbe82
commit 2af34b3069
No known key found for this signature in database
GPG Key ID: 384AAACE61391474
1 changed files with 13 additions and 0 deletions

View File

@ -79,6 +79,19 @@ class Export(GlancesExport):
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())