Skip to content

Commit

Permalink
Support paho_mqtt >= 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RichieB2B committed Mar 4, 2024
1 parent 99257de commit a23aca7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
9 changes: 6 additions & 3 deletions lib/TWCManager/Control/MQTTControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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))
9 changes: 6 additions & 3 deletions lib/TWCManager/EMS/DSMRreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 6 additions & 3 deletions lib/TWCManager/EMS/MQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions lib/TWCManager/Status/MQTTStatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions lib/TWCManager/Vehicle/TeslaMateVehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions tests/EMS/test_MQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit a23aca7

Please sign in to comment.