@@ -169,6 +169,8 @@ class MQTT:
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.
171171 :param class user_data: arbitrary data to pass as a second argument to the callbacks.
172+ This works with all callbacks but "on_message"; there, it is necessary to extract
173+ the user_data from the MQTT object (passed as 1st argument) using the 'user_data' member.
172174
173175 """
174176
@@ -205,7 +207,7 @@ def __init__(
205207 self ._recv_timeout = recv_timeout
206208
207209 self .keep_alive = keep_alive
208- self ._user_data = user_data
210+ self .user_data = user_data
209211 self ._is_connected = False
210212 self ._msg_size_lim = MQTT_MSG_SZ_LIM
211213 self ._pid = 0
@@ -638,7 +640,7 @@ def _connect(
638640 self ._is_connected = True
639641 result = rc [0 ] & 1
640642 if self .on_connect is not None :
641- self .on_connect (self , self ._user_data , result , rc [2 ])
643+ self .on_connect (self , self .user_data , result , rc [2 ])
642644
643645 return result
644646
@@ -661,7 +663,7 @@ def disconnect(self) -> None:
661663 self ._is_connected = False
662664 self ._subscribed_topics = []
663665 if self .on_disconnect is not None :
664- self .on_disconnect (self , self ._user_data , 0 )
666+ self .on_disconnect (self , self .user_data , 0 )
665667
666668 def ping (self ) -> list [int ]:
667669 """Pings the MQTT Broker to confirm if the broker is alive or if
@@ -757,7 +759,7 @@ def publish(
757759 self ._sock .send (pub_hdr_var )
758760 self ._sock .send (msg )
759761 if qos == 0 and self .on_publish is not None :
760- self .on_publish (self , self ._user_data , topic , self ._pid )
762+ self .on_publish (self , self .user_data , topic , self ._pid )
761763 if qos == 1 :
762764 stamp = time .monotonic ()
763765 while True :
@@ -769,7 +771,7 @@ def publish(
769771 rcv_pid = rcv_pid_buf [0 ] << 0x08 | rcv_pid_buf [1 ]
770772 if self ._pid == rcv_pid :
771773 if self .on_publish is not None :
772- self .on_publish (self , self ._user_data , topic , rcv_pid )
774+ self .on_publish (self , self .user_data , topic , rcv_pid )
773775 return
774776
775777 if op is None :
@@ -849,7 +851,7 @@ def subscribe(self, topic: str, qos: int = 0) -> None:
849851
850852 for t , q in topics :
851853 if self .on_subscribe is not None :
852- self .on_subscribe (self , self ._user_data , t , q )
854+ self .on_subscribe (self , self .user_data , t , q )
853855 self ._subscribed_topics .append (t )
854856 return
855857
@@ -907,7 +909,7 @@ def unsubscribe(self, topic: str) -> None:
907909 assert rc [1 ] == packet_id_bytes [0 ] and rc [2 ] == packet_id_bytes [1 ]
908910 for t in topics :
909911 if self .on_unsubscribe is not None :
910- self .on_unsubscribe (self , self ._user_data , t , self ._pid )
912+ self .on_unsubscribe (self , self .user_data , t , self ._pid )
911913 self ._subscribed_topics .remove (t )
912914 return
913915
0 commit comments