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

Allow multiple in sections #2471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
69 changes: 69 additions & 0 deletions docs/data_entry_flow_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,75 @@ To specify an icon for a section, update `icons.json` according to this example:
}
```

#### Use of repeating sections

Developers can also set `multiple` in sections to allow the user to add/remove sections as needed to provide additional configuration.
The output of the sections are then a list with dictionaries containing all the users configured sections.

The use of `multiple` in sections also requires to set `default`.
The use of `default` in sections has priority over defaults for the fields.
Comment on lines +197 to +198
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix grammar in requirements statement

-The use of `multiple` in sections also requires to set `default`.
+The use of `multiple` in sections also requires setting `default`.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
The use of `multiple` in sections also requires to set `default`.
The use of `default` in sections has priority over defaults for the fields.
The use of `multiple` in sections also requires setting `default`.
The use of `default` in sections has priority over defaults for the fields.
🧰 Tools
🪛 LanguageTool

[grammar] ~197-~197: Did you mean “setting”? Or maybe you should add a pronoun? In active voice, ‘require’ + ‘to’ takes an object, usually a pronoun.
Context: ...of multiple in sections also requires to set default. The use of default in sect...

(ALLOW_TO)


| Key | Type | Description |
| :----------------: | :----------------: | :-----------------------------------------: |
| `multiple` | `bool` | Allow user to add/remove sections |
| `default` | `list[dict]` | Provide default values for the section |

```python
from homeassistant.data_entry_flow import section
from homeassistant.helpers.selector import selector

class ExampleConfigFlow(data_entry_flow.FlowHandler):
async def async_step_user(self, user_input=None):
# Specify items in the order they are to be displayed in the UI
data_schema = {
vol.Required("username"): str,
vol.Required("password"): str,
# Items can be grouped by collapsible sections
"options": section(
vol.Schema(
{
vol.Required("stage_1", default=5): int,
vol.Required("stage_2", default=10): int,
}
),
# Whether or not the section is initially collapsed (default = False),
# user can provide multiple sections (default = None) and
# default values for the section in case of multiple (default = None)
{
"collapsed": False,
"multiple": True,
"default": [],
},
)
}

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(data_schema)
)
```

The resulting output from the section is a list with dictionaries containing the values provided by the user.

##### Example output from sections using multiple

```json
{
"username": "some_username",
"password": "some_password",
"options": [
{
"stage_1":7,
"stage_2":15
},
{
"stage_1":24,
"stage_2":4
}
]
}
```

#### Labels & descriptions

Translations for the form are added to `strings.json` in a key for the `step_id`. That object may contain the folowing keys:
Expand Down