diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index a4b2c20e2a5..4d286a355eb 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -669,25 +669,29 @@ class MQTT: result: int | None = None self._available_future = client_available self._should_reconnect = True + connect_args: list[Any] = [ + self.conf[CONF_BROKER], + self.conf.get(CONF_PORT, DEFAULT_PORT), + self.conf.get(CONF_KEEPALIVE, DEFAULT_KEEPALIVE), + "", # bind_address default + 0, # bind_port default + ] + if self.is_mqttv5: + # See: + # https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html + # `clean_start` (bool) – (MQTT v5.0 only) `True`, `False` or + # `MQTT_CLEAN_START_FIRST_ONLY`. Sets the MQTT v5.0 clean_start flag + # always, never or on the first successful connect only, + # respectively. MQTT session data (such as outstanding messages and + # subscriptions) is cleared on successful connect when the + # clean_start flag is set. For MQTT v3.1.1, the clean_session + # argument of Client should be used for similar result. + connect_args.append(True) # clean_start try: async with self._connection_lock, self._async_connect_in_executor(): result = await self.hass.async_add_executor_job( - self._mqttc.connect, - self.conf[CONF_BROKER], - self.conf.get(CONF_PORT, DEFAULT_PORT), - self.conf.get(CONF_KEEPALIVE, DEFAULT_KEEPALIVE), - "", # bind_address default - 0, # bind_port default - # See: - # https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html - # `clean_start` (bool) – (MQTT v5.0 only) `True`, `False` or - # `MQTT_CLEAN_START_FIRST_ONLY`. Sets the MQTT v5.0 clean_start flag - # always, never or on the first successful connect only, - # respectively. MQTT session data (such as outstanding messages and - # subscriptions) is cleared on successful connect when the - # clean_start flag is set. For MQTT v3.1.1, the clean_session - # argument of Client should be used for similar result. - True if self.is_mqttv5 else mqtt.MQTT_CLEAN_START_FIRST_ONLY, + self._mqttc.connect, # type: ignore[arg-type] + *connect_args, ) except (OSError, mqtt.WebsocketConnectionError) as err: _LOGGER.error("Failed to connect to MQTT server due to exception: %s", err) diff --git a/tests/components/mqtt/test_client.py b/tests/components/mqtt/test_client.py index c309bdaa1b8..2e7958035c1 100644 --- a/tests/components/mqtt/test_client.py +++ b/tests/components/mqtt/test_client.py @@ -1387,14 +1387,14 @@ async def test_setup_mqtt_client_clean_session_and_protocol( mqtt.CONF_BROKER: "mock-broker", CONF_PROTOCOL: "3.1", }, - call("mock-broker", 1883, 60, "", 0, 3), + call("mock-broker", 1883, 60, "", 0), ), ( { mqtt.CONF_BROKER: "mock-broker", CONF_PROTOCOL: "3.1.1", }, - call("mock-broker", 1883, 60, "", 0, 3), + call("mock-broker", 1883, 60, "", 0), ), ( { @@ -1412,7 +1412,10 @@ async def test_setup_mqtt_client_clean_start( mqtt_client_mock: MqttMockPahoClient, connect_args: tuple[Any], ) -> None: - """Test MQTT client protocol connects with `clean_start` set correctly.""" + """Test MQTT client protocol connects with `clean_start` set correctly. + + When MQTT v5 is used, we set `clean_start` to `True` on connect. + """ await mqtt_mock_entry() # check if clean_start was set correctly