diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 2904efb75e9..3d3de40a2c6 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1090,6 +1090,11 @@ def key_dependency[_KT: Hashable, _VT]( def custom_serializer(schema: Any) -> Any: + """Serialize additional types for voluptuous_serialize.""" + return _custom_serializer(schema, allow_section=True) + + +def _custom_serializer(schema: Any, *, allow_section: bool) -> Any: """Serialize additional types for voluptuous_serialize.""" from .. import data_entry_flow # pylint: disable=import-outside-toplevel from . import selector # pylint: disable=import-outside-toplevel @@ -1104,10 +1109,15 @@ def custom_serializer(schema: Any) -> Any: return {"type": "boolean"} if isinstance(schema, data_entry_flow.section): + if not allow_section: + raise ValueError("Nesting expandable sections is not supported") return { "type": "expandable", "schema": voluptuous_serialize.convert( - schema.schema, custom_serializer=custom_serializer + schema.schema, + custom_serializer=functools.partial( + _custom_serializer, allow_section=False + ), ), "expanded": not schema.options["collapsed"], } diff --git a/tests/test_data_entry_flow.py b/tests/test_data_entry_flow.py index 967b2565206..01b6a530105 100644 --- a/tests/test_data_entry_flow.py +++ b/tests/test_data_entry_flow.py @@ -1098,3 +1098,27 @@ def test_section_in_serializer() -> None: ], "type": "expandable", } + + +def test_nested_section_in_serializer() -> None: + """Test section with custom_serializer.""" + with pytest.raises( + ValueError, match="Nesting expandable sections is not supported" + ): + cv.custom_serializer( + data_entry_flow.section( + vol.Schema( + { + vol.Required("section_1"): data_entry_flow.section( + vol.Schema( + { + vol.Optional("option_1", default=False): bool, + vol.Required("option_2"): int, + } + ) + ) + } + ), + {"collapsed": False}, + ) + )