Fix add_suggested_values_to_schema when the schema has sections (#149718)

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
karwosts 2025-07-31 11:50:26 -07:00 committed by GitHub
parent bbc1466cfc
commit aa6b37bc7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 12 deletions

View File

@ -676,9 +676,10 @@ class FlowHandler(Generic[_FlowContextT, _FlowResultT, _HandlerT]):
and key in suggested_values
):
new_section_key = copy.copy(key)
schema[new_section_key] = val
val.schema = self.add_suggested_values_to_schema(
val.schema, suggested_values[key]
new_val = copy.copy(val)
schema[new_section_key] = new_val
new_val.schema = self.add_suggested_values_to_schema(
new_val.schema, suggested_values[key]
)
continue

View File

@ -135,6 +135,19 @@ async def test_show_form(manager: MockFlowManager) -> None:
async def test_form_shows_with_added_suggested_values(manager: MockFlowManager) -> None:
"""Test that we can show a form with suggested values."""
def compare_schemas(schema: vol.Schema, expected_schema: vol.Schema) -> None:
"""Compare two schemas."""
assert schema.schema is not expected_schema.schema
assert list(schema.schema) == list(expected_schema.schema)
for key, validator in schema.schema.items():
if isinstance(validator, data_entry_flow.section):
assert validator.schema == expected_schema.schema[key].schema
continue
assert validator == expected_schema.schema[key]
schema = vol.Schema(
{
vol.Required("username"): str,
@ -172,7 +185,8 @@ async def test_form_shows_with_added_suggested_values(manager: MockFlowManager)
)
assert form["type"] == data_entry_flow.FlowResultType.FORM
assert form["data_schema"].schema is not schema.schema
assert form["data_schema"].schema == schema.schema
assert form["data_schema"].schema != schema.schema
compare_schemas(form["data_schema"], schema)
markers = list(form["data_schema"].schema)
assert len(markers) == 3
assert markers[0] == "username"
@ -182,10 +196,11 @@ async def test_form_shows_with_added_suggested_values(manager: MockFlowManager)
assert markers[2] == "section_1"
section_validator = form["data_schema"].schema["section_1"]
assert isinstance(section_validator, data_entry_flow.section)
# The section class was not replaced
assert section_validator is schema.schema["section_1"]
# The section schema was not replaced
assert section_validator.schema is schema.schema["section_1"].schema
# The section instance was copied
assert section_validator is not schema.schema["section_1"]
# The section schema instance was copied
assert section_validator.schema is not schema.schema["section_1"].schema
assert section_validator.schema == schema.schema["section_1"].schema
section_markers = list(section_validator.schema.schema)
assert len(section_markers) == 1
assert section_markers[0] == "full_name"
@ -207,15 +222,14 @@ async def test_form_shows_with_added_suggested_values(manager: MockFlowManager)
assert markers[2] == "section_1"
section_validator = form["data_schema"].schema["section_1"]
assert isinstance(section_validator, data_entry_flow.section)
# The section class was not replaced
# The section class is not replaced if there is no suggested value for the section
assert section_validator is schema.schema["section_1"]
# The section schema was not replaced
# The section schema is not replaced if there is no suggested value for the section
assert section_validator.schema is schema.schema["section_1"].schema
section_markers = list(section_validator.schema.schema)
assert len(section_markers) == 1
assert section_markers[0] == "full_name"
# This is a known bug, which needs to be fixed
assert section_markers[0].description == {"suggested_value": "John Doe"}
assert section_markers[0].description is None
async def test_abort_removes_instance(manager: MockFlowManager) -> None: