@@ -168,7 +168,10 @@ class MQTT:
168
168
in seconds.
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
- :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.
172
175
173
176
"""
174
177
@@ -205,7 +208,7 @@ def __init__(
205
208
self ._recv_timeout = recv_timeout
206
209
207
210
self .keep_alive = keep_alive
208
- self ._user_data = user_data
211
+ self .user_data = user_data
209
212
self ._is_connected = False
210
213
self ._msg_size_lim = MQTT_MSG_SZ_LIM
211
214
self ._pid = 0
@@ -413,6 +416,11 @@ def add_topic_callback(self, mqtt_topic: str, callback_method) -> None:
413
416
414
417
:param str mqtt_topic: MQTT topic identifier.
415
418
: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.
416
424
"""
417
425
if mqtt_topic is None or callback_method is None :
418
426
raise ValueError ("MQTT topic and callback method must both be defined." )
@@ -437,6 +445,7 @@ def on_message(self):
437
445
"""Called when a new message has been received on a subscribed topic.
438
446
439
447
Expected method signature is ``on_message(client, topic, message)``
448
+ To get access to the user_data, use the client argument.
440
449
"""
441
450
return self ._on_message
442
451
@@ -638,7 +647,7 @@ def _connect(
638
647
self ._is_connected = True
639
648
result = rc [0 ] & 1
640
649
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 ])
642
651
643
652
return result
644
653
@@ -661,7 +670,7 @@ def disconnect(self) -> None:
661
670
self ._is_connected = False
662
671
self ._subscribed_topics = []
663
672
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 )
665
674
666
675
def ping (self ) -> list [int ]:
667
676
"""Pings the MQTT Broker to confirm if the broker is alive or if
@@ -757,7 +766,7 @@ def publish(
757
766
self ._sock .send (pub_hdr_var )
758
767
self ._sock .send (msg )
759
768
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 )
761
770
if qos == 1 :
762
771
stamp = time .monotonic ()
763
772
while True :
@@ -769,7 +778,7 @@ def publish(
769
778
rcv_pid = rcv_pid_buf [0 ] << 0x08 | rcv_pid_buf [1 ]
770
779
if self ._pid == rcv_pid :
771
780
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 )
773
782
return
774
783
775
784
if op is None :
@@ -849,7 +858,7 @@ def subscribe(self, topic: str, qos: int = 0) -> None:
849
858
850
859
for t , q in topics :
851
860
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 )
853
862
self ._subscribed_topics .append (t )
854
863
return
855
864
@@ -907,7 +916,7 @@ def unsubscribe(self, topic: str) -> None:
907
916
assert rc [1 ] == packet_id_bytes [0 ] and rc [2 ] == packet_id_bytes [1 ]
908
917
for t in topics :
909
918
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 )
911
920
self ._subscribed_topics .remove (t )
912
921
return
913
922
0 commit comments