@@ -168,7 +168,10 @@ class MQTT:
168168 in seconds.
169169 :param int connect_retries: How many times to try to connect to the broker before giving up
170170 on connect or reconnect. Exponential backoff will be used for the retries.
171- :param class user_data: arbitrary data to pass as a second argument to the callbacks.
171+ :param class user_data: arbitrary data to pass as a second argument to most of the callbacks.
172+ This works with all callbacks but the "on_message" and those added via add_topic_callback();
173+ for those, to get access to the user_data use the 'user_data' member of the MQTT object
174+ passed as 1st argument.
172175
173176 """
174177
@@ -205,7 +208,7 @@ def __init__(
205208 self ._recv_timeout = recv_timeout
206209
207210 self .keep_alive = keep_alive
208- self ._user_data = user_data
211+ self .user_data = user_data
209212 self ._is_connected = False
210213 self ._msg_size_lim = MQTT_MSG_SZ_LIM
211214 self ._pid = 0
@@ -413,6 +416,11 @@ def add_topic_callback(self, mqtt_topic: str, callback_method) -> None:
413416
414417 :param str mqtt_topic: MQTT topic identifier.
415418 :param function callback_method: The callback method.
419+
420+ Expected method signature is ``on_message(client, topic, message)``
421+ To get access to the user_data, use the client argument.
422+
423+ If a callback is called for the topic, then any "on_message" callback will not be called.
416424 """
417425 if mqtt_topic is None or callback_method is None :
418426 raise ValueError ("MQTT topic and callback method must both be defined." )
@@ -437,6 +445,7 @@ def on_message(self):
437445 """Called when a new message has been received on a subscribed topic.
438446
439447 Expected method signature is ``on_message(client, topic, message)``
448+ To get access to the user_data, use the client argument.
440449 """
441450 return self ._on_message
442451
@@ -638,7 +647,7 @@ def _connect(
638647 self ._is_connected = True
639648 result = rc [0 ] & 1
640649 if self .on_connect is not None :
641- self .on_connect (self , self ._user_data , result , rc [2 ])
650+ self .on_connect (self , self .user_data , result , rc [2 ])
642651
643652 return result
644653
@@ -661,7 +670,7 @@ def disconnect(self) -> None:
661670 self ._is_connected = False
662671 self ._subscribed_topics = []
663672 if self .on_disconnect is not None :
664- self .on_disconnect (self , self ._user_data , 0 )
673+ self .on_disconnect (self , self .user_data , 0 )
665674
666675 def ping (self ) -> list [int ]:
667676 """Pings the MQTT Broker to confirm if the broker is alive or if
@@ -757,7 +766,7 @@ def publish(
757766 self ._sock .send (pub_hdr_var )
758767 self ._sock .send (msg )
759768 if qos == 0 and self .on_publish is not None :
760- self .on_publish (self , self ._user_data , topic , self ._pid )
769+ self .on_publish (self , self .user_data , topic , self ._pid )
761770 if qos == 1 :
762771 stamp = time .monotonic ()
763772 while True :
@@ -769,7 +778,7 @@ def publish(
769778 rcv_pid = rcv_pid_buf [0 ] << 0x08 | rcv_pid_buf [1 ]
770779 if self ._pid == rcv_pid :
771780 if self .on_publish is not None :
772- self .on_publish (self , self ._user_data , topic , rcv_pid )
781+ self .on_publish (self , self .user_data , topic , rcv_pid )
773782 return
774783
775784 if op is None :
@@ -849,7 +858,7 @@ def subscribe(self, topic: str, qos: int = 0) -> None:
849858
850859 for t , q in topics :
851860 if self .on_subscribe is not None :
852- self .on_subscribe (self , self ._user_data , t , q )
861+ self .on_subscribe (self , self .user_data , t , q )
853862 self ._subscribed_topics .append (t )
854863 return
855864
@@ -907,7 +916,7 @@ def unsubscribe(self, topic: str) -> None:
907916 assert rc [1 ] == packet_id_bytes [0 ] and rc [2 ] == packet_id_bytes [1 ]
908917 for t in topics :
909918 if self .on_unsubscribe is not None :
910- self .on_unsubscribe (self , self ._user_data , t , self ._pid )
919+ self .on_unsubscribe (self , self .user_data , t , self ._pid )
911920 self ._subscribed_topics .remove (t )
912921 return
913922
0 commit comments