From b95dfe2b00635b15228b04931c372efd85d12bf4 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:18:07 +0200 Subject: [PATCH] Add test helper for starting reconfiguration flow (#127154) --- tests/common.py | 19 +++++++++ tests/components/axis/test_config_flow.py | 9 +--- tests/test_config_entries.py | 50 +++++++++++++++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/tests/common.py b/tests/common.py index d53c3821364..924aeb81f98 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1066,6 +1066,25 @@ class MockConfigEntry(config_entries.ConfigEntry): """Start a reauthentication flow.""" return await start_reauth_flow(hass, self, context, data) + async def start_reconfigure_flow( + self, + hass: HomeAssistant, + context: dict[str, Any] | None = None, + data: dict[str, Any] | None = None, + ) -> ConfigFlowResult: + """Start a reconfiguration flow.""" + return await hass.config_entries.flow.async_init( + self.domain, + context={ + "source": config_entries.SOURCE_RECONFIGURE, + "entry_id": self.entry_id, + "title_placeholders": {"name": self.title}, + "unique_id": self.unique_id, + } + | (context or {}), + data=self.data | (data or {}), + ) + async def start_reauth_flow( hass: HomeAssistant, diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index 8591b4583c1..c8ffc46ca3f 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -17,7 +17,6 @@ from homeassistant.components.axis.const import ( ) from homeassistant.config_entries import ( SOURCE_DHCP, - SOURCE_RECONFIGURE, SOURCE_SSDP, SOURCE_USER, SOURCE_ZEROCONF, @@ -240,13 +239,7 @@ async def test_reconfiguration_flow_update_configuration( assert config_entry_setup.data[CONF_USERNAME] == "root" assert config_entry_setup.data[CONF_PASSWORD] == "pass" - result = await hass.config_entries.flow.async_init( - AXIS_DOMAIN, - context={ - "source": SOURCE_RECONFIGURE, - "entry_id": config_entry_setup.entry_id, - }, - ) + result = await config_entry_setup.start_reconfigure_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index e92095bad75..59ea7cdd808 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -6440,3 +6440,53 @@ async def test_reauth_helper_alignment( # Ensure context and init data are aligned assert helper_flow_context == reauth_flow_context assert helper_flow_init_data == reauth_flow_init_data + + +async def test_reconfigure_helper_alignment( + hass: HomeAssistant, + manager: config_entries.ConfigEntries, +) -> None: + """Test `start_reconfigure_flow` helper alignment. + + It should be aligned with `ConfigEntry._async_init_reconfigure`. + """ + entry = MockConfigEntry( + title="test_title", + domain="test", + entry_id="01J915Q6T9F6G5V0QJX6HBC94T", + data={"host": "any", "port": 123}, + unique_id=None, + ) + entry.add_to_hass(hass) + + mock_integration(hass, MockModule("test")) + mock_platform(hass, "test.config_flow", None) + + # Check context via auto-generated reconfigure + entry.async_start_reconfigure(hass) + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + + reconfigure_flow_context = flows[0]["context"] + reconfigure_flow_init_data = hass.config_entries.flow._progress[ + flows[0]["flow_id"] + ].init_data + + # Clear to make way for `start_reauth_flow` helper + manager.flow.async_abort(flows[0]["flow_id"]) + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 0 + + # Check context via `start_reconfigure_flow` helper + await entry.start_reconfigure_flow(hass) + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + helper_flow_context = flows[0]["context"] + helper_flow_init_data = hass.config_entries.flow._progress[ + flows[0]["flow_id"] + ].init_data + + # Ensure context and init data are aligned + assert helper_flow_context == reconfigure_flow_context + assert helper_flow_init_data == reconfigure_flow_init_data