Fix for statistics #177
Open
disforw wants to merge 27 commits into
Open
Conversation
feat: set state_class TOTAL_INCREASING on water sensor
fix: statistics sensor reliability
fix: statistics reliability, service handler exception safety, and sensor improvements
kdeyev
reviewed
Apr 12, 2026
| _attr_has_entity_name = True | ||
| _attr_name = None | ||
| _attr_device_class = SensorDeviceClass.WATER | ||
| _attr_state_class = SensorStateClass.TOTAL_INCREASING |
Owner
There was a problem hiding this comment.
I think this one will bring back a race condition, I was fighting for a couple of years.
I do not mind to release it a s beta for testing :-)
kdeyev
reviewed
Apr 12, 2026
| "source": "eyeonwater", | ||
| "statistic_id": statistic_id, | ||
| "unit_of_measurement": currency, | ||
| "unit_class": "monetary", |
Owner
There was a problem hiding this comment.
I've reverted the "monetary" intentionally because it was causing an exception
kdeyev flagged _attr_state_class = TOTAL_INCREASING as re-introducing a race condition he'd previously resolved. Removing it keeps the sensor consistent with upstream and passes test_no_state_class.
kdeyev reverted this intentionally because it was causing an exception in their HA instance. Removing it keeps the cost metadata consistent with upstream and passes test_get_cost_statistic_metadata.
Syncs with kdeyev's test_get_cost_statistic_metadata which asserts "unit_class" not in meta. This matches the reverted implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes the statistics population failures that caused duplicate re-imports, silent WebSocket crashes, and incorrect timezone comparisons.
Changes
init.py — Wrap import_historical_data service handler in try/except. Without this, any API error propagates into HA's WebSocket handler and returns an opaque "unknown_error" in the frontend.
coordinator.py
Update _last_imported_times cache after a service-call import. Previously the cache was never updated, so the next regular coordinator poll would re-import all the data that was just written, causing duplicate statistics.
Guard import_historical_data against non-positive days values.
statistic_helper.py
filter_newer_data: normalise both timestamps to UTC before comparing. last_imported_time (from HA recorder) and DataPoint.dt (from pyonwater) may use different timezone objects; comparing them directly can silently skip valid data points.
get_last_imported_time: use datetime.timezone.utc explicitly in fromtimestamp instead of dtutil.DEFAULT_TIME_ZONE, which can be None on some HA configurations.
get_cost_statistic_metadata: add unit_class="monetary" so HA displays cost statistics correctly in the Energy dashboard.
Type of Change
Checklist
poetry run pytest)poetry run ruff check .)