Skip to content

Fix history/sensor cards stuck loading after backend restart#51531

Open
RaHehl wants to merge 3 commits intohome-assistant:devfrom
RaHehl:fix/history-cards-reconnect
Open

Fix history/sensor cards stuck loading after backend restart#51531
RaHehl wants to merge 3 commits intohome-assistant:devfrom
RaHehl:fix/history-cards-reconnect

Conversation

@RaHehl
Copy link
Copy Markdown
Contributor

@RaHehl RaHehl commented Apr 12, 2026

Breaking change

Proposed change

After a backend restart, history graph cards, map cards with history paths, graph header/footers, and the more-info history dialog would get stuck in a loading state and never recover without a full page refresh.

This happened because of a 3-part failure:

  1. On reconnect, the WebSocket library would auto-resubscribe history streams, but the HistoryStream object holds mutable internal state that becomes corrupt when resubscribed with stale data.
  2. Lovelace cards are destroyed and recreated on reconnect. The new cards call isComponentLoaded("history") which returns false because the full component list hasn't arrived yet.
  3. shouldUpdate() blocks hass-only updates, so updated() never fires to retry the subscription when components become available.

This PR fixes all three issues:

  • Adds { resubscribe: false } to history stream subscriptions to prevent corrupt auto-resubscription
  • Adds connection-status event handlers to all affected components to cleanly re-subscribe on reconnect
  • Adds retry logic in shouldUpdate/updated/willUpdate to subscribe once the history component becomes available
  • Adds a sentinel pattern to prevent race conditions from re-entrant async subscription calls
  • Clears the sensor device classes cache on WebSocket errors so retries can succeed
  • Adds .catch() to unsubscribe calls to handle dead server-side subscriptions gracefully

Screenshots

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to backend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

To help with the load of incoming pull requests:

- Add { resubscribe: false } to history subscriptions to prevent
  corrupt HistoryStream state on auto-resubscription
- Add connection-status handlers to re-subscribe on reconnect
- Add sentinel pattern to prevent re-entrant async subscriptions
- Add shouldUpdate/updated retry when components become available
- Clear sensor device classes cache on WS error
- Clear _error on reconnect so cards can retry
- Add .catch() on unsubscribe to handle dead subscriptions
Copilot AI review requested due to automatic review settings April 12, 2026 21:12
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

Fixes history-based UI (Lovelace history graph card, map card history paths, graph header/footer, and more-info history) getting stuck in a loading state after a backend restart by making history stream subscriptions reconnect-safe and adding retry logic when components become available.

Changes:

  • Disable WebSocket auto-resubscription for history streams to avoid HistoryStream internal state corruption.
  • Add reconnect (connection-status) handling + retry-on-components-available logic to affected cards/dialogs, plus a sentinel pattern to prevent re-entrant async subscription races.
  • Improve resiliency by clearing the sensor numeric device class cache on WS errors and swallowing unsubscribe failures from dead server-side subscriptions.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/panels/lovelace/header-footer/hui-graph-header-footer.ts Reconnect-driven unsubscribe/resubscribe, retry on component availability, and safer unsubscribe handling.
src/panels/lovelace/cards/hui-map-card.ts Allow updates when component list changes, reconnect-driven resubscribe, and safer unsubscribe handling.
src/panels/lovelace/cards/hui-history-graph-card.ts Sentinel anti-reentrancy for async subscribe, reconnect-driven resubscribe, component-availability retry, and safer unsubscribe handling.
src/panels/history/ha-panel-history.ts Safer unsubscribe handling (but now impacted by global history resubscribe change).
src/dialogs/more-info/ha-more-info-history.ts Reconnect-driven resubscribe/reset, component-availability retry, sentinel anti-reentrancy, and safer unsubscribe handling.
src/data/sensor.ts Clear getSensorNumericDeviceClasses cache on WS error so retries can succeed.
src/data/history.ts Disable auto-resubscription for history stream subscriptions (resubscribe: false).

…tory panel

- Use HASSDomEvent<ConnectionStatus> instead of (ev as CustomEvent).detail
  for proper type safety on all connection-status handlers
- Add connection-status handler to ha-panel-history so it re-subscribes
  after backend restart (addresses concern about resubscribe: false)
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