fix: prevent MQTT from setting connected state on failed connections

When the MQTT broker rejects a connection (bad credentials, not
authorized, etc.), _on_connect still unconditionally sets connected=True,
subscribes to topics, and calls _set_initial_topics(). This causes
publish() to attempt sending messages to a broker that rejected the
connection.

Add an early return after logging the connection error so the client
correctly remains in a disconnected state.
This commit is contained in:
ryzendigo 2026-03-17 06:56:19 +08:00
parent 722ef6a1fe
commit 2233be8e7a

View File

@ -199,6 +199,7 @@ class MqttClient(Communicator):
"Unable to connect to MQTT server: Connection refused. Error code: "
+ reason_code.getName()
)
return
self.connected = True
logger.debug("MQTT connected")