-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Conversation
Hey there @dgomes, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
87a2917
to
6d06fdb
Compare
@ronweikamp FYI |
c471798
to
e8e6776
Compare
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. |
This PR needs a rebase |
I think this is still valid. But need rebase. |
e8e6776
to
0cbf6d6
Compare
Rebased, but some new tests are needed |
@ronweikamp @dgomes @elupus I think this PR is ready, can you take a look please? |
There was a problem hiding this 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: |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
5e4d9d2
to
a16738d
Compare
homeassistant/core.py
Outdated
old_state: NotRequired[State | None] | ||
new_state: State | None | ||
|
||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
homeassistant/core.py
Outdated
old_state: NotRequired[State | None] | ||
new_state: State | None | ||
|
||
|
There was a problem hiding this comment.
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.
dea2581
to
2058777
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks 👍
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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proposed change
Use
state_reported
instead ofstate_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 TrueTest PRs which should be merged first:
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: