8000 Calculate derivative unit instantly on creation if possible by karwosts · Pull Request #147527 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Calculate derivative unit instantly on creation if possible #147527

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
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

karwosts
Copy link
Contributor
@karwosts karwosts commented Jun 25, 2025

Proposed change

Fix some issues with the derivative unit_of_measurement.

  1. Fixes Derivative sensor shows wrong unit #136419, wherein once a derivative sensor was created, it's displayed unit of measurement could not be changed, even when user modifies it through options flow, making the actual state internally calculate using a different unit, leading to wildly incorrect value.

  2. Improves derivative to calculate and set it's unit instantly when created based on the source sensor, instead of waiting for 2 updates of the source sensor when the first derivative could be calculated. The existing behavior is confusing, as after creating a sensor it could have no unit for many minutes or hours depending on how long it takes the source to change state. Also while it was waiting, it would ultimately be a problem for statistics, which would start recording data with unit "", and then when the real unit updated later, it would generate a statistics issue due to change of native unit.

  3. (added late) Update the derivative unit of measurement anytime the source sensor changes unit_of_measurement. Reset the derivative to 0 if the unit changes, since we don't want to calculate huge jumps if a sensor changes from e.g. Wh to kWh.

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.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @afaucogney, mind taking a look at this pull request as it has been labeled with an integration (derivative) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of derivative 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 derivative 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.

@frenck frenck requested a review from Copilot June 27, 2025 07:22
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves the derivative sensor’s behavior by immediately calculating and setting its unit_of_measurement upon creation, ensuring consistency between the state and its unit.

  • Fixes an issue where the derivative sensor’s unit was delayed in updating.
  • Adds tests to restore state and verifies dynamic updates through the options flow.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/components/derivative/test_sensor.py Adds test_restore to verify state and unit restoration on boot
tests/components/derivative/test_config_flow.py Adds awaits and assertions to ensure proper unit update after state changes
homeassistant/components/derivative/sensor.py Introduces new methods for unit derivation and state restoration, and refines state update handling
Comments suppressed due to low confidence (1)

tests/components/derivative/test_config_flow.py:115

  • [nitpick] Consider adding further assertions after updating the source sensor state to verify that unit_of_measurement is dynamically updated as expected.
    hass.states.async_set("sensor.valid", 10, {"unit_of_measurement": "cat"})

@dgomes
Copy link
Contributor
dgomes commented Jun 29, 2025

When addressing the unit of measurement we could share some patterns with "rienmann sum" integration

https://github.com/home-assistant/core/blob/dev/homeassistant/components/integration/sensor.py#L349

@karwosts
Copy link
Contributor Author

Yes I had been following that one a bit. It seems there the unit is recalculated on every source update.

Absent any further input I will probably change this to do the same, I just know this had been originally written to snap the unit only once, and without knowing why that was done was going to try to preserve that behavior, but perhaps that's undesirable anyway (and difficult to do).

@dgomes
Copy link
Contributor
dgomes commented Jun 29, 2025

Integral went through the same issue, calculate once and they more recently do it on every source change.

The decision came from the fact that some sources change unit

@karwosts
Copy link
Contributor Author

This has been changed to update the unit on every source state change.

@karwosts karwosts force-pushed the derivative_instant_unit branch from 66908c1 to 9742db9 Compare July 5, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
0