Skip to content
Draft
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
74 changes: 70 additions & 4 deletions docs/config_entries_config_flow_handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,41 @@ class LocationSubentryFlowHandler(ConfigSubentryFlow):
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> SubentryFlowResult:
"""User flow to add a new location."""
...
"""User flow to add a new location.

Function name must be in the format "async_step_{step_id}"
The first step is always "user"
"""
Comment on lines +437 to +441
Copy link
Member

Choose a reason for hiding this comment

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

Let's not use docstrings for this


errors: dict[str, str] = {}

# The function is called once to start the step and then again
# each time the user submits an input. This handles the latter
if user_input is not None:
try:
user_input = await _validate_subentry(user_input)
return self.async_create_entry(
title=user_input.get(CONF_NAME),
data=user_input,
)
except (SchemaFlowError) as err:
# errors can be attached the base of a form or to a specific field
errors["base"] = str(err)

# This runs when the step starts or after a failed validation
return self.async_show_form(
step_id=str(ObservationTypes.STATE),
data_schema=self.add_suggested_values_to_schema(
data_schema=LOCATION_SUBSCHEMA, suggested_values=user_input
),
last_step=True,
errors=errors,
description_placeholders={
# the parent config entry can be accessed with self._get_entry()
"parent_entry_title": self._get_entry().title,
},
)

```

### Subentry unique ID
Expand All @@ -454,7 +487,7 @@ Subentries can set a unique ID. The rules are similar to [unique IDs](#unique-id
"step": {
"user": {
"title": "Add location",
"description": "Configure the weather location"
"description": "Configure a weather location for {parent_entry_title}."
},
"reconfigure": {
"title": "Update location",
Expand Down Expand Up @@ -488,11 +521,44 @@ class LocationSubentryFlowHandler(ConfigSubentryFlow):
self, user_input: dict[str, Any] | None = None
) -> SubentryFlowResult:
"""User flow to modify an existing location."""

errors: dict[str, str] = {}
# Retrieve the parent config entry for reference.
config_entry = self._get_reconfigure_entry()
# Retrieve the specific subentry targeted for update.
config_subentry = self._get_reconfigure_subentry()
...

if user_input is not None:
# validate user_input, possibly with some checks on ther subentries
# If checking for duplicates remeber to remove the entry you are reconfiguring
other_subentries = [
dict(se.data) for se in config_entry.subentries.values()
]
other_subentries.remove(dict(config_subentry.data))
Comment on lines +532 to +537
Copy link
Member

Choose a reason for hiding this comment

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

I would assume that this is something we should take into account with helpers

There are some typos in the comment

try:
... #validation
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
... #validation
# Validate the data

return self.async_update_and_abort(
config_entry,
config_subentry,
title=user_input.get(CONF_NAME, config_subentry.data[CONF_NAME]),
data_updates=user_input,
)
Comment on lines +540 to +545
Copy link
Member

Choose a reason for hiding this comment

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

Keep this out of the try

except (SchemaFlowError) as err:
# errors can be attached the base of a form or to a specific field
errors["base"] = str(err)

return self.async_show_form(
step_id="reconfigure",
# You will likely want to fetch original values
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# You will likely want to fetch original values
# You generally want to pre-fill the original values

data_schema=self.add_suggested_values_to_schema(
data_schema=SUBENTRY_SCHEMA,
suggested_values=config_subentry.data,
Comment on lines +554 to +555
Copy link
Member

Choose a reason for hiding this comment

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

I think we hardly use kwargs for these

),
errors=errors,
description_placeholders={
"parent_entry_title": self._get_entry().title,
},
)

```

Expand Down