2828
2929
3030class MqttClientAdapter (threading .Thread ):
31- def __init__ (self , on_message_callback : t .Optional [t .Callable ] = None , host : str = "localhost" , port : int = 1883 ):
31+ def __init__ (
32+ self ,
33+ on_message_callback : t .Optional [t .Callable ] = None ,
34+ host : str = "localhost" ,
35+ port : int = 1883 ,
36+ username : str = "guest" ,
37+ password : str = "guest" ,
38+ ):
3239 super ().__init__ ()
3340 self .client : mqtt .Client
3441 if not hasattr (mqtt , "CallbackAPIVersion" ):
@@ -42,6 +49,9 @@ def __init__(self, on_message_callback: t.Optional[t.Callable] = None, host: str
4249 self .on_message_callback = on_message_callback
4350 self .host = host
4451 self .port = int (port )
52+ self .username = username
53+ self .password = password
54+
4555 self .setup ()
4656
4757 def setup (self ):
@@ -52,6 +62,7 @@ def setup(self):
5262 client .on_message = self .on_message
5363 if self .on_message_callback :
5464 client .on_message = self .on_message_callback
65+ client .username_pw_set (self .username , self .password )
5566
5667 logger .debug ("[PYTEST] Connecting to MQTT broker" )
5768 client .connect (host = self .host , port = self .port )
@@ -92,12 +103,21 @@ def publish(self, topic: str, payload: str, **kwargs) -> mqtt.MQTTMessageInfo:
92103class MqttCaptureFixture :
93104 """Provides access and control of log capturing."""
94105
95- def __init__ (self , decode_utf8 : t .Optional [bool ], host : str = "localhost" , port : int = 1883 ) -> None :
106+ def __init__ (
107+ self ,
108+ decode_utf8 : t .Optional [bool ],
109+ host : str = "localhost" ,
110+ port : int = 1883 ,
111+ username : str = "guest" ,
112+ password : str = "guest" ,
113+ ) -> None :
96114 """Creates a new funcarg."""
97115 self ._buffer : t .List [MqttMessage ] = []
98116 self ._decode_utf8 : bool = decode_utf8 or False
99117
100- self .mqtt_client = MqttClientAdapter (on_message_callback = self .on_message , host = host , port = port )
118+ self .mqtt_client = MqttClientAdapter (
119+ on_message_callback = self .on_message , host = host , port = port , username = username , password = password
120+ )
101121 self .mqtt_client .start ()
102122 # time.sleep(0.1)
103123
@@ -145,13 +165,16 @@ def capmqtt(request, mqtt_settings: MqttSettings):
145165 # https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#using-markers-to-pass-data-to-fixtures
146166
147167 host , port = mqtt_settings .host , mqtt_settings .port
168+ username , password = mqtt_settings .username , mqtt_settings .password
148169
149170 capmqtt_decode_utf8 = (
150171 getattr (request .config .option , "capmqtt_decode_utf8" , False )
151172 or getattr (request .module , "capmqtt_decode_utf8" , False )
152173 or request .node .get_closest_marker ("capmqtt_decode_utf8" ) is not None
153174 )
154- result = MqttCaptureFixture (decode_utf8 = capmqtt_decode_utf8 , host = host , port = port )
175+ result = MqttCaptureFixture (
176+ decode_utf8 = capmqtt_decode_utf8 , host = host , port = port , username = username , password = password
177+ )
155178 delay ()
156179 yield result
157180 result .finalize ()
0 commit comments