Abort existing reauth flow on entry removal (#52407)

This commit is contained in:
Franck Nijhof 2021-07-02 20:56:51 +02:00 committed by GitHub
parent 7f3f6757ea
commit b91b553ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -819,6 +819,19 @@ class ConfigEntries:
dev_reg.async_clear_config_entry(entry_id)
ent_reg.async_clear_config_entry(entry_id)
# If the configuration entry is removed during reauth, it should
# abort any reauth flow that is active for the removed entry.
for progress_flow in self.hass.config_entries.flow.async_progress():
context = progress_flow.get("context")
if (
context
and context["source"] == SOURCE_REAUTH
and "entry_id" in context
and context["entry_id"] == entry_id
and "flow_id" in progress_flow
):
self.hass.config_entries.flow.async_abort(progress_flow["flow_id"])
# After we have fully removed an "ignore" config entry we can try and rediscover it so that a
# user is able to immediately start configuring it. We do this by starting a new flow with
# the 'unignore' step. If the integration doesn't implement async_step_unignore then

View File

@ -337,6 +337,30 @@ async def test_remove_entry(hass, manager):
assert not entity_entry_list
async def test_remove_entry_cancels_reauth(hass, manager):
"""Tests that removing a config entry, also aborts existing reauth flows."""
entry = MockConfigEntry(title="test_title", domain="test")
mock_setup_entry = AsyncMock(side_effect=ConfigEntryAuthFailed())
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
mock_entity_platform(hass, "config_flow.test", None)
entry.add_to_hass(hass)
await entry.async_setup(hass)
await hass.async_block_till_done()
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["context"]["entry_id"] == entry.entry_id
assert flows[0]["context"]["source"] == config_entries.SOURCE_REAUTH
assert entry.state is config_entries.ConfigEntryState.SETUP_ERROR
await manager.async_remove(entry.entry_id)
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 0
async def test_remove_entry_handles_callback_error(hass, manager):
"""Test that exceptions in the remove callback are handled."""
mock_setup_entry = AsyncMock(return_value=True)