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

Support readonly selectors in config_flows #129456

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions homeassistant/components/history_stats/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DurationSelector,
DurationSelectorConfig,
EntitySelector,
EntitySelectorConfig,
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
Expand Down Expand Up @@ -66,6 +67,20 @@ async def validate_options(
)
DATA_SCHEMA_OPTIONS = vol.Schema(
{
vol.Optional(CONF_ENTITY_ID): EntitySelector(
EntitySelectorConfig(read_only=True)
),
vol.Optional(CONF_STATE): TextSelector(
TextSelectorConfig(multiple=True, read_only=True)
),
vol.Optional(CONF_TYPE): SelectSelector(
SelectSelectorConfig(
options=CONF_TYPE_KEYS,
mode=SelectSelectorMode.DROPDOWN,
translation_key=CONF_TYPE,
read_only=True,
)
),
vol.Optional(CONF_START): TemplateSelector(),
vol.Optional(CONF_END): TemplateSelector(),
vol.Optional(CONF_DURATION): DurationSelector(
Expand Down
12 changes: 12 additions & 0 deletions homeassistant/components/history_stats/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@
"options": {
"description": "Read the documention for further details on how to configure the history stats sensor using these options.",
"data": {
"entity_id": "[%key:component::history_stats::config::step::user::data::entity_id%]",
"state": "[%key:component::history_stats::config::step::user::data::state%]",
"type": "[%key:component::history_stats::config::step::user::data::type%]",
"start": "Start",
"end": "End",
"duration": "Duration"
},
"data_description": {
"entity_id": "[%key:component::history_stats::config::step::user::data_description::entity_id%]",
"state": "[%key:component::history_stats::config::step::user::data_description::state%]",
"type": "[%key:component::history_stats::config::step::user::data_description::type%]",
"start": "When to start the measure (timestamp or datetime). Can be a template.",
"end": "When to stop the measure (timestamp or datetime). Can be a template",
"duration": "Duration of the measure."
Expand All @@ -49,11 +55,17 @@
"init": {
"description": "[%key:component::history_stats::config::step::options::description%]",
"data": {
"entity_id": "[%key:component::history_stats::config::step::user::data::entity_id%]",
"state": "[%key:component::history_stats::config::step::user::data::state%]",
"type": "[%key:component::history_stats::config::step::user::data::type%]",
"start": "[%key:component::history_stats::config::step::options::data::start%]",
"end": "[%key:component::history_stats::config::step::options::data::end%]",
"duration": "[%key:component::history_stats::config::step::options::data::duration%]"
},
"data_description": {
"entity_id": "[%key:component::history_stats::config::step::user::data_description::entity_id%]",
"state": "[%key:component::history_stats::config::step::user::data_description::state%]",
"type": "[%key:component::history_stats::config::step::user::data_description::type%]",
"start": "[%key:component::history_stats::config::step::options::data_description::start%]",
"end": "[%key:component::history_stats::config::step::options::data_description::end%]",
"duration": "[%key:component::history_stats::config::step::options::data_description::duration%]"
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/helpers/schema_config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ def _update_and_remove_omitted_optional_keys(
and key.description.get("advanced")
and not self._handler.show_advanced_options
)
and not (
# don't remove read_only keys
isinstance(data_schema.schema[key], selector.Selector)
and data_schema.schema[key].config.get("read_only")
)
):
# Key not present, delete keys old value (if present) too
values.pop(key.schema, None)
Expand Down
25 changes: 19 additions & 6 deletions homeassistant/helpers/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,20 @@ def _validate_supported_features(supported_features: int | list[str]) -> int:
return feature_mask


ENTITY_FILTER_SELECTOR_CONFIG_SCHEMA = vol.Schema(
BASE_SELECTOR_CONFIG_SCHEMA = vol.Schema(
{
vol.Optional("read_only"): bool,
}
)


class BaseSelectorConfig(TypedDict, total=False):
"""Class to common options of all selectors."""

read_only: bool


ENTITY_FILTER_SELECTOR_CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA.extend(
{
# Integration that provided the entity
vol.Optional("integration"): str,
Expand All @@ -147,7 +160,7 @@ def _validate_supported_features(supported_features: int | list[str]) -> int:
)


class EntityFilterSelectorConfig(TypedDict, total=False):
class EntityFilterSelectorConfig(BaseSelectorConfig, total=False):
"""Class to represent a single entity selector config."""

integration: str
Expand Down Expand Up @@ -1139,7 +1152,7 @@ class SelectSelectorMode(StrEnum):
DROPDOWN = "dropdown"


class SelectSelectorConfig(TypedDict, total=False):
class SelectSelectorConfig(BaseSelectorConfig, total=False):
"""Class to represent a select selector config."""

options: Required[Sequence[SelectOptionDict] | Sequence[str]]
Expand All @@ -1156,7 +1169,7 @@ class SelectSelector(Selector[SelectSelectorConfig]):

selector_type = "select"

CONFIG_SCHEMA = vol.Schema(
CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA.extend(
{
vol.Required("options"): vol.All(vol.Any([str], [select_option])),
vol.Optional("multiple", default=False): cv.boolean,
Expand Down Expand Up @@ -1292,7 +1305,7 @@ def __call__(self, data: Any) -> str:
return template.template


class TextSelectorConfig(TypedDict, total=False):
class TextSelectorConfig(BaseSelectorConfig, total=False):
"""Class to represent a text selector config."""

multiline: bool
Expand Down Expand Up @@ -1327,7 +1340,7 @@ class TextSelector(Selector[TextSelectorConfig]):

selector_type = "text"

CONFIG_SCHEMA = vol.Schema(
CONFIG_SCHEMA = BASE_SELECTOR_CONFIG_SCHEMA.extend(
{
vol.Optional("multiline", default=False): bool,
vol.Optional("prefix"): str,
Expand Down