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 <bas@nijho.lt>
This commit is contained in:
edgimar 2025-12-05 07:15:24 -05:00 committed by GitHub
commit 4aeb50bfe3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 152 additions and 1 deletions

View file

@ -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",

View file

@ -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": {

View file

@ -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": {

View file

@ -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