Ingestion: derive consumption on ingest, and poll HA in minutes not seconds
ci / build-test (push) Successful in 1m13s

Live ingestion wrote the raw reading and stopped there. Import, the REST push
endpoint and the meter editor all recompute afterwards; the MQTT/Tasmota/HA
path was the one that did not, so a polled reading landed in `reading` and
every derived figure stayed frozen at the last import. Observed on a
GenerationCounter: 45 readings, 44 consumption rows, generation pinned to the
register value of the last imported reading.

Recompute inline rather than behind a debounce. Normalizing a whole meter is
cheap at metering cadence and a background dirty-set worker is machinery this
does not yet need; the remark on RenormalizeAsync records when it would.

Fixes a latent bug this surfaced in NormalizationService: ExecuteDelete drops
the consumption rows in the database but leaves them in the change tracker, so
a second recompute on the same context threw an identity conflict on
(meter, time, kind). One worker scope ingesting two readings was enough to hit
it. Detach the stale entries after the delete.

Poll interval is now minutes, default 60, replacing seconds/60. A meter answers
"how much this month, what will it cost" — an hourly sample answers that
exactly as well as a per-second one, with far less raw volume (SDD §5.5). The
`pollSeconds` key no longer binds, so existing sources fall back to the 60
default and move from every-60-seconds to hourly, which is the intent. A source
that had deliberately set e.g. 300 seconds also lands on 60 minutes.

Two test cleanups now delete consumption before the meter: live ingestion never
produced any before, so the FK had nothing to trip on.

Claude-Session: https://claude.ai/code/session_01V6joyergfvVLFEizH1hJLd
This commit is contained in:
2026-07-18 18:45:57 +02:00
parent e23df37a3f
commit 62d102c335
9 changed files with 114 additions and 19 deletions
+5 -3
View File
@@ -55,11 +55,13 @@ it as an MQTT source (above). No HA endpoint needed.
and a `HomeAssistant` source on the meter:
```json
{ "entityId": "sensor.house_power", "attribute": null, "pollSeconds": 60 }
{ "entityId": "sensor.house_power", "attribute": null, "pollMinutes": 60 }
```
Set `HA_TOKEN` (a long-lived access token) in the environment. Numeric state (or a named
`attribute`) is read every `pollSeconds`; `unavailable`/`unknown` states are skipped.
Set `HA_TOKEN` (a long-lived access token) in the environment, or type the token into the connector
dialog to have it encrypted at rest (SDD §6.4). Numeric state (or a named `attribute`) is read every
`pollMinutes` — default 60, because monthly totals and cost are identical whether a meter is sampled
hourly or per-second. `unavailable`/`unknown` states are skipped.
**C — HA pushes to the REST API.** POST to `/api/v1/readings` with an `X-Api-Key` header (see the
README). Good when HA should drive the cadence.