Skip to content

Commit

Permalink
Allow multiple sections
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed Nov 24, 2024
1 parent 84630ef commit ff51d92
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
24 changes: 23 additions & 1 deletion homeassistant/components/kitchen_sink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,29 @@ async def async_step_options_1(
): 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}],
},
),
}
),
Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/kitchen_sink/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
10 changes: 9 additions & 1 deletion homeassistant/data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ class SectionConfig(TypedDict, total=False):
"""Class to represent a section config."""

collapsed: bool
multiple: bool
default: list[Any]


class section:
Expand All @@ -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],
},
)

Expand All @@ -928,7 +932,11 @@ def __init__(

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
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ff51d92

Please sign in to comment.