Skip to content

Add finalized energy history import for Ohme#169679

Draft
tstordyallison wants to merge 9 commits intohome-assistant:devfrom
tstordyallison:tstordyallison/ohme-energy-draft
Draft

Add finalized energy history import for Ohme#169679
tstordyallison wants to merge 9 commits intohome-assistant:devfrom
tstordyallison:tstordyallison/ohme-energy-draft

Conversation

@tstordyallison
Copy link
Copy Markdown

@tstordyallison tstordyallison commented May 3, 2026

Summary

  • add a statistics-only finalized-energy history import path for Ohme
  • bootstrap and repair imported recorder statistics at startup
  • trigger bounded history re-imports when a charge session appears to finalize
  • add an integration option for how many days of hourly history to backfill

Approach

This keeps the live Ohme entity model unchanged and imports finalized charge history into recorder as an external statistic, rather than introducing a new dedicated total_charged_energy sensor entity.

Sync Model

  • First setup, missing statistics, or a backfill_days change triggers a rebuild of the configured history window.
  • Normal startup recovery re-imports from the last imported hour minus one hour of overlap, rather than rebuilding everything.
  • Session-driven syncs re-import from the observed session start forward when Ohme exposes a finalized session marker.
  • If the session markers are incomplete, unplug/finalized status transitions fall back to a bounded repair window instead.
  • One delayed retry after finalization plus a daily bounded repair pass handle late provider finalization without polling the summary API continuously.
  • Large windows use MONTH summary calls to prefilter active ranges, then fetch HOUR buckets only inside those ranges.
  • Small windows skip the prefilter and fetch HOUR buckets directly.
  • The intended model is convergent: overlapping re-imports are expected, and the probe work here suggested the Ohme summary API only reflects completed/finalized charging rather than the current in-progress session.

Why Statistics-Only

I chose the statistics-only route because it keeps the integration surface smaller and avoids introducing another user-facing sensor whose main purpose would be Energy Dashboard compatibility.

That does mean the imported energy history is authoritative in recorder/statistics rather than as a dedicated entity state.

Similar Patterns In Core

This is not totally novel in Home Assistant core. A few existing energy-focused integrations already manage external recorder statistics from provider-owned historical data rather than relying only on live entity history:

The Ohme version here is a bit more session/finalization-driven because of the API shape, but the general pattern of using imported external statistics as the authoritative long-term energy history is already established elsewhere in core.

Tradeoffs / Known Non-Ideal Areas

  • The session-finalization logic is heuristic-based. It uses explicit finish markers when present, then unplug fallback, then a delayed retry, then daily repair. That is meant to tolerate late provider finalization rather than assume the summary API is immediately consistent.
  • A missing imported statistic or a backfill_days option change currently triggers a rebuild of the configured history window. That is simple and robust, but not the cheapest possible path for very large histories. It's probably what you want if you tweak it to go further back though.
  • The statistics source is external-only. There is still no standalone total_charged_energy entity. I did try having one but HA gets very unhappy if you make it the same one. We could always add it on the side just as a number, but my fear is people would select it in the Energy dashboard and it wouldn't work properly (we couldn't update the past and the updates would be in the wrong buckets).

Feedback Areas

  • Is the statistics-only approach OK here, or would a dedicated energy entity still be expected? It's a little less obvious but I think the best all round. It shows up in the dashboard view but not many other places (you can use a Statistic card with it directly too).
  • Is the finalized-session + delayed-retry + daily-repair model a reasonable way to handle eventual consistency from the Ohme summary API? This part is all a bit naff, but I was keen to not have a whole polling of the summary charge data in addition to the normal polling.
  • Is the rebuild/recovery split the right level of complexity, or should this be simplified further before merge?

Companion Dependency

This draft intentionally depends on the companion ohmepy change that exposes public session_start / session_finish accessors and restores MONTH summary granularity:

Testing

  • python -m compileall homeassistant/components/ohme tests/components/ohme
  • python -m pytest -q tests/components/ohme/test_sensor.py tests/components/ohme/test_init.py tests/components/ohme/test_history.py
    • result: 19 passed
    • snapshots: 15 snapshots passed

Copilot AI review requested due to automatic review settings May 3, 2026 09:18
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented May 3, 2026

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

Code owner commands

Code owners of ohme can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign ohme Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a statistics-only finalized energy-history import path to the Ohme integration so Home Assistant can backfill and repair recorder statistics for charging energy without introducing a new user-facing sensor entity.

