Skip check for entry updated by current flow in _async_abort_entries_match (#141003)

* Ignore entries with source reconfigure in _async_abort_entries_match

* Exclude reconfigure and reauth entry from match check

* Add tests

* Fix tests for other components

* Revert unrelated changes

* Update docstring

* Make test more realistic

* Change name and docstring for sabnzbd test

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Andre Lengwenus
2025-05-09 16:15:17 +02:00
committed by GitHub
parent 3e0e807c96
commit b6c4b06fc7
4 changed files with 57 additions and 5 deletions

View File

@ -2839,10 +2839,16 @@ class ConfigFlow(ConfigEntryBaseFlow):
) -> None:
"""Abort if current entries match all data.
Do not abort for the entry that is being updated by the current flow.
Requires `already_configured` in strings.json in user visible flows.
"""
_async_abort_entries_match(
self._async_current_entries(include_ignore=False), match_dict
[
entry
for entry in self._async_current_entries(include_ignore=False)
if entry.entry_id != self.context.get("entry_id")
],
match_dict,
)
@callback

View File

@ -217,7 +217,7 @@ async def test_reauth_with_existing_config(hass: HomeAssistant) -> None:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "MYAPIKEY2",
CONF_API_KEY: MOCK_CONFIG[CONF_API_KEY],
},
)

View File

@ -153,10 +153,10 @@ async def test_abort_already_configured(
assert result["reason"] == "already_configured"
async def test_abort_reconfigure_already_configured(
async def test_abort_reconfigure_successful(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test that the reconfigure flow aborts if SABnzbd instance is already configured."""
"""Test that the reconfigure flow aborts successfully if SABnzbd instance is already configured."""
result = await config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
@ -166,4 +166,4 @@ async def test_abort_reconfigure_already_configured(
VALID_CONFIG,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert result["reason"] == "reconfigure_successful"

View File

@ -5268,6 +5268,52 @@ async def test_async_abort_entries_match(
assert result["reason"] == reason
@pytest.mark.parametrize(
("matchers", "reason"),
[
({"host": "3.4.5.6", "ip": "1.2.3.4", "port": 23}, "no_match"),
],
)
async def test_async_abort_entries_match_context(
hass: HomeAssistant,
manager: config_entries.ConfigEntries,
matchers: dict[str, str],
reason: str,
) -> None:
"""Test aborting if matching config entries exist."""
entry = MockConfigEntry(
domain="comp", data={"ip": "1.2.3.4", "host": "3.4.5.6", "port": 23}
)
entry.add_to_hass(hass)
mock_setup_entry = AsyncMock(return_value=True)
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
mock_platform(hass, "comp.config_flow", None)
class TestFlow(config_entries.ConfigFlow):
"""Test flow."""
VERSION = 1
async def async_step_reconfigure(self, user_input=None):
"""Test user step."""
self._async_abort_entries_match(matchers)
return self.async_abort(reason="no_match")
with mock_config_flow("comp", TestFlow), mock_config_flow("invalid_flow", 5):
result = await manager.flow.async_init(
"comp",
context={
"source": config_entries.SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == reason
@pytest.mark.parametrize(
("matchers", "reason"),
[