Allow multiple sections

This commit is contained in:
G Johansson 2024-11-23 18:22:24 +00:00
parent 84630ef8cc
commit ff51d924b4
4 changed files with 42 additions and 2 deletions

View File

@ -89,7 +89,29 @@ class OptionsFlowHandler(OptionsFlow):
): int,
}
),
{"collapsed": False},
{
"collapsed": False,
"multiple": True,
},
),
vol.Required("section_2"): data_entry_flow.section(
vol.Schema(
{
vol.Optional(
"a",
default=2,
): int,
vol.Optional(
"b",
default=4,
): int,
}
),
{
"collapsed": False,
"multiple": True,
"default": [{"a": 7, "b": 10}],
},
),
}
),

View File

@ -23,6 +23,14 @@
},
"description": "This section allows input of some extra data",
"name": "Collapsible section"
},
"section_2": {
"data": {
"a": "A",
"b": "B"
},
"description": "Some datapoints",
"name": "Data point"
}
},
"submit": "Save!"

View File

@ -908,6 +908,8 @@ class SectionConfig(TypedDict, total=False):
"""Class to represent a section config."""
collapsed: bool
multiple: bool
default: list[Any]
class section:
@ -916,6 +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],
},
)
@ -928,7 +932,11 @@ class section:
def __call__(self, value: Any) -> Any:
"""Validate input."""
return self.schema(value)
if not self.options["multiple"]:
return self.schema(value)
if not isinstance(value, list):
raise vol.Invalid("Value should be a list")
return [vol.Schema(dict)(val) for val in value]
# These can be removed if no deprecated constant are in this module anymore

View File

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