Skip to content

Sensor outages show as gaps; one Home Assistant client for both devices - #68

Merged
robdevops merged 7 commits into
mainfrom
claude/signalbot-feed
Dec 1, 2026
Merged

robdevops merged 7 commits into
mainfrom
claude/signalbot-feed

Conversation

@robdevops

Copy link
Copy Markdown
Owner

Fixes two problems in reading Home Assistant. When a sensor went offline, ecobot drew a flat line at its last value until now. And the two devices each had their own Home Assistant code.

Outages show as gaps

  • History import: Home Assistant's unavailable/unknown states now end a value, so nothing is stored for the outage.
  • Device that stops reporting: a device is taken to have stopped after 10 minutes with no change on any of its entities. Its values then end a report or two after their last change, both mid-history and at the end. A steady entity (NOx 1, rain rate 0) is still held while the rest of the device keeps changing.
  • Live readings: the air sensor's live reading uses the same 10-minute staleness rule as the station's. A reading is filed at its own time, once.
  • Charts: a line breaks where its readings stop (charts.runs) instead of being drawn straight across the gap.
  • Station: the same applies to the Ecowitt Home Assistant history fill and the min/max bands (ranges_of).
  • scripts/edit_readings.py: new --src and --delete options, to remove rows Home Assistant filled an outage with. The day figures are recalculated in the same transaction.

One Home Assistant package

  • New lib/homeassistant/: client (one per process, made in start_sources and held by each source), history, device (prefix finding and staleness) and units.
  • Device packages keep only their own entity map and checks.
  • Effect: one connection pool, one outage warning and its recovery, and one /api/states answer shared between the two devices.
  • Lifecycle: the shared file and client are released correctly when a source fails to start or to close.

Merge with main

Merges PR 67. Main's "ecobot-mcp" wording is applied to the moved code, and lib.homeassistant is added to the irrigation-mcp image's list of imports it must not make.

Checks

  • python -m pytest -q -m "": everything passes except the 2 animation tests in tests/test_web_dashboard.py. Those time out at 60 s here under parallel load, and they time out on main on the same 4-CPU machine too.
  • Before the merge, the branch passed 1961 of 1961, and 101 of 101 prompt evals (main has since removed the evals).

After deploying

To clear readings already stored for an outage, run this on the server. It's a dry run without --apply:

sudo python scripts/edit_readings.py co2 --sensor air --from <start> --to <end> --src home_assistant --delete

Generated by Claude Code

…alue

Home Assistant's history import held a value through "unavailable" and on
to now, so an offline AirGradient was stored (5-minute fills and minutes)
at its last reading for the whole outage; its live path re-filed a stale
state every minute; and charts bridged any gap with a straight line.

- states_of keeps unavailable/unknown as (t, None); rollup and ranges_of
  end a value there (no buckets, minutes or ranges until the next number;
  low/high/mean skip the gap). Shared with the station's HA import.
- HomeAssistant.history ends a value HOLD_SECONDS (300 s: five of the
  AirGradient integration's reports, a few of the gateway's, one bucket)
  after the sensor's last_reported, so one that stops reporting without
  going unavailable isn't held to now (steady values still reported stay).
- ha_live leaves out states last heard of over STALE_SECONDS (600) ago;
  _record files Home Assistant's reading at its own time, and once.
- Charts break a line where points are more than 4x apart of both the
  line's median spacing and the spacing either side (charts.runs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbGfzi38exq5AFMGFUMRMN
…reported

- Home Assistant's /api/states serves a state's JSON cached from when it was
  first sent: its last_reported isn't moved by a report of the same value.
  The hold cap and the live check used it per entity, so a steady reading
  (NOx 1 for hours, rain rate 0) was cut into a fake gap and left out of the
  live reading. Now a value ends HOLD_SECONDS (900) after the device's last
  change of any entity; the station's history is read with its temperatures,
  humidities and pressures (HEARTBEAT) so a group asked for alone (rain,
  solar at night) isn't taken for a stop. The live reading is None only when
  nothing of the monitor was heard of for STALE_SECONDS.
- charts.runs: the spacing around a step is the wider of the medians of the
  steps either side, so a reading or two alone in a long outage no longer
  bridges it.
- edit_readings.py: --src and --delete, to take out the rows Home Assistant
  filled an outage with.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbGfzi38exq5AFMGFUMRMN
- lib/homeassistant/: what both devices read of Home Assistant. client
  (HomeAssistant, Trouble, parse_time, states_of, ended, HOLD_SECONDS),
  history (rollup, ranges_of), device (find_prefix by a device's anchor
  entities, entity_ids, STALE_SECONDS with heard/fresh), units (convert,
  converted). lib/ecowitt/homeassistant.py is gone; the station's prefix
  and bands move to hass_api.Station; the air monitor's HA module no
  longer reaches into lib/ecowitt.
