Skip to content

Import the cloud weigh-in history as long-term statistics - #3

Merged
AboveColin merged 3 commits into
mainfrom
feature/import-history
Sep 15, 2026
Merged

AboveColin merged 3 commits into
mainfrom
feature/import-history

Conversation

@AboveColin

Copy link
Copy Markdown
Owner

Closes #2. SteveJaye84 asked whether past scale reports can be pulled from the account, or whether only measurements taken after install are kept.

They already are pulled. FitdaysClient.get_sync(days=HISTORY_DAYS) fetches 400 days on every 30-minute refresh, and the coordinator threw all of it away except the newest reading, so the recorder started each graph at install time.

What this does

  • custom_components/fitdays/statistics.py groups each profile's downloaded history into hourly buckets per sensor and calls async_import_statistics with the sensor's own entity id, writing min, max and mean per hour.
  • The coordinator now keeps the full window in its data under measurements; the sensors still read only latest.
  • The import runs at the end of async_setup_entry, after async_forward_entry_setups, because a statistics row is keyed by entity id and the entity id does not exist before the platform is set up.
  • The manifest lists recorder under after_dependencies, not dependencies, so an installation without a recorder is not forced to load one. The import returns 0 when recorder is absent from hass.config.components.

Scope and limits, also written into the README

  • Statistics only. Numeric sensors carrying SensorStateClass.MEASUREMENT get a past; the measurement counter and the timestamp sensor do not.
  • Two weigh-ins inside one hour collapse into a single row with min, max and mean, which is the recorder's resolution.
  • No state history and no logbook entries for the past. The history view still starts when the integration does.
  • Re-importing an hour overwrites that row, so a restart rewrites rather than duplicates.
  • 400 days is the client default. Anything older is not downloaded and cannot be imported.

Compatibility

StatisticMetaData changed: has_mean is the pre-2025.11 spelling and the recorder drops it in 2026.4. _metadata sets mean_type=StatisticMeanType.ARITHMETIC when the enum imports and falls back to has_mean=True otherwise, so the 2024.11.0 floor in hacs.json still holds.

Receipts

  • Verified against homeassistant 2026.2.3 in a throwaway venv: async_import_statistics(hass, metadata, statistics), StatisticMetaData now carries mean_type with has_mean marked deprecated for removal in 2026.4, and _async_import_statistics converts a leftover has_mean to mean_type itself.
  • Bucketing tested against the real StatisticData/StatisticMetaData models with 5 synthetic measurements: 2 rows out, the 08:41 and 08:56 pair collapsing to min=80.0 max=81.0 mean=80.5 at 08:00 UTC, every start on the hour, rows sorted oldest first, and the rows with a None timestamp or a None value dropped.
  • ruff check custom_components/fitdays: all checks passed. ruff format: clean.
  • Not verified: a live end-to-end import. I have no Fitdays credentials on this machine, so the depth the API actually returns for a 400-day window is still the client's default, not a measured number.

Base

This branch starts at 26b0e1e, the v1.0.3 release commit, which is not an ancestor of main on GitHub. main sits at a7679d3 with manifest version 1.0.2, so the release artwork and version bump never landed on the branch. Merging this PR brings that commit onto main along with the feature. That divergence predates this work.

Manifest version bumped to 1.1.0.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 71a92fba-b74d-421b-84de-62823f967928


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

AboveColin and others added 3 commits September 15, 2026 14:33
….0.3

frenck's hacs/default review was against v1.0.0; the homeassistant floor
and the README wording on body-composition/heart-rate sensors going
unknown were already fixed by then. The remaining item was brand assets:
custom_components/fitdays/brand/ held only a placeholder icon, no logo.
Replace it with the real teal icon and logo (1x/2x, no dark variant
needed) and update the README note that called it a placeholder.
The coordinator already downloads 400 days of measurements on every refresh
and then kept only the newest one, so the recorder started every graph at
install time. Issue #2 asked for the past readings.

statistics.py groups each profile's history into hourly buckets per sensor and
hands them to the recorder under the sensor's own entity id, min, max and mean
per hour. It runs after the platforms are set up, because a statistics row is
keyed by entity id. Re-importing an hour overwrites that row, so a restart
rewrites rather than duplicates.

Only sensors with a MEASUREMENT state class are backfilled; the counter and
the timestamp sensors have no mean. Disabled entities are skipped. The import
returns early when the recorder is not set up, which is why the manifest lists
recorder under after_dependencies rather than dependencies.

mean_type is used where the enum exists and has_mean otherwise: has_mean is
the pre-2025.11 spelling and the recorder drops it in 2026.4.

Closes #2

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Home Assistant 2026.9 logs a frame warning when async_import_statistics is
called without unit_class, and the recorder stops accepting metadata without
the key in 2026.11. Derive it from STATISTIC_UNIT_TO_UNIT_CONVERTER the way
the recorder derives it, and skip the key on cores that predate it, so the
2024.11 floor in hacs.json still holds.

Measured on the ha-dev instance (Home Assistant 2026.9, podman container
ha-dev on nix1): before the change the metadata was accepted and the warning
fired; after it the metadata carries unit_class 'mass' for a kg sensor, no
warning appears, and the two hourly rows land in statistics_meta/statistics
with min 80.0, max 81.0, mean 80.5 at 08:00 UTC and 79.5/79.5/79.5 at 09:00.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@AboveColin
AboveColin force-pushed the feature/import-history branch from cf9cf72 to 05aa0c4 Compare September 15, 2026 12:33
@AboveColin
AboveColin merged commit d43b5b4 into main Sep 15, 2026
5 checks passed
@AboveColin
AboveColin deleted the feature/import-history branch September 15, 2026 12:35
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.

Is it possible to pull history from the cloud?

1 participant