Ensure suggested values are added to section schema in data entry fow (#141227)

This commit is contained in:
Jan Bouwhuis 2025-03-23 22:14:06 +01:00 committed by GitHub
parent 842356877e
commit 93010ab5c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 22 deletions

View File

@ -17,6 +17,7 @@ from homeassistant.config_entries import (
SubentryFlowResult,
)
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from . import DOMAIN
@ -80,9 +81,7 @@ class OptionsFlowHandler(OptionsFlow):
if user_input is not None:
return self.async_create_entry(data=self.config_entry.options | user_input)
return self.async_show_form(
step_id="options_1",
data_schema=vol.Schema(
data_schema = vol.Schema(
{
vol.Required("section_1"): data_entry_flow.section(
vol.Schema(
@ -93,17 +92,19 @@ class OptionsFlowHandler(OptionsFlow):
CONF_BOOLEAN, False
),
): bool,
vol.Optional(
CONF_INT,
default=self.config_entry.options.get(CONF_INT, 10),
): int,
vol.Optional(CONF_INT): cv.positive_int,
}
),
{"collapsed": False},
),
}
),
)
self.add_suggested_values_to_schema(
data_schema,
{"section_1": {"int": self.config_entry.options.get(CONF_INT, 10)}},
)
return self.async_show_form(step_id="options_1", data_schema=data_schema)
class SubentryFlowHandler(ConfigSubentryFlow):

View File

@ -657,6 +657,19 @@ class FlowHandler(Generic[_FlowContextT, _FlowResultT, _HandlerT]):
):
continue
# Process the section schema options
if (
suggested_values is not None
and isinstance(val, section)
and key in suggested_values
):
new_section_key = copy.copy(key)
schema[new_section_key] = val
val.schema = self.add_suggested_values_to_schema(
copy.deepcopy(val.schema), suggested_values[key]
)
continue
new_key = key
if (
suggested_values

View File

@ -96,6 +96,15 @@ async def test_options_flow(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "options_1"
section_marker, section_schema = list(result["data_schema"].schema.items())[0]
assert section_marker == "section_1"
section_schema_markers = list(section_schema.schema.schema)
assert len(section_schema_markers) == 2
assert section_schema_markers[0] == "bool"
assert section_schema_markers[0].description is None
assert section_schema_markers[1] == "int"
assert section_schema_markers[1].description == {"suggested_value": 10}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"section_1": {"bool": True, "int": 15}},