Skip to content

Help confusion with subentries #2708

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

Open
wants to merge 7 commits 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
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"
"""

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(
Comment on lines +449 to +450
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Qualify call to _validate_subentry
The call to _validate_subentry lacks a qualifier and will raise a NameError. Either prefix it with self. (await self._validate_subentry(user_input)) if it’s an instance method, or explicitly import it if it’s standalone.

🤖 Prompt for AI Agents
In docs/config_entries_config_flow_handler.md around lines 449 to 450, the call
to _validate_subentry is unqualified and will cause a NameError. Fix this by
prefixing the call with self. to make it await
self._validate_subentry(user_input) if _validate_subentry is an instance method,
or import it explicitly if it is a standalone function.

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))
try:
... #validation
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,
)
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
data_schema=self.add_suggested_values_to_schema(
data_schema=SUBENTRY_SCHEMA,
suggested_values=config_subentry.data,
),
errors=errors,
description_placeholders={
"parent_entry_title": self._get_entry().title,
},
)

```

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy