From 3ef9f7360fc96bb10a67453e70af8953b59497ed Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 16 Dec 2024 12:25:49 +0100 Subject: [PATCH] Add subentry reconfigure support to kitchen_sink --- .../components/kitchen_sink/config_flow.py | 46 +++++++++++++----- .../kitchen_sink/test_config_flow.py | 48 ++++++++++++++++++- 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/kitchen_sink/config_flow.py b/homeassistant/components/kitchen_sink/config_flow.py index 9cdd44802df..47f0bf9a5ee 100644 --- a/homeassistant/components/kitchen_sink/config_flow.py +++ b/homeassistant/components/kitchen_sink/config_flow.py @@ -37,20 +37,13 @@ class KitchenSinkConfigFlow(ConfigFlow, domain=DOMAIN): """Get the options flow for this handler.""" return OptionsFlowHandler() - @staticmethod - @callback - def async_get_subentry_flow( - config_entry: ConfigEntry, subentry_type: str - ) -> ConfigSubentryFlow: - """Get the subentry flow for this handler.""" - - return SubentryFlowHandler() - @classmethod @callback - def async_supported_subentries(cls, config_entry: ConfigEntry) -> tuple[str, ...]: + def async_get_supported_subentry_flows( + cls, config_entry: ConfigEntry + ) -> dict[str, type[ConfigSubentryFlow]]: """Return subentries supported by this handler.""" - return ("add_entity",) + return {"add_entity": SubentryFlowHandler} async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Set the config entry up from yaml.""" @@ -116,7 +109,7 @@ class OptionsFlowHandler(OptionsFlow): class SubentryFlowHandler(ConfigSubentryFlow): """Handle subentry flow.""" - async def async_step_init( + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> SubentryFlowResult: """Manage the options.""" @@ -139,3 +132,32 @@ class SubentryFlowHandler(ConfigSubentryFlow): } ), ) + + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> SubentryFlowResult: + """Manage the options.""" + return await self.async_step_reconfigure_sensor() + + async def async_step_reconfigure_sensor( + self, user_input: dict[str, Any] | None = None + ) -> SubentryFlowResult: + """Add a new sensor.""" + if user_input is not None: + title = user_input.pop("name") + return self.async_update_and_abort( + self._get_reconfigure_entry(), + self._get_reconfigure_subentry(), + data=user_input, + title=title, + ) + + return self.async_show_form( + step_id="reconfigure_sensor", + data_schema=vol.Schema( + { + vol.Required("name"): str, + vol.Required("state"): int, + } + ), + ) diff --git a/tests/components/kitchen_sink/test_config_flow.py b/tests/components/kitchen_sink/test_config_flow.py index d78fc08f470..3b52ea82bcb 100644 --- a/tests/components/kitchen_sink/test_config_flow.py +++ b/tests/components/kitchen_sink/test_config_flow.py @@ -116,7 +116,8 @@ async def test_subentry_flow(hass: HomeAssistant) -> None: await hass.async_block_till_done() result = await hass.config_entries.subentries.async_init( - (config_entry.entry_id, "add_entity") + (config_entry.entry_id, "add_entity"), + context={"source": config_entries.SOURCE_USER}, ) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "add_sensor" @@ -137,3 +138,48 @@ async def test_subentry_flow(hass: HomeAssistant) -> None: } await hass.async_block_till_done() + + +@pytest.mark.usefixtures("no_platforms") +async def test_subentry_reconfigure_flow(hass: HomeAssistant) -> None: + """Test config flow options.""" + subentry_id = "mock_id" + config_entry = MockConfigEntry( + domain=DOMAIN, + subentries_data=[ + config_entries.ConfigSubentryData( + data={"state": 15}, + subentry_id="mock_id", + title="Sensor 1", + unique_id=None, + ) + ], + ) + config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + result = await config_entry.start_subentry_reconfigure_flow( + hass, "add_entity", subentry_id + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure_sensor" + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], + user_input={"name": "Renamed sensor 1", "state": 5}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + + assert config_entry.subentries == { + subentry_id: config_entries.ConfigSubentry( + data={"state": 5}, + subentry_id=subentry_id, + title="Renamed sensor 1", + unique_id=None, + ) + } + + await hass.async_block_till_done()