From 43dc5415de73c148d824b61c937f0dfa827df114 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Sun, 28 Apr 2024 01:42:38 +0200 Subject: [PATCH] Fix no will published when mqtt is down (#116319) --- homeassistant/components/mqtt/client.py | 3 ++- tests/components/mqtt/test_init.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index f01b8e80b3d..d094776efe0 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -710,7 +710,8 @@ class MQTT: async with self._connection_lock: self._should_reconnect = False self._async_cancel_reconnect() - self._mqttc.disconnect() + # We do not gracefully disconnect to ensure + # the broker publishes the will message @callback def async_restore_tracked_subscriptions( diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 9d135b89f36..cfb8ce7ac04 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -141,17 +141,17 @@ async def test_mqtt_connects_on_home_assistant_mqtt_setup( assert mqtt_client_mock.connect.call_count == 1 -async def test_mqtt_disconnects_on_home_assistant_stop( +async def test_mqtt_does_not_disconnect_on_home_assistant_stop( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator, mqtt_client_mock: MqttMockPahoClient, ) -> None: - """Test if client stops on HA stop.""" + """Test if client is not disconnected on HA stop.""" await mqtt_mock_entry() hass.bus.fire(EVENT_HOMEASSISTANT_STOP) await hass.async_block_till_done() await hass.async_block_till_done() - assert mqtt_client_mock.disconnect.call_count == 1 + assert mqtt_client_mock.disconnect.call_count == 0 async def test_mqtt_await_ack_at_disconnect(