@@ -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 :param bool use_imprecise_time: on boards without time.monotonic_ns() one has to set
173176 this to True in order to operate correctly over more than 24 days or so
174177
@@ -222,7 +225,7 @@ def __init__(
222225 self ._recv_timeout = recv_timeout
223226
224227 self .keep_alive = keep_alive
225- self ._user_data = user_data
228+ self .user_data = user_data
226229 self ._is_connected = False
227230 self ._msg_size_lim = MQTT_MSG_SZ_LIM
228231 self ._pid = 0
@@ -440,6 +443,11 @@ def add_topic_callback(self, mqtt_topic: str, callback_method) -> None:
440443
441444 :param str mqtt_topic: MQTT topic identifier.
442445 :param function callback_method: The callback method.
446+
447+ Expected method signature is ``on_message(client, topic, message)``
448+ To get access to the user_data, use the client argument.
449+
450+ If a callback is called for the topic, then any "on_message" callback will not be called.
443451 """
444452 if mqtt_topic is None or callback_method is None :
445453 raise ValueError ("MQTT topic and callback method must both be defined." )
@@ -464,6 +472,7 @@ def on_message(self):
464472 """Called when a new message has been received on a subscribed topic.
465473
466474 Expected method signature is ``on_message(client, topic, message)``
475+ To get access to the user_data, use the client argument.
467476 """
468477 return self ._on_message
469478
@@ -665,7 +674,7 @@ def _connect(
665674 self ._is_connected = True
666675 result = rc [0 ] & 1
667676 if self .on_connect is not None :
668- self .on_connect (self , self ._user_data , result , rc [2 ])
677+ self .on_connect (self , self .user_data , result , rc [2 ])
669678
670679 return result
671680
@@ -688,7 +697,7 @@ def disconnect(self) -> None:
688697 self ._is_connected = False
689698 self ._subscribed_topics = []
690699 if self .on_disconnect is not None :
691- self .on_disconnect (self , self ._user_data , 0 )
700+ self .on_disconnect (self , self .user_data , 0 )
692701
693702 def ping (self ) -> list [int ]:
694703 """Pings the MQTT Broker to confirm if the broker is alive or if
@@ -784,7 +793,7 @@ def publish(
784793 self ._sock .send (pub_hdr_var )
785794 self ._sock .send (msg )
786795 if qos == 0 and self .on_publish is not None :
787- self .on_publish (self , self ._user_data , topic , self ._pid )
796+ self .on_publish (self , self .user_data , topic , self ._pid )
788797 if qos == 1 :
789798 stamp = self .get_monotonic_time ()
790799 while True :
@@ -796,7 +805,7 @@ def publish(
796805 rcv_pid = rcv_pid_buf [0 ] << 0x08 | rcv_pid_buf [1 ]
797806 if self ._pid == rcv_pid :
798807 if self .on_publish is not None :
799- self .on_publish (self , self ._user_data , topic , rcv_pid )
808+ self .on_publish (self , self .user_data , topic , rcv_pid )
800809 return
801810
802811 if op is None :
@@ -876,7 +885,7 @@ def subscribe(self, topic: str, qos: int = 0) -> None:
876885
877886 for t , q in topics :
878887 if self .on_subscribe is not None :
879- self .on_subscribe (self , self ._user_data , t , q )
888+ self .on_subscribe (self , self .user_data , t , q )
880889 self ._subscribed_topics .append (t )
881890 return
882891
@@ -934,7 +943,7 @@ def unsubscribe(self, topic: str) -> None:
934943 assert rc [1 ] == packet_id_bytes [0 ] and rc [2 ] == packet_id_bytes [1 ]
935944 for t in topics :
936945 if self .on_unsubscribe is not None :
937- self .on_unsubscribe (self , self ._user_data , t , self ._pid )
946+ self .on_unsubscribe (self , self .user_data , t , self ._pid )
938947 self ._subscribed_topics .remove (t )
939948 return
940949
@@ -1013,7 +1022,7 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
10131022 :param float timeout: return after this timeout, in seconds.
10141023
10151024 """
1016-
1025+ self . _connected ()
10171026 self .logger .debug (f"waiting for messages for { timeout } seconds" )
10181027 if self ._timestamp == 0 :
10191028 self ._timestamp = self .get_monotonic_time ()
0 commit comments