Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow multiple sections #131393

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions homeassistant/components/kitchen_sink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,38 @@ async def async_step_options_1(
{
vol.Optional(
CONF_BOOLEAN,
default=self.config_entry.options.get(
CONF_BOOLEAN, False
),
default=False,
): bool,
vol.Optional(
CONF_INT,
default=self.config_entry.options.get(CONF_INT, 10),
default=10,
): int,
}
),
{"collapsed": False},
{
"collapsed": False,
"multiple": True,
"default": self.config_entry.options.get("section_1"),
},
),
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": self.config_entry.options.get("section_2"),
},
),
}
),
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 @@ -910,6 +910,8 @@ class SectionConfig(TypedDict, total=False):
"""Class to represent a section config."""

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


class section:
Expand All @@ -918,6 +920,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 @@ -930,7 +934,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
Loading