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

Use state_reported events in Riemann sum sensor #113869

Merged
merged 4 commits into from
Jun 26, 2024

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Mar 20, 2024

Proposed change

Use state_reported instead of state_changed events in Riemann sum sensor.

Unlike state_changed events, state_reported events are fired every time an integration writes the sensor state, even when state and attributes are unchanged.

This fixes bugs where the calculated integral is incorrect for sensors which don't set force_update to True

Test PRs which should be merged first:

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

To help with the load of incoming pull requests:

@home-assistant
Copy link

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

Code owner commands

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

@emontnemery emontnemery mentioned this pull request Mar 20, 2024
20 tasks
@emontnemery emontnemery force-pushed the integration_sensor_last_reported branch from 87a2917 to 6d06fdb Compare March 21, 2024 09:21
@emontnemery emontnemery marked this pull request as ready for review March 21, 2024 09:21
@emontnemery
Copy link
Contributor Author

@ronweikamp FYI

@ronweikamp
Copy link
Contributor

In your review of #110685 you requested to extract the refactoring. Although not strictly necessary, it might be a better base for this PR as well #113932 .

@emontnemery emontnemery force-pushed the integration_sensor_last_reported branch from c471798 to e8e6776 Compare March 28, 2024 11:55
@ronweikamp
Copy link
Contributor

What issues does this PR address or what is the reason of this PR?

Can we close this in favor of max_sub_interval #110685? In that PR people started to discuss whether state_reported or max_sub_interval is a better solution for the issue that the integral doesn't update when the source is constant.

@rkistner
Copy link

rkistner commented May 4, 2024

What issues does this PR address or what is the reason of this PR?

Can we close this in favor of max_sub_interval #110685? In that PR people started to discuss whether state_reported or max_sub_interval is a better solution for the issue that the integral doesn't update when the source is constant.

#110685 can solve the same issue, but requires additional configuration.

This PR solves it without any additional configuration, which means the default behavior won't be broken anymore. It only works for cases where the sensor reports values even if unchanged, so there is value in having both.

@dgomes
Copy link
Contributor

dgomes commented May 17, 2024

This PR needs a rebase

@elupus
Copy link
Contributor

elupus commented Jun 18, 2024

I think this is still valid. But need rebase.

@emontnemery emontnemery force-pushed the integration_sensor_last_reported branch from e8e6776 to 0cbf6d6 Compare June 24, 2024 07:53
@emontnemery
Copy link
Contributor Author

Rebased, but some new tests are needed

@frenck frenck added this to the 2024.7.0b0 milestone Jun 24, 2024
@emontnemery
Copy link
Contributor Author

@ronweikamp @dgomes @elupus I think this PR is ready, can you take a look please?

Copy link
Contributor

@dgomes dgomes left a comment

Choose a reason for hiding this comment

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

We should add a test that clearly benefits from state_reported

def validate_states(
self, left: State, right: State
) -> tuple[Decimal, Decimal] | None:
def validate_states(self, left: str, right: str) -> tuple[Decimal, Decimal] | None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the move from State to str ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's because we don't have two complete state objects when only the reported time is bumped

@emontnemery emontnemery force-pushed the integration_sensor_last_reported branch from 5e4d9d2 to a16738d Compare June 25, 2024 20:09
old_state: NotRequired[State | None]
new_state: State | None


Copy link
Contributor

Choose a reason for hiding this comment

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

I was mucking around with this in another change. The reported event is actually a union of changed or reported structures. It was somewhat tricky to get the typing correct.

The EVENT_STATE_REPORTED constant should really be set up go map to the event definition, but was tricky to right with a union of types. Maybe that works with this squashed class.

If we have a squashed class, maybe there should be some doc string to indicate that it contains the full changed event or the reported event?

Copy link
Contributor

@elupus elupus Jun 25, 2024

Choose a reason for hiding this comment

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

Were not fully how i expected it to be resolved. Somewhat easy to break this if the changed event data is modified. Typing wont catch that reported event piggy backs on the same data as changed.
But im okey with this.

Copy link
Contributor

@elupus elupus left a comment

Choose a reason for hiding this comment

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

I think this looks alright. I have some itches with how the reported event have some rather weird overlap with changed event, but doesn't need to block this.

old_state: NotRequired[State | None]
new_state: State | None


Copy link
Contributor

@elupus elupus Jun 25, 2024

Choose a reason for hiding this comment

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

Were not fully how i expected it to be resolved. Somewhat easy to break this if the changed event data is modified. Typing wont catch that reported event piggy backs on the same data as changed.
But im okey with this.

@emontnemery emontnemery force-pushed the integration_sensor_last_reported branch from dea2581 to 2058777 Compare June 26, 2024 09:29
Copy link
Contributor

@elupus elupus left a comment

Choose a reason for hiding this comment

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

Looks 👍

@frenck frenck merged commit a36c40a into dev Jun 26, 2024
25 checks passed
@frenck frenck deleted the integration_sensor_last_reported branch June 26, 2024 11:35
Comment on lines -446 to +459
async_track_state_change_event(
self.hass,
[self._sensor_source_id],
self.hass.bus.async_listen(
EVENT_STATE_CHANGED,
handle_state_change,
event_filter=callback(
lambda event_data: event_data["entity_id"] == self._sensor_source_id
),
run_immediately=True,
)
)
self.async_on_remove(
self.hass.bus.async_listen(
EVENT_STATE_REPORTED,
handle_state_report,
event_filter=callback(
lambda event_data: event_data["entity_id"] == self._sensor_source_id
),
run_immediately=True,
Copy link
Member

@bdraco bdraco Jun 26, 2024

Choose a reason for hiding this comment

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

I'm getting profiles from beta users this lambda being called 1M/min

I think we need to switch the first one back to async_track_state_change_event and than make an async_track_state_change_reported so we can dispatch based on entity id since we now have 2 * X integration entities top level listeners with this change

#120621

Copy link
Member

Choose a reason for hiding this comment

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

Small snapshot of the lambdas being called.
Screenshot 2024-06-26 at 5 16 48 PM

@github-actions github-actions bot locked and limited conversation to collaborators Jun 27, 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.

7 participants