-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
Add config flow to compensation helper #121857
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
"""Config flow for statistics.""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Mapping | ||
from typing import Any, cast | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.const import ( | ||
CONF_ATTRIBUTE, | ||
CONF_ENTITY_ID, | ||
CONF_NAME, | ||
CONF_UNIT_OF_MEASUREMENT, | ||
) | ||
from homeassistant.helpers.schema_config_entry_flow import ( | ||
SchemaCommonFlowHandler, | ||
SchemaConfigFlowHandler, | ||
SchemaFlowError, | ||
SchemaFlowFormStep, | ||
) | ||
from homeassistant.helpers.selector import ( | ||
AttributeSelector, | ||
AttributeSelectorConfig, | ||
BooleanSelector, | ||
EntitySelector, | ||
NumberSelector, | ||
NumberSelectorConfig, | ||
NumberSelectorMode, | ||
SelectSelector, | ||
SelectSelectorConfig, | ||
SelectSelectorMode, | ||
TextSelector, | ||
) | ||
|
||
from .const import ( | ||
CONF_DATAPOINTS, | ||
CONF_DEGREE, | ||
CONF_LOWER_LIMIT, | ||
CONF_PRECISION, | ||
CONF_UPPER_LIMIT, | ||
DEFAULT_DEGREE, | ||
DEFAULT_NAME, | ||
DEFAULT_PRECISION, | ||
DOMAIN, | ||
) | ||
|
||
|
||
async def get_options_schema(handler: SchemaCommonFlowHandler) -> vol.Schema: | ||
"""Get options schema.""" | ||
entity_id = handler.options[CONF_ENTITY_ID] | ||
|
||
return vol.Schema( | ||
{ | ||
vol.Required(CONF_DATAPOINTS): SelectSelector( | ||
SelectSelectorConfig( | ||
options=[], | ||
multiple=True, | ||
custom_value=True, | ||
5DA8 mode=SelectSelectorMode.DROPDOWN, | ||
) | ||
), | ||
vol.Optional(CONF_ATTRIBUTE): AttributeSelector( | ||
AttributeSelectorConfig(entity_id=entity_id) | ||
), | ||
vol.Optional(CONF_UPPER_LIMIT, default=False): BooleanSelector(), | ||
vol.Optional(CONF_LOWER_LIMIT, default=False): BooleanSelector(), | ||
vol.Optional(CONF_PRECISION, default=DEFAULT_PRECISION): NumberSelector( | ||
NumberSelectorConfig(min=0, step=1, mode=NumberSelectorMode.BOX) | ||
), | ||
vol.Optional(CONF_DEGREE, default=DEFAULT_DEGREE): NumberSelector( | ||
NumberSelectorConfig(min=0, max=7, step=1, mode=NumberSelectorMode.BOX) | ||
), | ||
vol.Optional(CONF_UNIT_OF_MEASUREMENT): TextSelector(), | ||
} | ||
) | ||
|
||
|
||
def _is_valid_data_points(check_data_points: list[str]) -> bool: | ||
"""Validate data points.""" | ||
result = False | ||
for data_point in check_data_points: | ||
if not data_point.find(",") > 0: | ||
return False | ||
values = data_point.split(",", maxsplit=1) | ||
for value in values: | ||
try: | ||
float(value) | ||
except ValueError: | ||
return False | ||
result = True | ||
return result | ||
|
||
|
||
async def validate_options( | ||
handler: SchemaCommonFlowHandler, user_input: dict[str, Any] | ||
) -> dict[str, Any]: | ||
"""Validate options selected.""" | ||
|
||
user_input[CONF_PRECISION] = int(user_input[CONF_PRECISION]) | ||
user_input[CONF_DEGREE] = int(user_input[CONF_DEGREE]) | ||
|
||
if not _is_valid_data_points(user_input[CONF_DATAPOINTS]): | ||
raise SchemaFlowError("incorrect_datapoints") | ||
|
||
if len(user_input[CONF_DATAPOINTS]) <= user_input[CONF_DEGREE]: | ||
raise SchemaFlowError("not_enough_datapoints") | ||
|
||
handler.parent_handler._async_abort_entries_match({**handler.options, **user_input}) # noqa: SLF001 | ||
|
||
return user_input | ||
|
||
|
||
DATA_SCHEMA_SETUP = vol.Schema( | ||
{ | ||
vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(), | ||
vol.Required(CONF_ENTITY_ID): EntitySelector(), | ||
} | ||
) | ||
|
||
CONFIG_FLOW = { | ||
"user": SchemaFlowFormStep( | ||
schema=DATA_SCHEMA_SETUP, | ||
next_step="options", | ||
), | ||
"options": SchemaFlowFormStep( | ||
schema=get_options_schema, | ||
validate_user_input=validate_options, | ||
), | ||
} | ||
OPTIONS_FLOW = { | ||
"init": SchemaFlowFormStep( | ||
get_options_schema, | ||
validate_user_input=validate_options, | ||
), | ||
} | ||
|
||
|
||
class CompensationConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN): | ||
"""Handle a config flow for Compensation.""" | ||
|
||
config_flow = CONFIG_FLOW | ||
options_flow = OPTIONS_FLOW | ||
|
||
def async_config_entry_title(self, options: Mapping[str, Any]) -> str: | ||
"""Return config entry title.""" | ||
return cast(str, options[CONF_NAME]) |
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
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
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
Oops, something went wrong.
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.
We shouldn't be asking for users to enter comma-separated values in a text field. This is error-prone as well as a bad end-user experience.
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.
Do you have another suggestion to get the desired functionality? There's no other selectors that offer an "endless list" of items.
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.
I think the only good solution here is to see if we can make a selector that allows for adding "groups" of selectors.
Little like sections I guess, but then for repeating groups of items.
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.
While I do get that's not optimal for users to input like I built it, I'm not thinking as it's too bad as there is quite descriptive text, easier than in yaml and also quite some validation that its input is ok.
However @frenck I'm not against building a new selector for this (as there is also other places that would benefit). Could you expand how you were thinking this would be presented as I'm struggling to see how it would be made. Thanks