From 4aeb50bfe346f53a444f86de24304dcf79ffecc9 Mon Sep 17 00:00:00 2001 From: edgimar Date: Fri, 5 Dec 2025 07:15:24 -0500 Subject: [PATCH] feat: add option to duplicate existing lighting instance (#1329) * feat: add option to duplicate existing lighting instance A menu step in the config flow was added that allows a user to create a new instance or duplicate the options of an existing one. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add type annotation, tests, and translations for duplicate feature - Add type annotation for source_options class attribute - Add menu step translations to en.json - Add tests for menu display, new instance creation, and duplication * Simplify source_options access with class-level default --------- Co-authored-by: Bas Nijholt --- .../adaptive_lighting/config_flow.py | 37 ++++++- .../adaptive_lighting/strings.json | 7 ++ .../adaptive_lighting/translations/en.json | 7 ++ tests/test_config_flow.py | 102 ++++++++++++++++++ 4 files changed, 152 insertions(+), 1 deletion(-) diff --git a/custom_components/adaptive_lighting/config_flow.py b/custom_components/adaptive_lighting/config_flow.py index d1799888..12f1f65e 100644 --- a/custom_components/adaptive_lighting/config_flow.py +++ b/custom_components/adaptive_lighting/config_flow.py @@ -27,14 +27,49 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 + source_options: dict[str, Any] | None = None + async def async_step_user(self, user_input: dict[str, Any] | None = None): """Handle the initial step.""" + if user_input is None and self._async_current_entries(): + return await self.async_step_menu() + return await self.async_step_wait_for_name(user_input) + + async def async_step_menu(self, user_input: dict[str, Any] | None = None): + """Handle the menu step.""" + if user_input is not None: + if user_input["action"] != "new": + entry_id = user_input["action"] + entry = self.hass.config_entries.async_get_entry(entry_id) + if entry: + self.source_options = dict(entry.options) + return await self.async_step_wait_for_name() + + entries = self._async_current_entries() + options = {"new": "Create new instance"} + for entry in entries: + options[entry.entry_id] = f"Duplicate '{entry.title}'" + + return self.async_show_form( + step_id="menu", + data_schema=vol.Schema( + {vol.Required("action", default="new"): vol.In(options)}, + ), + ) + + async def async_step_wait_for_name(self, user_input: dict[str, Any] | None = None): + """Handle the name step.""" errors: dict[str, str] = {} if user_input is not None: await self.async_set_unique_id(user_input[CONF_NAME]) self._abort_if_unique_id_configured() - return self.async_create_entry(title=user_input[CONF_NAME], data=user_input) + options = self.source_options + return self.async_create_entry( + title=user_input[CONF_NAME], + data=user_input, + options=options, + ) return self.async_show_form( step_id="user", diff --git a/custom_components/adaptive_lighting/strings.json b/custom_components/adaptive_lighting/strings.json index a2724f6b..1a9ba06c 100644 --- a/custom_components/adaptive_lighting/strings.json +++ b/custom_components/adaptive_lighting/strings.json @@ -7,6 +7,13 @@ "data": { "name": "Name" } + }, + "menu": { + "title": "Create or Duplicate", + "description": "Do you want to create a new instance or duplicate an existing one?", + "data": { + "action": "Action" + } } }, "abort": { diff --git a/custom_components/adaptive_lighting/translations/en.json b/custom_components/adaptive_lighting/translations/en.json index 9991d232..fcb2ed8b 100644 --- a/custom_components/adaptive_lighting/translations/en.json +++ b/custom_components/adaptive_lighting/translations/en.json @@ -8,6 +8,13 @@ "data": { "name": "Name" } + }, + "menu": { + "title": "Create or Duplicate", + "description": "Do you want to create a new instance or duplicate an existing one?", + "data": { + "action": "Action" + } } }, "abort": { diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index 02ed6d42..8a09265e 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -145,3 +145,105 @@ async def test_options_flow_for_yaml_import(hass): assert result["type"] == FlowResultType.FORM assert result["step_id"] == "init" assert result.get("data_schema") is None + + +async def test_menu_shown_when_entries_exist(hass): + """Test that menu step is shown when existing entries exist.""" + # Create an existing entry + entry = MockConfigEntry( + domain=DOMAIN, + title="existing", + data={CONF_NAME: "existing"}, + options={"min_brightness": 10}, + ) + entry.add_to_hass(hass) + + # Start a new config flow - should show menu + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": "user"}, + ) + + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "menu" + + +async def test_menu_create_new_instance(hass): + """Test creating a new instance through the menu.""" + # Create an existing entry + entry = MockConfigEntry( + domain=DOMAIN, + title="existing", + data={CONF_NAME: "existing"}, + options={"min_brightness": 10}, + ) + entry.add_to_hass(hass) + + # Start config flow - shows menu + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": "user"}, + ) + assert result["step_id"] == "menu" + + # Choose to create new instance + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={"action": "new"}, + ) + + # Should show name form + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "user" + + # Enter name + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_NAME: "new instance"}, + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == "new instance" + # New instance should have no options (not duplicated) + assert result["options"] == {} + + +async def test_menu_duplicate_instance(hass): + """Test duplicating an existing instance through the menu.""" + # Create an existing entry with custom options + source_options = {"min_brightness": 20, "max_brightness": 80} + entry = MockConfigEntry( + domain=DOMAIN, + title="source", + data={CONF_NAME: "source"}, + options=source_options, + ) + entry.add_to_hass(hass) + + # Start config flow - shows menu + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": "user"}, + ) + assert result["step_id"] == "menu" + + # Choose to duplicate existing entry + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={"action": entry.entry_id}, + ) + + # Should show name form + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "user" + + # Enter name for duplicated instance + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_NAME: "duplicated"}, + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == "duplicated" + # Duplicated instance should have copied options + assert result["options"] == source_options