From 885749cd91f4fa29b4ffd2fa40ebd501da1d605b Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sun, 24 Nov 2024 19:06:03 +0000 Subject: [PATCH] Change multiple and default to inclusive with no defaults --- homeassistant/data_entry_flow.py | 4 +- homeassistant/helpers/config_validation.py | 4 +- tests/test_data_entry_flow.py | 60 ++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/homeassistant/data_entry_flow.py b/homeassistant/data_entry_flow.py index 25183dd6741..6d41e7a68c0 100644 --- a/homeassistant/data_entry_flow.py +++ b/homeassistant/data_entry_flow.py @@ -918,8 +918,8 @@ class section: CONFIG_SCHEMA = vol.Schema( { vol.Optional("collapsed", default=False): bool, - vol.Optional("multiple", default=False): bool, - vol.Optional("default", default=[]): list[Any], + vol.Inclusive("multiple", "multiple"): bool, + vol.Inclusive("default", "multiple"): list[Any], }, ) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 365024a537e..a7e109df55c 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1145,8 +1145,8 @@ def _custom_serializer(schema: Any, *, allow_section: bool) -> Any: ), ), "expanded": not schema.options["collapsed"], - "multiple": schema.options["multiple"], - "default": schema.options["default"], + "multiple": schema.options.get("multiple"), + "default": schema.options.get("default"), } if isinstance(schema, multi_select): diff --git a/tests/test_data_entry_flow.py b/tests/test_data_entry_flow.py index 32020ac0d76..3522d7236f5 100644 --- a/tests/test_data_entry_flow.py +++ b/tests/test_data_entry_flow.py @@ -1020,9 +1020,69 @@ def test_section_in_serializer() -> None: {"name": "option_2", "required": True, "type": "integer"}, ], "type": "expandable", + "multiple": None, + "default": None, } +def test_section_multiple_in_serializer() -> None: + """Test section with multiple with custom_serializer.""" + assert cv.custom_serializer( + data_entry_flow.section( + vol.Schema( + { + vol.Optional("option_1", default=False): bool, + vol.Required("option_2"): int, + } + ), + {"collapsed": False, "multiple": True, "default": [{True, 10}]}, + ) + ) == { + "expanded": True, + "schema": [ + {"default": False, "name": "option_1", "optional": True, "type": "boolean"}, + {"name": "option_2", "required": True, "type": "integer"}, + ], + "type": "expandable", + "multiple": True, + "default": [ + { + True, + 10, + }, + ], + } + + +def test_section_multiple_and_default_inclusive_in_serializer() -> None: + """Test section with multiple missing default in custom_serializer.""" + with pytest.raises(vol.MultipleInvalid): + cv.custom_serializer( + data_entry_flow.section( + vol.Schema( + { + vol.Optional("option_1", default=False): bool, + vol.Required("option_2"): int, + } + ), + {"collapsed": False, "multiple": None}, + ) + ) + + with pytest.raises(vol.MultipleInvalid): + cv.custom_serializer( + data_entry_flow.section( + vol.Schema( + { + vol.Optional("option_1", default=False): bool, + vol.Required("option_2"): int, + } + ), + {"collapsed": False, "default": []}, + ) + ) + + def test_nested_section_in_serializer() -> None: """Test section with custom_serializer.""" with pytest.raises(