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

Conversation

karwosts
Copy link
Contributor

@karwosts karwosts commented Oct 29, 2024

Proposed change

A common complaint for a UI configured entity is that after the initial config flow, the inputs to that config flow are no longer visible, and you can't review how you configured it in the first place.

As a solution to this I propose supporting readonly/disabled selectors in options flows, which can show the values chosen during config flow, but freeze them so that they cannot be changed.

I've configured history_stats in this PR as a proof of concept of this flow.

A small frontend change is also required to support the way the disabled flag is passed to the form.

image

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

Copy link
Member

@gjohansson-ST gjohansson-ST left a comment

Choose a reason for hiding this comment

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

While I didn't receive any response on my proposal for a read only selector (which is guess should close as this is likely a better suggestion anyhow), that and this proposal share the same issue eg. They should be only for display and not sent back to core (the helper should not need to consider fields which are there only for display purpose I think).

@@ -44,6 +44,9 @@ async def test_form(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_ENTITY_ID: "binary_sensor.test_monitored",
Copy link
Member

Choose a reason for hiding this comment

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

Read only fields should not be set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I was kind of disappointed I had to do this to pass the testing, was initially hoping it would be less invasive.

If I mark the disabled fields as vol.Required, I have to pass them into this function.

If I mark the disabled fields as vol.Optional, (and don't pass them in here), they don't get passed through the config+options flow and the assert fails at the end.

Will have to think about this some more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok trying a bit different approach.

  • Make readonly fields vol.Optional instead of vol.Required.
  • Change frontend to delete the readonly fields before sending data back to backend.
  • Change backend to not delete optional/readonly fields when they are not provided by the step flow (which they never will be provided).

With this can revert all the changes to the test.

@MartinHjelmare MartinHjelmare changed the title Support disabled selectors in config_flows. Add config visibility to history_stats. Support disabled selectors in config_flows Oct 29, 2024
@MindFreeze
Copy link
Contributor

While I didn't receive any response on my proposal for a read only selector (which is guess should close as this is likely a better suggestion anyhow), that and this proposal share the same issue eg. They should be only for display and not sent back to core (the helper should not need to consider fields which are there only for display purpose I think).

Yeah, readonly sounds better. disabled is a consequence of the fact that the field is readonly and is usually something you toggle. Read only makes it clear that we don't need to process this data on save

@karwosts karwosts changed the title Support disabled selectors in config_flows Support readonly selectors in config_flows Oct 30, 2024
@epenet
Copy link
Contributor

epenet commented Oct 30, 2024

While I didn't receive any response on my proposal for a read only selector (which is guess should close as this is likely a better suggestion anyhow), that and this proposal share the same issue eg. They should be only for display and not sent back to core (the helper should not need to consider fields which are there only for display purpose I think).

Yeah, readonly sounds better. disabled is a consequence of the fact that the field is readonly and is usually something you toggle. Read only makes it clear that we don't need to process this data on save

Small nit: I would rename readonly to displayonly as with readonly I would expect to have them to make the round trip and be sent back to core.

@karwosts
Copy link
Contributor Author

Small nit: I would rename readonly to displayonly as with readonly I would expect to have them to make the round trip and be sent back to core.

I'll let a little more time to accumulate for further opinions, then I will change this if no other objections.

@tetele
Copy link
Contributor

tetele commented Oct 30, 2024

Small nit: I would rename readonly to displayonly as with readonly I would expect to have them to make the round trip and be sent back to core.

I've never heard the term "display only", whereas I know exactly what "read only" means. That said, this option is only in the code so maybe from a core perspective it makes more sense.

@MindFreeze
Copy link
Contributor

While I didn't receive any response on my proposal for a read only selector (which is guess should close as this is likely a better suggestion anyhow), that and this proposal share the same issue eg. They should be only for display and not sent back to core (the helper should not need to consider fields which are there only for display purpose I think).

Yeah, readonly sounds better. disabled is a consequence of the fact that the field is readonly and is usually something you toggle. Read only makes it clear that we don't need to process this data on save

Small nit: I would rename readonly to displayonly as with readonly I would expect to have them to make the round trip and be sent back to core.

I don't agree as readonly seems clear enough to me but displayonly is also fine. I just wanted it to be differentiated from the existing disabled option on the frontend.

@@ -66,6 +66,17 @@ async def validate_options(
)
DATA_SCHEMA_OPTIONS = vol.Schema(
{
vol.Optional(CONF_ENTITY_ID, description={"readonly": True}): EntitySelector(),
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed to read_only

@karwosts karwosts force-pushed the config-flow-disabled-selectors branch from b501945 to a1ce4a5 Compare November 23, 2024 13:53
@joostlek
Copy link
Member

So we discussed this PR in the core team and we had the following remarks:

  • Concept is good
  • Developer documentation should make it clear with examples when a read only selector is useful
  • The PR doesn't do what the title suggests, the PR adds a flag to voluptuous. Ideally we add the read_only flag to the SelectorConfig
  • Either frontend should not be allowed to send data for a read_only selector, or the data must match. In either case, this must be checked. We think frontend should decide which option is better. Ideally we align with what the const selector does.

@MindFreeze
Copy link
Contributor

Frontend shouldn't be allowed to send read only or core can just discard it. Checking if it matches is not worth it.

@karwosts karwosts marked this pull request as draft January 16, 2025 15:53
@karwosts karwosts marked this pull request as ready for review January 18, 2025 17:24
@karwosts
Copy link
Contributor Author

The PR doesn't do what the title suggests, the PR adds a flag to voluptuous. Ideally we add the read_only flag to the SelectorConfig

I've changed such that read_only is a flag to the selector config. Hope I have understood what you were asking for.

        vol.Optional(CONF_STATE): TextSelector(
            TextSelectorConfig(multiple=True, read_only=True)
        ),

Either frontend should not be allowed to send data for a read_only selector, or the data must match. In either case, this must be checked. We think frontend should decide which option is better. Ideally we align with what the const selector does.

Current behavior of frontend is that it does not send data for read_only selector.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants