From 680f414f766421b8b033d90dcc6f1820bfdc1917 Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Mon, 6 Jul 2026 17:41:15 +0200 Subject: [PATCH] Unload platforms in stiebel_eltron before closing connection in async_unload_entry (#175776) Co-authored-by: Claude Sonnet 4.6 --- .../components/stiebel_eltron/__init__.py | 6 +-- .../stiebel_eltron/quality_scale.yaml | 2 +- tests/components/stiebel_eltron/test_init.py | 41 ++++++++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/stiebel_eltron/__init__.py b/homeassistant/components/stiebel_eltron/__init__.py index 7802bb954b7b..187a7ae674af 100644 --- a/homeassistant/components/stiebel_eltron/__init__.py +++ b/homeassistant/components/stiebel_eltron/__init__.py @@ -45,6 +45,6 @@ async def async_unload_entry( entry: StiebelEltronConfigEntry, ) -> bool: """Unload a config entry.""" - coordinator = entry.runtime_data - await coordinator.close() - return await hass.config_entries.async_unload_platforms(entry, _PLATFORMS) + if unload_ok := await hass.config_entries.async_unload_platforms(entry, _PLATFORMS): + await entry.runtime_data.close() + return unload_ok diff --git a/homeassistant/components/stiebel_eltron/quality_scale.yaml b/homeassistant/components/stiebel_eltron/quality_scale.yaml index d476c182a829..183845abfa13 100644 --- a/homeassistant/components/stiebel_eltron/quality_scale.yaml +++ b/homeassistant/components/stiebel_eltron/quality_scale.yaml @@ -31,7 +31,7 @@ rules: # Silver action-exceptions: todo - config-entry-unloading: todo + config-entry-unloading: done docs-configuration-parameters: status: exempt comment: Integration does not have an options flow. diff --git a/tests/components/stiebel_eltron/test_init.py b/tests/components/stiebel_eltron/test_init.py index 44fa7259309a..c690ecb7a709 100644 --- a/tests/components/stiebel_eltron/test_init.py +++ b/tests/components/stiebel_eltron/test_init.py @@ -1,6 +1,6 @@ """Tests for the STIEBEL ELTRON integration.""" -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch from pymodbus.exceptions import ModbusException from pystiebeleltron import StiebelEltronModbusError @@ -89,3 +89,42 @@ async def test_async_setup_entry_coordinator_update_fails( assert result is False assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_unload_entry_closes_connection( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_lwz_api: MagicMock, +) -> None: + """Test unloading the config entry closes the Modbus connection.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert result is True + assert mock_config_entry.state is ConfigEntryState.NOT_LOADED + mock_lwz_api.close.assert_awaited_once() + + +async def test_unload_entry_does_not_close_connection_if_platform_unload_fails( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_lwz_api: MagicMock, +) -> None: + """Test the connection is not closed if platform unload fails.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + with patch( + "homeassistant.config_entries.ConfigEntries.async_unload_platforms", + return_value=False, + ): + result = await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert result is False + mock_lwz_api.close.assert_not_awaited()