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 history stats count update immediately after change #131856

Merged
merged 2 commits into from
Nov 30, 2024

Conversation

karwosts
Copy link
Contributor

Proposed change

Fix history stats to update count immediately after a change.

What was broken was when we were checking to reject state changes in the future, we were accidentally comparing a floored value of now against the non-floored timestamp from the database, leading to think that the changes were in the future instead of the current change.

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:

@karwosts

This comment was marked as outdated.

@karwosts karwosts marked this pull request as draft November 28, 2024 16:52
@epenet
Copy link
Contributor

epenet commented Nov 28, 2024

Hmm not sure about that failing test yet.

It's flaky, and it was flaky since your previous PR (I think #126271).
pytest -x --count=100 tests/components/history_stats/test_sensor.py::test_async_on_entire_period

===================================================================================== short test summary info =====================================================================================
FAILED tests/components/history_stats/test_sensor.py::test_async_on_entire_period[6-100] - AssertionError: assert '99.9' == '100.0'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Results (4.07s):
       5 passed
       1 failed
         - tests/components/history_stats/test_sensor.py:446 test_async_on_entire_period[6-100]

@karwosts karwosts marked this pull request as ready for review November 28, 2024 22:33
@karwosts
Copy link
Contributor Author

Flaky test was fixed in #131869

@@ -186,8 +190,13 @@ def _async_compute_seconds_and_changes(
current_state_matches = history_state.state in self._entity_states
state_change_timestamp = history_state.last_changed
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't you do the flooring here? or else, you will calculate with the not-floored version moving forward ? (ie elapsed)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did actually try that but it breaks three tests that calculate the time with microsecond precision.

I'm not sure why that's important, but I don't think there's anything wrong with calculating elapsed with the non-floored time?

FAILED tests/components/history_stats/test_sensor.py::test_end_time_with_microseconds_zeroed[Europe/Berlin] - AssertionError: assert '16.0' == '16.0002388888929'
FAILED tests/components/history_stats/test_sensor.py::test_end_time_with_microseconds_zeroed[America/Chicago] - AssertionError: assert '16.0' == '16.0002388888929'
FAILED tests/components/history_stats/test_sensor.py::test_end_time_with_microseconds_zeroed[US/Hawaii] - AssertionError: assert '16.0' == '16.0002388888929'

But if you think we should change that I guess we can change the tests also, but seems more disruptive.

Copy link
Member

Choose a reason for hiding this comment

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

It was a drive by comment, checking if you knew why. I didn't verify it myself.

Copy link
Member

Choose a reason for hiding this comment

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

The fix looks fine 👍

@balloob balloob merged commit 9209e43 into home-assistant:dev Nov 30, 2024
34 checks passed
@karwosts karwosts deleted the hstats-fix-131847 branch November 30, 2024 13:26
balloob pushed a commit that referenced this pull request Dec 1, 2024
* Fix history stats count update immediately after change

* rerun CI
zweckj pushed a commit to zweckj/home-assistant-core that referenced this pull request Dec 1, 2024
…nt#131856)

* Fix history stats count update immediately after change

* rerun CI
@github-actions github-actions bot locked and limited conversation to collaborators Dec 2, 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.

history_stats sensor with count option not updating immediately
4 participants