1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add async_schedule_reload helper to the ConfigEntries manager (#110912)

* Add async_schedule_reload helper to the ConfigEntries manager

We have cases where the the setup retry kicks in right before
the reload happens causing the reload to fail with
OperationNotAllowed. The async_schedule_reload will
cancel the setup retry before the async_reload task
is created to avoid this problem.

I updated a few integrations that were most likely
to have this problem. Future PRs will do a more
extensive audit

* coverage

* revert for now since this needs more refactoring in a followup

* cover

* cleanup and fixes
This commit is contained in:
J. Nick Koston
2024-02-19 19:14:45 -06:00
committed by GitHub
parent 9361f3c443
commit ae49b3a274
8 changed files with 74 additions and 53 deletions

View File

@@ -3602,6 +3602,39 @@ async def test_setup_retrying_during_shutdown(hass: HomeAssistant) -> None:
entry.async_cancel_retry_setup()
async def test_scheduling_reload_cancels_setup_retry(hass: HomeAssistant) -> None:
"""Test scheduling a reload cancels setup retry."""
entry = MockConfigEntry(domain="test")
entry.add_to_hass(hass)
mock_setup_entry = AsyncMock(side_effect=ConfigEntryNotReady)
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
mock_platform(hass, "test.config_flow", None)
cancel_mock = Mock()
with patch(
"homeassistant.config_entries.async_call_later", return_value=cancel_mock
):
await entry.async_setup(hass)
assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY
assert len(cancel_mock.mock_calls) == 0
mock_setup_entry.side_effect = None
mock_setup_entry.return_value = True
hass.config_entries.async_schedule_reload(entry.entry_id)
assert len(cancel_mock.mock_calls) == 1
await hass.async_block_till_done()
assert entry.state is config_entries.ConfigEntryState.LOADED
async def test_scheduling_reload_unknown_entry(hass: HomeAssistant) -> None:
"""Test scheduling a reload raises with an unknown entry."""
with pytest.raises(config_entries.UnknownEntry):
hass.config_entries.async_schedule_reload("non-existing")
@pytest.mark.parametrize(
("matchers", "reason"),
[
@@ -3947,7 +3980,7 @@ async def test_unique_id_update_while_setup_in_progress(
assert entry.state is config_entries.ConfigEntryState.LOADED
async def test_disallow_entry_reload_with_setup_in_progresss(
async def test_disallow_entry_reload_with_setup_in_progress(
hass: HomeAssistant, manager: config_entries.ConfigEntries
) -> None:
"""Test we do not allow reload while the config entry is still setting up."""