Browse Source

Adaption to 2.0 API

master
Dirk Alders 8 months ago
parent
commit
328d3471a7
1 changed files with 6 additions and 6 deletions
  1. 6
    6
      __init__.py

+ 6
- 6
__init__.py View File

51
         self.__block_add_callbacks__ = True
51
         self.__block_add_callbacks__ = True
52
         logger.info("Initiating mqtt client instance")
52
         logger.info("Initiating mqtt client instance")
53
         self.__callbacks__ = {}
53
         self.__callbacks__ = {}
54
-        self.__client__ = paho.Client(name)                                     # create client object
54
+        self.__client__ = paho.Client(paho.CallbackAPIVersion.VERSION2)         # create client object
55
         if username is not None and password is not None:
55
         if username is not None and password is not None:
56
             logger.debug("Configuring authentification")
56
             logger.debug("Configuring authentification")
57
             self.__client__.username_pw_set(username, password)                 # login with credentials
57
             self.__client__.username_pw_set(username, password)                 # login with credentials
59
         self.__client__.on_connect = self.__on_connect__
59
         self.__client__.on_connect = self.__on_connect__
60
         self.__client__.on_disconnect = self.__on_disconnect__
60
         self.__client__.on_disconnect = self.__on_disconnect__
61
         try:
61
         try:
62
-            self.__client__.connect(host, port)                                 # establish connection
62
+            self.__client__.connect(host, port, 60)                             # establish connection
63
         except (socket.timeout, OSError) as e:
63
         except (socket.timeout, OSError) as e:
64
             logger.warning("Can not connect to MQTT-Server")
64
             logger.warning("Can not connect to MQTT-Server")
65
         self.__client__.loop_start()                                            # start the loop
65
         self.__client__.loop_start()                                            # start the loop
75
         get_topic_logger(topic).debug("Sending message with topic %s and payload %s", topic, str(payload))
75
         get_topic_logger(topic).debug("Sending message with topic %s and payload %s", topic, str(payload))
76
         self.__client__.publish(topic, payload)
76
         self.__client__.publish(topic, payload)
77
 
77
 
78
-    def __on_connect__(self, client, userdata, flags, rc):
79
-        logger.debug("Connect with rc=%d", rc)
78
+    def __on_connect__(self, client, userdata, flags, rc, properties):
79
+        logger.debug("Connect with rc=%s", repr(rc))
80
         if rc == 0:
80
         if rc == 0:
81
             self.__block_add_callbacks__ = True
81
             self.__block_add_callbacks__ = True
82
             logger.debug("Registering topics...")
82
             logger.debug("Registering topics...")
84
                 self.__client__.subscribe(topic)
84
                 self.__client__.subscribe(topic)
85
             self.__block_add_callbacks__ = False
85
             self.__block_add_callbacks__ = False
86
 
86
 
87
-    def __on_disconnect__(self, client, flags, rc):
88
-        logger.warning("Disconnect with rc=%d", rc)
87
+    def __on_disconnect__(self, client, userdata, flags, rc, properties):
88
+        logger.warning("Disconnect with rc=%s", repr(rc))
89
 
89
 
90
     def __receive__(self, client, userdata, message):
90
     def __receive__(self, client, userdata, message):
91
         get_topic_logger(message.topic).debug("Received message with topic %s and payload %s", message.topic, str(message.payload))
91
         get_topic_logger(message.topic).debug("Received message with topic %s and payload %s", message.topic, str(message.payload))

Loading…
Cancel
Save