@@ -169,6 +169,8 @@ class MQTT:
169
169
:param int connect_retries: How many times to try to connect to the broker before giving up
170
170
on connect or reconnect. Exponential backoff will be used for the retries.
171
171
: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.
172
174
173
175
"""
174
176
@@ -205,7 +207,7 @@ def __init__(
205
207
self ._recv_timeout = recv_timeout
206
208
207
209
self .keep_alive = keep_alive
208
- self ._user_data = user_data
210
+ self .user_data = user_data
209
211
self ._is_connected = False
210
212
self ._msg_size_lim = MQTT_MSG_SZ_LIM
211
213
self ._pid = 0
@@ -638,7 +640,7 @@ def _connect(
638
640
self ._is_connected = True
639
641
result = rc [0 ] & 1
640
642
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 ])
642
644
643
645
return result
644
646
@@ -661,7 +663,7 @@ def disconnect(self) -> None:
661
663
self ._is_connected = False
662
664
self ._subscribed_topics = []
663
665
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 )
665
667
666
668
def ping (self ) -> list [int ]:
667
669
"""Pings the MQTT Broker to confirm if the broker is alive or if
@@ -757,7 +759,7 @@ def publish(
757
759
self ._sock .send (pub_hdr_var )
758
760
self ._sock .send (msg )
759
761
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 )
761
763
if qos == 1 :
762
764
stamp = time .monotonic ()
763
765
while True :
@@ -769,7 +771,7 @@ def publish(
769
771
rcv_pid = rcv_pid_buf [0 ] << 0x08 | rcv_pid_buf [1 ]
770
772
if self ._pid == rcv_pid :
771
773
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 )
773
775
return
774
776
775
777
if op is None :
@@ -849,7 +851,7 @@ def subscribe(self, topic: str, qos: int = 0) -> None:
849
851
850
852
for t , q in topics :
851
853
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 )
853
855
self ._subscribed_topics .append (t )
854
856
return
855
857
@@ -907,7 +909,7 @@ def unsubscribe(self, topic: str) -> None:
907
909
assert rc [1 ] == packet_id_bytes [0 ] and rc [2 ] == packet_id_bytes [1 ]
908
910
for t in topics :
909
911
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 )
911
913
self ._subscribed_topics .remove (t )
912
914
return
913
915
0 commit comments