From a23aca73b9227045b20b39992fadcab60eca94bb Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Mon, 4 Mar 2024 22:53:08 +0100 Subject: [PATCH] Support paho_mqtt >= 2.0.0 --- lib/TWCManager/Control/MQTTControl.py | 9 ++++++--- lib/TWCManager/EMS/DSMRreader.py | 9 ++++++--- lib/TWCManager/EMS/MQTT.py | 9 ++++++--- lib/TWCManager/Status/MQTTStatus.py | 7 +++++-- lib/TWCManager/Vehicle/TeslaMateVehicle.py | 9 ++++++--- tests/EMS/test_MQTT.py | 7 +++++-- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/TWCManager/Control/MQTTControl.py b/lib/TWCManager/Control/MQTTControl.py index 0644bcbb..749b0d65 100644 --- a/lib/TWCManager/Control/MQTTControl.py +++ b/lib/TWCManager/Control/MQTTControl.py @@ -48,7 +48,10 @@ def __init__(self, master): # to determine if they represent control messages logger.debug("Attempting to Connect") if self.brokerIP: - self.__client = self.mqtt.Client("MQTTCtrl") + if hasattr(self.mqtt, 'CallbackAPIVersion'): + self.__client = self.mqtt.Client(self.mqtt.CallbackAPIVersion.VERSION2, "MQTTCtrl") + else: + self.__client = self.mqtt.Client("MQTTCtrl") if self.username and self.password: self.__client.username_pw_set(self.username, self.password) self.__client.on_connect = self.mqttConnect @@ -73,7 +76,7 @@ def __init__(self, master): else: logger.log(logging.INFO4, "Module enabled but no brokerIP specified.") - def mqttConnect(self, client, userdata, flags, rc): + def mqttConnect(self, client, userdata, flags, rc, properties=None): logger.log(logging.INFO5, "MQTT Connected.") logger.log(logging.INFO5, "Subscribe to " + self.topicPrefix + "/#") res = self.__client.subscribe(self.topicPrefix + "/#", qos=0) @@ -109,5 +112,5 @@ def mqttMessage(self, client, userdata, message): logger.log(logging.INFO3, "MQTT Message called Stop") self._thread.interrupt_main() - def mqttSubscribe(self, client, userdata, mid, granted_qos): + def mqttSubscribe(self, client, userdata, mid, reason_codes, properties=None): logger.info("Subscribe operation completed with mid " + str(mid)) diff --git a/lib/TWCManager/EMS/DSMRreader.py b/lib/TWCManager/EMS/DSMRreader.py index f727e9ae..f59a0072 100644 --- a/lib/TWCManager/EMS/DSMRreader.py +++ b/lib/TWCManager/EMS/DSMRreader.py @@ -48,7 +48,10 @@ def __init__(self, master): logger.debug("Attempting to Connect to DSMR-reader MQTT Broker") if self.brokerIP: - self.__client = self.mqtt.Client("DSMRreader.EMS") + if hasattr(self.mqtt, 'CallbackAPIVersion'): + self.__client = self.mqtt.Client(self.mqtt.CallbackAPIVersion.VERSION2, "DSMRreader.EMS") + else: + self.__client = self.mqtt.Client("DSMRreader.EMS") if self.username and self.password: self.__client.username_pw_set(self.username, self.password) self.__client.on_connect = self.mqttConnect @@ -73,7 +76,7 @@ def __init__(self, master): else: logger.log(logging.INFO4, "Module enabled but no brokerIP specified.") - def mqttConnect(self, client, userdata, flags, rc): + def mqttConnect(self, client, userdata, flags, rc, properties=None): logger.log(logging.INFO5, "DSMRreader MQTT Connected.") if self.__topic: @@ -131,7 +134,7 @@ def mqttMessage(self, client, userdata, message): logging.INFO3, f"Consumption Amps Value updated to {self.consumedA}A" ) - def mqttSubscribe(self, client, userdata, mid, granted_qos): + def mqttSubscribe(self, client, userdata, mid, reason_codes, properties=None): logger.info("Subscribe operation completed with mid " + str(mid)) def getConsumption(self): diff --git a/lib/TWCManager/EMS/MQTT.py b/lib/TWCManager/EMS/MQTT.py index f55a69c7..1ff13f1f 100644 --- a/lib/TWCManager/EMS/MQTT.py +++ b/lib/TWCManager/EMS/MQTT.py @@ -49,7 +49,10 @@ def __init__(self, master): logger.debug("Attempting to Connect to MQTT Broker") if self.brokerIP: - self.__client = self.mqtt.Client("MQTT.EMS") + if hasattr(self.mqtt, 'CallbackAPIVersion'): + self.__client = self.mqtt.Client(self.mqtt.CallbackAPIVersion.VERSION2, "MQTT.EMS") + else: + self.__client = self.mqtt.Client("MQTT.EMS") if self.username and self.password: self.__client.username_pw_set(self.username, self.password) self.__client.on_connect = self.mqttConnect @@ -74,7 +77,7 @@ def __init__(self, master): else: logger.log(logging.INFO4, "Module enabled but no brokerIP specified.") - def mqttConnect(self, client, userdata, flags, rc): + def mqttConnect(self, client, userdata, flags, rc, properties=None): logger.log(logging.INFO5, "MQTT Connected.") if self.__topicConsumption: @@ -99,7 +102,7 @@ def mqttMessage(self, client, userdata, message): self.generatedW = payload logger.log(logging.INFO3, "MQTT EMS Generation Value updated") - def mqttSubscribe(self, client, userdata, mid, granted_qos): + def mqttSubscribe(self, client, userdata, reason_codes, properties=None): logger.info("Subscribe operation completed with mid " + str(mid)) def getConsumption(self): diff --git a/lib/TWCManager/Status/MQTTStatus.py b/lib/TWCManager/Status/MQTTStatus.py index 327f6ea3..f6b3eee2 100644 --- a/lib/TWCManager/Status/MQTTStatus.py +++ b/lib/TWCManager/Status/MQTTStatus.py @@ -106,7 +106,10 @@ def setStatus(self, twcid, key_underscore, key_camelcase, value, unit): if self.connectionState == 0: logger.debug("MQTT Status: Attempting to Connect") try: - client = self.mqtt.Client() + if hasattr(self.mqtt, 'CallbackAPIVersion'): + client = self.mqtt.Client(self.mqtt.CallbackAPIVersion.VERSION2, "MQTTStatus") + else: + client = self.mqtt.Client("MQTTStatus") if self.username and self.password: client.username_pw_set(self.username, self.password) client.on_connect = self.mqttConnected @@ -130,7 +133,7 @@ def setStatus(self, twcid, key_underscore, key_camelcase, value, unit): logger.debug(str(e)) return False - def mqttConnected(self, client, userdata, flags, rc): + def mqttConnected(self, client, userdata, flags, rc, properties=None): # This callback function is called once the MQTT client successfully # connects to the MQTT server. It will then publish all queued messages # to the server, and then disconnect. diff --git a/lib/TWCManager/Vehicle/TeslaMateVehicle.py b/lib/TWCManager/Vehicle/TeslaMateVehicle.py index 5f725fed..00936cd1 100644 --- a/lib/TWCManager/Vehicle/TeslaMateVehicle.py +++ b/lib/TWCManager/Vehicle/TeslaMateVehicle.py @@ -81,7 +81,10 @@ def __init__(self, master): timer.start() def doMQTT(self): - self.__client = mqtt.Client("TWCTeslaMate") + if hasattr(mqtt, 'CallbackAPIVersion'): + self.__client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, "TWCTeslaMate") + else: + self.__client = mqtt.Client("TWCTeslaMate") if self.__mqtt_user and self.__mqtt_pass: self.__client.username_pw_set(self.__mqtt_user, self.__mqtt_pass) self.__client.on_connect = self.mqttConnect @@ -169,7 +172,7 @@ def doSyncTokens(self, firstrun=False): # Required database details not provided. Turn off token sync self.syncTokens = False - def mqttConnect(self, client, userdata, flags, rc): + def mqttConnect(self, client, userdata, flags, rc, properties=None): logger.log(logging.INFO5, "MQTT Connected.") logger.log(logging.INFO5, "Subscribe to " + self.__mqtt_prefix + "/cars/#") res = client.subscribe(self.__mqtt_prefix + "/cars/#", qos=0) @@ -221,7 +224,7 @@ def mqttMessage(self, client, userdata, message): else: pass - def mqttSubscribe(self, client, userdata, mid, granted_qos): + def mqttSubscribe(self, client, userdata, mid, reason_codes, properties=None): logger.info("Subscribe operation completed with mid " + str(mid)) def updateVehicles(self, vehicle_id, vehicle_name): diff --git a/tests/EMS/test_MQTT.py b/tests/EMS/test_MQTT.py index 50e0cb27..8cb7e9a4 100755 --- a/tests/EMS/test_MQTT.py +++ b/tests/EMS/test_MQTT.py @@ -3,7 +3,7 @@ import paho.mqtt.client as mqtt import time -def mqttConnected(client, userdata, flags, rc): +def mqttConnected(client, userdata, flags, rc, properties=None): global test_state test_state = 1 @@ -12,7 +12,10 @@ def mqttConnected(client, userdata, flags, rc): test_duration_max = 120 test_state = 0 -client = mqtt.Client("MQTT.EMS.Test") +if hasattr(mqtt, 'CallbackAPIVersion'): + client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, "MQTT.EMS.Test") +else: + client = mqtt.Client("MQTT.EMS.Test") client.username_pw_set("twcmanager", "twcmanager") client.on_connect = mqttConnected