Changes:

  • Adds new history import/rebuild/recovery helpers for finalized Ohme charging data and wires them into integration startup.
  • Extends the charge-session coordinator to trigger bounded re-imports on session finalization, delayed retries, and daily repair syncs.
  • Adds an options flow and supporting tests/snapshots for configurable history backfill and the updated sensor naming/behavior.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
homeassistant/components/ohme/__init__.py Initializes history sync during setup and toggles sync orchestration on load/unload.
homeassistant/components/ohme/config_flow.py Adds an options flow for backfill_days and stores default options on initial setup.
homeassistant/components/ohme/const.py Defines new history/backfill-related constants.
homeassistant/components/ohme/coordinator.py Adds session-finalization detection, queued history syncs, delayed retry, and daily repair scheduling.
homeassistant/components/ohme/history.py Implements recorder-statistics import, rebuild, recovery, and bounded repair logic.
homeassistant/components/ohme/manifest.json Switches the integration dependency to a branch-based ohme package source.
homeassistant/components/ohme/sensor.py Renames the live energy sensor translation and keeps sensor setup aligned with runtime data.
homeassistant/components/ohme/services.py Refactors config-entry lookup helpers and guards against duplicate service registration.
homeassistant/components/ohme/strings.json Adds options-flow strings and the new sensor translation.
tests/components/ohme/conftest.py Extends the mock Ohme client with summary/session-history fields and defaults.
tests/components/ohme/snapshots/test_sensor.ambr Updates sensor snapshots for the renamed energy sensor and metadata changes.
tests/components/ohme/test_config_flow.py Covers default options on setup and the new options flow.
tests/components/ohme/test_history.py Adds focused coverage for bootstrap, rebuild/recovery, session-finalization syncs, retries, and repair windows.
tests/components/ohme/test_sensor.py Updates live sensor expectations and adds coverage for non-fatal summary failures at setup.

"quality_scale": "platinum",
"requirements": ["ohme==1.9.0"]
"requirements": [
"ohme @ git+https://github.com/tstordyallison/ohmepy.git@public-session-timestamps-and-summary-granularity"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Waiting on dan-r/ohmepy#9

Comment thread homeassistant/components/ohme/__init__.py
Comment thread homeassistant/components/ohme/sensor.py Outdated
@tstordyallison
Copy link
Copy Markdown
Author

tstordyallison commented May 3, 2026

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@dan-r before I can sign the CLA - I need some internal approvals from my employer. Shouldn't be a problem (HA isn't exactly a trading platform!) but will take a few days.

@tstordyallison tstordyallison force-pushed the tstordyallison/ohme-energy-draft branch from 0c98b7e to 1cfb5d9 Compare May 3, 2026 09:48
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copilot AI review requested due to automatic review settings May 3, 2026 09:57
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 6 comments.

Comment thread homeassistant/components/ohme/history.py
Comment thread homeassistant/components/ohme/history.py
Comment thread homeassistant/components/ohme/__init__.py
Comment thread homeassistant/components/ohme/coordinator.py
Comment thread homeassistant/components/ohme/history.py
Comment thread homeassistant/components/ohme/manifest.json
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copilot AI review requested due to automatic review settings May 3, 2026 12:40
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@tstordyallison
Copy link
Copy Markdown
Author

Woah, the co-pilot review really kicked in! I'll take a look at the comments and see what's what. They look like the are probably right!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copilot AI review requested due to automatic review settings May 3, 2026 14:28
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Comment on lines +92 to 93
entry.runtime_data.charge_session_coordinator.disable_history_sync()
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
Comment on lines +319 to +325
StatisticData(start=window_start, state=current_sum, sum=current_sum)
)

for bucket_start, bucket_kwh in hourly_points:
current_sum = round(current_sum + bucket_kwh, 6)
statistics.append(
StatisticData(start=bucket_start, state=current_sum, sum=current_sum)
Comment on lines +376 to +392
stats = await get_instance(hass).async_add_executor_job(
statistics_during_period,
hass,
FULL_HISTORY_START,
end,
{stat_id},
"hour",
None,
{"sum"},
)
rows = stats.get(stat_id, [])
if not rows:
return 0.0
last_sum = rows[-1]["sum"]
return float(last_sum) if last_sum is not None else 0.0


Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copilot AI review requested due to automatic review settings May 3, 2026 14:42
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @tstordyallison

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants