From 988d72b8b61c49250c81a3067b7904e4d55ecb94 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Mon, 22 Jan 2024 17:40:20 +0100 Subject: [PATCH] Add helper function to update and reload config entry to config flow (#108034) * Add helper function to update and reload config entry to config flow * Use async_create_task * Remove await * Reload only when update & add task name * Rename function --- homeassistant/config_entries.py | 23 ++++++++++++++++++ tests/test_config_entries.py | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 819b813832d..79c36658417 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -1937,6 +1937,29 @@ class ConfigFlow(data_entry_flow.FlowHandler): return result + @callback + def async_update_reload_and_abort( + self, + entry: ConfigEntry, + title: str | UndefinedType = UNDEFINED, + data: Mapping[str, Any] | UndefinedType = UNDEFINED, + options: Mapping[str, Any] | UndefinedType = UNDEFINED, + reason: str = "reauth_successful", + ) -> data_entry_flow.FlowResult: + """Update config entry, reload config entry and finish config flow.""" + result = self.hass.config_entries.async_update_entry( + entry=entry, + title=title, + data=data, + options=options, + ) + if result: + self.hass.async_create_task( + self.hass.config_entries.async_reload(entry.entry_id), + f"config entry reload {entry.title} {entry.domain} {entry.entry_id}", + ) + return self.async_abort(reason=reason) + class OptionsFlowManager(data_entry_flow.FlowManager): """Flow to set options for a configuration entry.""" diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 2a56e34b981..14224b95fc9 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -4144,3 +4144,46 @@ def test_raise_trying_to_add_same_config_entry_twice( entry.add_to_hass(hass) entry.add_to_hass(hass) assert f"An entry with the id {entry.entry_id} already exists" in caplog.text + + +async def test_update_entry_and_reload( + hass: HomeAssistant, manager: config_entries.ConfigEntries +) -> None: + """Test updating an entry and reloading.""" + entry = MockConfigEntry( + domain="comp", + title="Test", + data={"vendor": "data"}, + options={"vendor": "options"}, + ) + entry.add_to_hass(hass) + + mock_integration( + hass, MockModule("comp", async_setup_entry=AsyncMock(return_value=True)) + ) + mock_platform(hass, "comp.config_flow", None) + + class MockFlowHandler(config_entries.ConfigFlow): + """Define a mock flow handler.""" + + VERSION = 1 + + async def async_step_reauth(self, data): + """Mock Reauth.""" + return self.async_update_reload_and_abort( + entry=entry, + title="Updated Title", + data={"vendor": "data2"}, + options={"vendor": "options2"}, + ) + + with patch.dict(config_entries.HANDLERS, {"comp": MockFlowHandler}): + task = await manager.flow.async_init("comp", context={"source": "reauth"}) + await hass.async_block_till_done() + + assert entry.title == "Updated Title" + assert entry.data == {"vendor": "data2"} + assert entry.options == {"vendor": "options2"} + assert entry.state == config_entries.ConfigEntryState.LOADED + assert task["type"] == FlowResultType.ABORT + assert task["reason"] == "reauth_successful"