Adaption to 2.0 API

This commit is contained in:
Dirk Alders 2024-02-11 10:27:31 +01:00
parent 1adfb0626e
commit 328d3471a7

View File

@ -51,7 +51,7 @@ class mqtt_client(object):
self.__block_add_callbacks__ = True self.__block_add_callbacks__ = True
logger.info("Initiating mqtt client instance") logger.info("Initiating mqtt client instance")
self.__callbacks__ = {} self.__callbacks__ = {}
self.__client__ = paho.Client(name) # create client object self.__client__ = paho.Client(paho.CallbackAPIVersion.VERSION2) # create client object
if username is not None and password is not None: if username is not None and password is not None:
logger.debug("Configuring authentification") logger.debug("Configuring authentification")
self.__client__.username_pw_set(username, password) # login with credentials self.__client__.username_pw_set(username, password) # login with credentials
@ -59,7 +59,7 @@ class mqtt_client(object):
self.__client__.on_connect = self.__on_connect__ self.__client__.on_connect = self.__on_connect__
self.__client__.on_disconnect = self.__on_disconnect__ self.__client__.on_disconnect = self.__on_disconnect__
try: try:
self.__client__.connect(host, port) # establish connection self.__client__.connect(host, port, 60) # establish connection
except (socket.timeout, OSError) as e: except (socket.timeout, OSError) as e:
logger.warning("Can not connect to MQTT-Server") logger.warning("Can not connect to MQTT-Server")
self.__client__.loop_start() # start the loop self.__client__.loop_start() # start the loop
@ -75,8 +75,8 @@ class mqtt_client(object):
get_topic_logger(topic).debug("Sending message with topic %s and payload %s", topic, str(payload)) get_topic_logger(topic).debug("Sending message with topic %s and payload %s", topic, str(payload))
self.__client__.publish(topic, payload) self.__client__.publish(topic, payload)
def __on_connect__(self, client, userdata, flags, rc): def __on_connect__(self, client, userdata, flags, rc, properties):
logger.debug("Connect with rc=%d", rc) logger.debug("Connect with rc=%s", repr(rc))
if rc == 0: if rc == 0:
self.__block_add_callbacks__ = True self.__block_add_callbacks__ = True
logger.debug("Registering topics...") logger.debug("Registering topics...")
@ -84,8 +84,8 @@ class mqtt_client(object):
self.__client__.subscribe(topic) self.__client__.subscribe(topic)
self.__block_add_callbacks__ = False self.__block_add_callbacks__ = False
def __on_disconnect__(self, client, flags, rc): def __on_disconnect__(self, client, userdata, flags, rc, properties):
logger.warning("Disconnect with rc=%d", rc) logger.warning("Disconnect with rc=%s", repr(rc))
def __receive__(self, client, userdata, message): def __receive__(self, client, userdata, message):
get_topic_logger(message.topic).debug("Received message with topic %s and payload %s", message.topic, str(message.payload)) get_topic_logger(message.topic).debug("Received message with topic %s and payload %s", message.topic, str(message.payload))