From c87a2ca335885860acbbfa3ac7cd25acc269f2f1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:02:58 +0200 Subject: [PATCH] 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 --- .../bryant_evolution/config_flow.py | 1 - homeassistant/config_entries.py | 6 +++- .../components/config/test_config_entries.py | 2 +- tests/test_config_entries.py | 30 ++++++++++++++++--- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/bryant_evolution/config_flow.py b/homeassistant/components/bryant_evolution/config_flow.py index 65ee394ef88..9e115bd69ee 100644 --- a/homeassistant/components/bryant_evolution/config_flow.py +++ b/homeassistant/components/bryant_evolution/config_flow.py @@ -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( diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index ee93f987a79..28fecf9bcc4 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -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: diff --git a/tests/components/config/test_config_entries.py b/tests/components/config/test_config_entries.py index 1b0e9dc7402..6fac86b6c81 100644 --- a/tests/components/config/test_config_entries.py +++ b/tests/components/config/test_config_entries.py @@ -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, } diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 0ab8620057d..300ebce491c 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -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]