Skip to content

Fix for statistics #177

Open
disforw wants to merge 27 commits into
kdeyev:masterfrom
disforw:master
Open

Fix for statistics #177
disforw wants to merge 27 commits into
kdeyev:masterfrom
disforw:master

Conversation

@disforw

@disforw disforw commented Apr 12, 2026

Copy link
Copy Markdown
Collaborator

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

  • [x ] Bug fix
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • Tests pass (poetry run pytest)
  • Linting is clean (poetry run ruff check .)
  • I have tested this on my own Home Assistant instance

disforw added 24 commits April 12, 2026 13:41
feat: set state_class TOTAL_INCREASING on water sensor
fix: statistics sensor reliability
fix: statistics reliability, service handler exception safety, and sensor improvements
Comment thread custom_components/eyeonwater/sensor.py Outdated
_attr_has_entity_name = True
_attr_name = None
_attr_device_class = SensorDeviceClass.WATER
_attr_state_class = SensorStateClass.TOTAL_INCREASING

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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 :-)

"source": "eyeonwater",
"statistic_id": statistic_id,
"unit_of_measurement": currency,
"unit_class": "monetary",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I've reverted the "monetary" intentionally because it was causing an exception

disforw added 3 commits April 20, 2026 22:14
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants