-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
HarvsG
wants to merge
7
commits into
home-assistant:master
Choose a base branch
from
HarvsG:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4b327be
Improve subentry documentation.
HarvsG ac7c02e
Update config_entries_config_flow_handler.md
HarvsG 36c2e43
Update config_entries_config_flow_handler.md
HarvsG c9d268a
Update config_entries_config_flow_handler.md
HarvsG ea77a1d
Update config_entries_config_flow_handler.md
HarvsG b4b935b
Don't log errors being shown to the user
HarvsG 14795c6
Update docs/config_entries_config_flow_handler.md
HarvsG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -434,8 +434,42 @@ 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. | ||
|
||
Fnction 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( | ||
title=user_input.get(CONF_NAME), | ||
data=user_input, | ||
) | ||
except (SchemaFlowError) as err: | ||
_LOGGER.error("Error validating subentry: %s", 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 | ||
|
@@ -492,7 +526,39 @@ class LocationSubentryFlowHandler(ConfigSubentryFlow): | |
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 self._get_entry().subentries.values() | ||
] | ||
other_subentries.remove(dict(config_subentry.data)) | ||
try: | ||
... #validation | ||
return self.async_update_and_abort( | ||
self._get_entry(), | ||
config_subentry, | ||
HarvsG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
title=user_input.get(CONF_NAME, config_subentry.data[CONF_NAME]), | ||
data_updates=user_input, | ||
) | ||
except (SchemaFlowError) as err: | ||
_LOGGER.error("Error reconfiguring subentry: %s", 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, | ||
}, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initialize The snippet references @@ async_step_reconfigure(self, user_input: dict[str, Any] | None = None) -> SubentryFlowResult:
- config_subentry = self._get_reconfigure_subentry()
+ config_subentry = self._get_reconfigure_subentry()
+ errors: dict[str, str] = {}
if user_input is not None:
# validate user_input... Optionally, at the top of the example add: from homeassistant.data_entry_flow import SchemaFlowError 🤖 Prompt for AI Agents
|
||
|
||
``` | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Qualify call to
_validate_subentry
The call to
_validate_subentry
lacks a qualifier and will raise a NameError. Either prefix it withself.
(await self._validate_subentry(user_input)
) if it’s an instance method, or explicitly import it if it’s standalone.🤖 Prompt for AI Agents