From c81aba44880881de739895735441eadc71aac2e3 Mon Sep 17 00:00:00 2001 From: Kevin Robert Keegan Date: Wed, 13 Nov 2024 15:38:28 -0800 Subject: [PATCH] Migrate to Paho API Version 2 --- insteon_mqtt/cmd_line/util.py | 2 +- insteon_mqtt/network/Mqtt.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/insteon_mqtt/cmd_line/util.py b/insteon_mqtt/cmd_line/util.py index 4af6aa05..c9e16198 100644 --- a/insteon_mqtt/cmd_line/util.py +++ b/insteon_mqtt/cmd_line/util.py @@ -74,7 +74,7 @@ def send(config, topic, payload, quiet=False): "quiet" : int(quiet), } - client = mqtt.Client(userdata=session) + client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, userdata=session) # Add user/password if the config file has them set. if config["mqtt"].get("username", None): diff --git a/insteon_mqtt/network/Mqtt.py b/insteon_mqtt/network/Mqtt.py index 90157fc7..365e0608 100644 --- a/insteon_mqtt/network/Mqtt.py +++ b/insteon_mqtt/network/Mqtt.py @@ -105,7 +105,11 @@ def setup_client(self): """ Create or reinitialise the MQTT client and set the callbacks. """ - client_args = {'client_id': self.id, 'clean_session': False} + client_args = { + 'callback_api_version': paho.CallbackAPIVersion.VERSION2, + 'client_id': self.id, + 'clean_session': False + } if not hasattr(self, 'client'): self.client = paho.Client(**client_args) @@ -385,7 +389,7 @@ def close(self): self.signal_needs_write.emit(self, True) #----------------------------------------------------------------------- - def _on_connect(self, client, data, flags, result): + def _on_connect(self, client, userdata, flags, reason_code, properties): """MQTT connection callback. This is called by the MQTT client once the connection has occurred. @@ -398,20 +402,20 @@ def _on_connect(self, client, data, flags, result): client, 3 = server unavailable, 4 = bad login, 5 = not authorized. """ - if result == 0: + if reason_code == 0: self.connected = True self.signal_connected.emit(self, True) self.client.publish(self.availability_topic, payload="online", qos=0, retain=True) else: LOG.error("MQTT connection refused %s %s %s", self.host, self.port, - result) + reason_code) #----------------------------------------------------------------------- - def _on_disconnect(self, client, data, result): + def _on_disconnect(self, client, userdata, flags, reason_code, properties): """MQTT disconnection callback. - This is called by the MQTT client when the connection is droppped. + This is called by the MQTT client when the connection is dropped. Args: client (paho.Client): The paho mqtt client (self.client).