Add default reconfigure reason in update_reload_and_abort (#127756)

* Add default reconfigure reason in async_update_reload_and_abort

* Simplify

* Fix test

* Add sample usage

* Remove multi-line ternary
This commit is contained in:
epenet 2024-10-07 09:02:58 +02:00 committed by GitHub
parent 4e650ec1ba
commit c87a2ca335
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 7 deletions

View File

@ -79,7 +79,6 @@ class BryantConfigFlow(ConfigFlow, domain=DOMAIN):
CONF_FILENAME: user_input[CONF_FILENAME],
CONF_SYSTEM_ZONE: system_zone,
},
reason="reconfigure_successful",
)
errors["base"] = "cannot_connect"
return self.async_show_form(

View File

@ -2731,7 +2731,7 @@ class ConfigFlow(ConfigEntryBaseFlow):
title: str | UndefinedType = UNDEFINED,
data: Mapping[str, Any] | UndefinedType = UNDEFINED,
options: Mapping[str, Any] | UndefinedType = UNDEFINED,
reason: str = "reauth_successful",
reason: str | UndefinedType = UNDEFINED,
reload_even_if_entry_is_unchanged: bool = True,
) -> ConfigFlowResult:
"""Update config entry, reload config entry and finish config flow."""
@ -2744,6 +2744,10 @@ class ConfigFlow(ConfigEntryBaseFlow):
)
if reload_even_if_entry_is_unchanged or result:
self.hass.config_entries.async_schedule_reload(entry.entry_id)
if reason is UNDEFINED:
reason = "reauth_successful"
if self.source == SOURCE_RECONFIGURE:
reason = "reconfigure_successful"
return self.async_abort(reason=reason)
def is_matching(self, other_flow: Self) -> bool:

View File

@ -2421,7 +2421,7 @@ async def test_supports_reconfigure(
data.pop("flow_id")
assert data == {
"handler": "test",
"reason": "reauth_successful",
"reason": "reconfigure_successful",
"type": "abort",
"description_placeholders": None,
}

View File

@ -5164,9 +5164,17 @@ def test_raise_trying_to_add_same_config_entry_twice(
"changed_entry_no_reload",
],
)
@pytest.mark.parametrize(
("source", "reason"),
[
(config_entries.SOURCE_REAUTH, "reauth_successful"),
(config_entries.SOURCE_RECONFIGURE, "reconfigure_successful"),
],
)
async def test_update_entry_and_reload(
hass: HomeAssistant,
manager: config_entries.ConfigEntries,
source: str,
reason: str,
title: tuple[str, str],
unique_id: tuple[str, str],
data_vendor: tuple[str, str],
@ -5210,8 +5218,22 @@ async def test_update_entry_and_reload(
**kwargs,
)
async def async_step_reconfigure(self, data):
"""Mock Reauth."""
return self.async_update_reload_and_abort(
entry=entry,
unique_id=unique_id[1],
title=title[1],
data={"vendor": data_vendor[1]},
options={"vendor": options_vendor[1]},
**kwargs,
)
with mock_config_flow("comp", MockFlowHandler):
task = await manager.flow.async_init("comp", context={"source": "reauth"})
if source == config_entries.SOURCE_REAUTH:
result = await entry.start_reauth_flow(hass)
elif source == config_entries.SOURCE_RECONFIGURE:
result = await entry.start_reconfigure_flow(hass)
await hass.async_block_till_done()
assert entry.title == title[1]
@ -5219,8 +5241,8 @@ async def test_update_entry_and_reload(
assert entry.data == {"vendor": data_vendor[1]}
assert entry.options == {"vendor": options_vendor[1]}
assert entry.state == config_entries.ConfigEntryState.LOADED
assert task["type"] == FlowResultType.ABORT
assert task["reason"] == "reauth_successful"
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == reason
# Assert entry was reloaded
assert len(comp.async_setup_entry.mock_calls) == calls_entry_load_unload[0]
assert len(comp.async_unload_entry.mock_calls) == calls_entry_load_unload[1]