Do not pass default value for clean_start on_connect

This commit is contained in:
jbouwh
2025-02-16 13:11:06 +00:00
parent 4594f7e07d
commit 75806736cf
2 changed files with 26 additions and 19 deletions

View File

@@ -669,15 +669,14 @@ class MQTT:
result: int | None = None result: int | None = None
self._available_future = client_available self._available_future = client_available
self._should_reconnect = True self._should_reconnect = True
try: connect_args: list[Any] = [
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[CONF_BROKER],
self.conf.get(CONF_PORT, DEFAULT_PORT), self.conf.get(CONF_PORT, DEFAULT_PORT),
self.conf.get(CONF_KEEPALIVE, DEFAULT_KEEPALIVE), self.conf.get(CONF_KEEPALIVE, DEFAULT_KEEPALIVE),
"", # bind_address default "", # bind_address default
0, # bind_port default 0, # bind_port default
]
if self.is_mqttv5:
# See: # See:
# https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html # https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html
# `clean_start` (bool) (MQTT v5.0 only) `True`, `False` or # `clean_start` (bool) (MQTT v5.0 only) `True`, `False` or
@@ -687,7 +686,12 @@ class MQTT:
# subscriptions) is cleared on successful connect when the # subscriptions) is cleared on successful connect when the
# clean_start flag is set. For MQTT v3.1.1, the clean_session # clean_start flag is set. For MQTT v3.1.1, the clean_session
# argument of Client should be used for similar result. # argument of Client should be used for similar result.
True if self.is_mqttv5 else mqtt.MQTT_CLEAN_START_FIRST_ONLY, 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, # type: ignore[arg-type]
*connect_args,
) )
except (OSError, mqtt.WebsocketConnectionError) as err: except (OSError, mqtt.WebsocketConnectionError) as err:
_LOGGER.error("Failed to connect to MQTT server due to exception: %s", err) _LOGGER.error("Failed to connect to MQTT server due to exception: %s", err)

View File

@@ -1387,14 +1387,14 @@ async def test_setup_mqtt_client_clean_session_and_protocol(
mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1", CONF_PROTOCOL: "3.1",
}, },
call("mock-broker", 1883, 60, "", 0, 3), call("mock-broker", 1883, 60, "", 0),
), ),
( (
{ {
mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1.1", 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, mqtt_client_mock: MqttMockPahoClient,
connect_args: tuple[Any], connect_args: tuple[Any],
) -> None: ) -> 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() await mqtt_mock_entry()
# check if clean_start was set correctly # check if clean_start was set correctly