Change multiple and default to inclusive with no defaults

This commit is contained in:
G Johansson 2024-11-24 19:06:03 +00:00
parent c7741a0885
commit 885749cd91
3 changed files with 64 additions and 4 deletions

View File

@ -918,8 +918,8 @@ class section:
CONFIG_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema(
{ {
vol.Optional("collapsed", default=False): bool, vol.Optional("collapsed", default=False): bool,
vol.Optional("multiple", default=False): bool, vol.Inclusive("multiple", "multiple"): bool,
vol.Optional("default", default=[]): list[Any], vol.Inclusive("default", "multiple"): list[Any],
}, },
) )

View File

@ -1145,8 +1145,8 @@ def _custom_serializer(schema: Any, *, allow_section: bool) -> Any:
), ),
), ),
"expanded": not schema.options["collapsed"], "expanded": not schema.options["collapsed"],
"multiple": schema.options["multiple"], "multiple": schema.options.get("multiple"),
"default": schema.options["default"], "default": schema.options.get("default"),
} }
if isinstance(schema, multi_select): if isinstance(schema, multi_select):

View File

@ -1020,9 +1020,69 @@ def test_section_in_serializer() -> None:
{"name": "option_2", "required": True, "type": "integer"}, {"name": "option_2", "required": True, "type": "integer"},
], ],
"type": "expandable", "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: def test_nested_section_in_serializer() -> None:
"""Test section with custom_serializer.""" """Test section with custom_serializer."""
with pytest.raises( with pytest.raises(