- One client per process: start_sources makes it and hands it to the
  station (and its HomeAssistantAPI) and the air sensor; each holds it
  (held) and lets go at close, the last close closes it.
- Staleness: the station's live reading through Home Assistant is stale
  by the air monitor's rule, none of the device's entities heard of for
  STALE_SECONDS, not the newest time of the groups asked for (a rain
  reading at 0 for hours no longer sends FallbackAPI to the cloud).
- Tests: a station outage in FallbackAPI.history (the cloud fills it,
  nothing carried across) and in the bands (import_ranges -> put_ranges);
  the shared staleness rule; holders closing the shared client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbGfzi38exq5AFMGFUMRMN
…e outage test on the fake's grid

- Storage is held like the Home Assistant client: the station takes a hold of the file it is given and lets go of it at close,
  so one that fails to start no longer closes the file the air sensor shares; start_sources lets go of its own hold once the
  station has started (else the air sensor's owned close is the last)
- The air sensor and the station let go of the shared client even when the rest of their close fails
- The station's cloud API is made before it takes its holds
- The outage test judges "inside" from the first fake report in the outage (HA_STEP grid), not the exact start: it failed
  about one run in ten when a 5-minute boundary fell between the two
- The shared-client test runs on the fake Home Assistant, not http://ha; a test pins that a state with an unreadable time
  keeps its value

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbGfzi38exq5AFMGFUMRMN
…he end; the fake station reports like a real one

- lib/homeassistant/client.py: `cut` replaces `ended`. Wherever none of a device's entities changes for HOLD_SECONDS
  (in the middle of the history or at its end) each value it holds ends HOLD_SECONDS after the last change, and comes
  back at the device's next change unless the entity has a state of its own then (Home Assistant records nothing for
  NOx 1 or rain rate 0 as a device resumes). History is asked from HOLD_SECONDS before the start, so a silence that
  began before a request (a day's chunk, the 3-hour re-import) isn't taken for a change at its start.
- tests/fakes.py: FakeHomeAssistant's HEARTBEAT entities wobble a little from report to report (seeded by entity and
  time), as a real gateway's readings almost never repeat; `silences` makes it stop reporting for a span with no
  "unavailable".
- The air sensor holds the file it is given and lets go of it at close (the owned= hand-off goes); a constructor that
  raises lets go of it first. start_sources always lets go of its own holds, in a finally.
- Tests: silent mid-history gaps for the station (Home Assistant's history, the cloud filling it, import_ranges) and the
  air sensor import, a steady entity back after the device resumes, no band missing over ten days of the fake, and the
  air sensor's failed constructor leaving the file's hold count as it was. docs/how-it-works.md updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbGfzi38exq5AFMGFUMRMN
…hange; nothing is put back from before a silence; sources hold nothing when they fail to be made

- lib/homeassistant/client.py `cut`: a device is taken to have stopped after 10 minutes with no change of any of its entities
  (HOLD_SECONDS 600, was 900: it is read every minute and one of its noisy readings changes nearly every time), and each value
  then ends REPORT_SECONDS (120) after its last change, not HOLD_SECONDS: no 15 minutes of a frozen value in the bands, the minutes
  or the 5-minute rows, and the cloud fills the silence from its start. As the device comes back, an entity's value from before
  the silence is put back only if it has no state of its own in that report (TOGETHER_SECONDS 10): Home Assistant stamps each
  entity of a report a few ms apart, so the old value was going into the band at the resume.
- tests/fakes.py: FakeHomeAssistant stamps each entity of a report a few ms apart, as Home Assistant does.
- The station's constructor lets go of the file and Home Assistant, and closes its cloud client, when it fails; the air sensor's
  closes its client and LocalSensor too (lib/warm.py close_soon, HomeAssistant.let_go). Source.close lets go of the file even when
  its client's close fails. start_sources closes a source whose start is cancelled.
- Tests for each; docs/how-it-works.md updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbGfzi38exq5AFMGFUMRMN
Conflicts: main's "the bot" -> "ecobot-mcp" wording is applied across the Home Assistant code this branch moved to
lib/homeassistant/ (and the files it changed); the Dockerfile takes main's two images, with lib.homeassistant added to
irrigation-mcp's list of imports it must not make; docs/how-it-works.md keeps this branch's Home Assistant and chart-gap
text in main's layout; tests/test_service_switch.py stays deleted with the bot's tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbGfzi38exq5AFMGFUMRMN
@robdevops
robdevops merged commit 559473b into main Dec 1, 2026
3 checks passed
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