Fix reload race in shelly when device is recently offline (#84328)

This commit is contained in:
J. Nick Koston 2022-12-20 22:29:48 -10:00 committed by GitHub
parent bbecf6ca37
commit e900918e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -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

View File

@ -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