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

Fix removing suggested_display_precision from entity registry #110671

Merged
merged 3 commits into from
Apr 24, 2024

Conversation

pnbruckner
Copy link
Contributor

@pnbruckner pnbruckner commented Feb 15, 2024

Proposed change

When a sensor entity's suggested_display_precision is changed to None from something else, it is supposed to be removed from options of the corresponding entry in the entity registry. However, the code that is supposed to do this (SensorEntity._update_suggested_precision) has a bug that prevents it from doing so. This change fixes that bug.

Some background can be found here: pnbruckner/ha-sun2#109

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

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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (sensor) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of sensor can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign sensor Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@gjohansson-ST
Copy link
Member

Why is it supposed to be removed?

@pnbruckner
Copy link
Contributor Author

Well, strictly speaking, I don't think it absolutely needs to be removed in this case, but the registry does need to be updated to be consistent with the entity's suggested_display_precision.

Currently, if the entity changes its suggested_display_precision from None to something other than None, the code will add it to the registry, and if it changes between two different non-None values, it updates the registry accordingly. However, if it changes from a non-None value to None, the bug is preventing it from updating the registry accordingly. That is what this PR fixes. I.e., it fixes the test that checks if the registry needs to be updated, not what the update does.

As far as removing as opposed to just setting it to None (i.e., null in the JSON file), since this value is optional, it does make more sense to remove it rather than simply changing it. That is how the function is currently coded. This PR does not change that aspect of the code. It just allows it to run when it should.

That is my interpretation. Maybe the original author, @emontnemery, has a different take on it.

And, FWIW, although this bug has existed since it was originally added a year ago in #87614, it probably has not been noticed because 1) most sensor entities probably don't change their suggested_display_precision from a non-None value to None, and 2) the effect of the bug is really only now apparent due to a recent frontend change (home-assistant/frontend#19396) that was released w/ 2024.2.

I noticed the bug when one of my custom sensor entities, which has a device_class of duration, changed its suggested_display_precision from 3 to None to compensate for the noted frontend change, yet the displayed value still wasn't formatted as HH:MM:SS. That's when I noticed core.entity_registry still had a suggested_display_precision of 3 for the entity instead of it being changed to null or removed.

Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Apr 16, 2024
@pnbruckner
Copy link
Contributor Author

I still believe this is valid and would appreciate a review.

@github-actions github-actions bot removed the stale label Apr 16, 2024
@emontnemery emontnemery force-pushed the pnb-suggested_display_precision branch from e4eba8f to 161dbda Compare April 24, 2024 13:57
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Thanks, @pnbruckner 👍

@emontnemery emontnemery merged commit 169b9b0 into dev Apr 24, 2024
38 checks passed
@emontnemery emontnemery deleted the pnb-suggested_display_precision branch April 24, 2024 14:47
@github-actions github-actions bot locked and limited conversation to collaborators Apr 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

suggested_display_precision not being removed from entity registry
3 participants