Close the SMA session when unloading the config entry (#183286)

This commit is contained in:
Erwin Douna
2026-09-27 13:13:57 +02:00
committed by GitHub
parent 6d1815c9d8
commit acdf77ad47
3 changed files with 33 additions and 1 deletions
+1
View File
@@ -43,6 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: SMAConfigEntry) -> bool:
)
coordinator = SMADataUpdateCoordinator(hass, entry, sma)
entry.async_on_unload(coordinator.async_close_sma_session)
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator
+5 -1
View File
@@ -107,5 +107,9 @@ class SMADataUpdateCoordinator(DataUpdateCoordinator[SMACoordinatorData]):
async def async_close_sma_session(self) -> None:
"""Close the SMA session."""
await self.sma.close_session()
try:
await self.sma.close_session()
except SmaConnectionException as err:
_LOGGER.debug("Could not close the SMA session: %s", err)
return
_LOGGER.debug("SMA session closed")
+27
View File
@@ -56,3 +56,30 @@ async def test_setup_exceptions(
mock_sma_client.device_info.side_effect = exception
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is expected_state
@pytest.mark.parametrize(
"close_session_error",
[
pytest.param(None, id="closed"),
pytest.param(SmaConnectionException, id="unreachable"),
],
)
async def test_unload_closes_session(
hass: HomeAssistant,
mock_sma_client: MagicMock,
mock_config_entry: MockConfigEntry,
close_session_error: type[Exception] | None,
) -> None:
"""Test unloading the entry closes the SMA session, even if unreachable."""
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED
mock_sma_client.close_session.assert_not_called()
mock_sma_client.close_session.side_effect = close_session_error
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
mock_sma_client.close_session.assert_called_once()