diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 1e77fae439e..7ccad2bffc1 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -1,6 +1,7 @@ """The Shelly integration.""" from __future__ import annotations +import contextlib from typing import Any, Final import aioshelly @@ -311,7 +312,13 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry, platforms ): if shelly_entry_data.rpc: - await shelly_entry_data.rpc.shutdown() + with contextlib.suppress(DeviceConnectionError): + # If the device is restarting or has gone offline before + # the ping/pong timeout happens, the shutdown command + # will fail, but we don't care since we are unloading + # and if we setup again, we will fix anything that is + # in an inconsistent state at that time. + await shelly_entry_data.rpc.shutdown() get_entry_data(hass).pop(entry.entry_id) return unload_ok diff --git a/tests/components/shelly/test_init.py b/tests/components/shelly/test_init.py index 48ea978e5d9..0697c6fb613 100644 --- a/tests/components/shelly/test_init.py +++ b/tests/components/shelly/test_init.py @@ -211,3 +211,30 @@ async def test_entry_unload_not_connected(hass, mock_rpc_device, monkeypatch): assert not mock_stop_scanner.call_count assert entry.state is ConfigEntryState.LOADED + + +async def test_entry_unload_not_connected_but_we_with_we_are( + hass, mock_rpc_device, monkeypatch +): + """Test entry unload when not connected but we think we are still connected.""" + with patch( + "homeassistant.components.shelly.coordinator.async_stop_scanner", + side_effect=DeviceConnectionError, + ) as mock_stop_scanner: + + entry = await init_integration( + hass, 2, options={CONF_BLE_SCANNER_MODE: BLEScannerMode.ACTIVE} + ) + entity_id = "switch.test_switch_0" + + assert entry.state is ConfigEntryState.LOADED + assert hass.states.get(entity_id).state is STATE_ON + assert not mock_stop_scanner.call_count + + monkeypatch.setattr(mock_rpc_device, "connected", False) + + await hass.config_entries.async_reload(entry.entry_id) + await hass.async_block_till_done() + + assert not mock_stop_scanner.call_count + assert entry.state is ConfigEntryState.LOADED