Fix check for duplicate config entry reauth when context is passed or augmented (#81753)

fixes https://github.com/home-assistant/core/issues/77578
This commit is contained in:
J. Nick Koston 2022-11-07 21:19:57 -06:00 committed by GitHub
parent 785cf0e29c
commit c2c26e2608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 15 deletions

View File

@ -660,24 +660,25 @@ class ConfigEntry:
data: dict[str, Any] | None = None, data: dict[str, Any] | None = None,
) -> None: ) -> None:
"""Start a reauth flow.""" """Start a reauth flow."""
flow_context = { if any(
"source": SOURCE_REAUTH, flow
"entry_id": self.entry_id, for flow in hass.config_entries.flow.async_progress()
"title_placeholders": {"name": self.title}, if flow["context"].get("source") == SOURCE_REAUTH
"unique_id": self.unique_id, and flow["context"].get("entry_id") == self.entry_id
} ):
# Reauth flow already in progress for this entry
if context: return
flow_context.update(context)
for flow in hass.config_entries.flow.async_progress_by_handler(self.domain):
if flow["context"] == flow_context:
return
hass.async_create_task( hass.async_create_task(
hass.config_entries.flow.async_init( hass.config_entries.flow.async_init(
self.domain, self.domain,
context=flow_context, context={
"source": SOURCE_REAUTH,
"entry_id": self.entry_id,
"title_placeholders": {"name": self.title},
"unique_id": self.unique_id,
}
| (context or {}),
data=self.data | (data or {}), data=self.data | (data or {}),
) )
) )

View File

@ -3287,6 +3287,7 @@ async def test_disallow_entry_reload_with_setup_in_progresss(hass, manager):
async def test_reauth(hass): async def test_reauth(hass):
"""Test the async_reauth_helper.""" """Test the async_reauth_helper."""
entry = MockConfigEntry(title="test_title", domain="test") entry = MockConfigEntry(title="test_title", domain="test")
entry2 = MockConfigEntry(title="test_title", domain="test")
mock_setup_entry = AsyncMock(return_value=True) mock_setup_entry = AsyncMock(return_value=True)
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry)) mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
@ -3313,7 +3314,19 @@ async def test_reauth(hass):
assert mock_init.call_args.kwargs["data"]["extra_data"] == 1234 assert mock_init.call_args.kwargs["data"]["extra_data"] == 1234
assert entry.entry_id != entry2.entry_id
# Check we can't start duplicate flows # Check we can't start duplicate flows
entry.async_start_reauth(hass, {"extra_context": "some_extra_context"}) entry.async_start_reauth(hass, {"extra_context": "some_extra_context"})
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(flows) == 1 assert len(hass.config_entries.flow.async_progress()) == 1
# Check we can't start duplicate when the context context is different
entry.async_start_reauth(hass, {"diff": "diff"})
await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 1
# Check we can start a reauth for a different entry
entry2.async_start_reauth(hass, {"extra_context": "some_extra_context"})
await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 2