fix: Clear the initialized condition state when the card is not usable (#2646)

The `initialized` state (used in conditions/triggers) was written once
and never cleared, so it meant "has this card ever been initialized"
while everything reading it took it as "is this card usable now". Home
Assistant takes a card off the page and puts it back whenever its
dashboard tab is left and returned to, so the card initialized again
while the state claimed it was initialized throughout: `trigger:
initialized` fired once per card rather than once per startup, and
automations were dropped in between.

The card lifecycle is now an explicit state machine (`SessionManager`),
the only writer of `initialized`, which separates a card that is
starting up from one initializing part of itself again while it runs. A
new `ever` parameter (conditions/triggers) selects the old latched
behaviour.

`remote_control` uses that parameter to keep its two camera priorities
correct under repeated starts. With `camera_priority: entity` the card
now re-reads the entity every time it starts, so a camera selected while
the card was away is picked up on return. With `camera_priority: card`
the card writes the entity on its first start only, unchanged, since
repeating that write would overwrite a camera the user had selected.

Closes: #2642


BREAKING CHANGE: `condition: initialized` is now `false` whenever the
card is not usable, and `trigger: initialized` fires each time the card
starts up rather than only the first time. Set `ever: true` on either to
keep the previous behaviour.
This commit is contained in:
Dermot Duffy
2026-08-02 12:52:58 -07:00
committed by GitHub
parent e590f72783
commit 17146c8ca6
44 changed files with 1967 additions and 654 deletions
+10
View File
@@ -168,6 +168,16 @@ export class FakeHASS {
this._renew();
}
/**
* Drop or restore the WebSocket, as a Home Assistant restart does. Home
* Assistant keeps handing the card a `hass` while it is disconnected, which
* is why this renews rather than going silent.
*/
public setConnected(connected: boolean): void {
this._connected = connected;
this._renew();
}
private _createEntity(
entityID: string,
options: FakeEntityOptions | string,
+30
View File
@@ -311,6 +311,36 @@ export class MountedCard {
this.card.hass = this._hass.getHASS();
}
/**
* Drop or restore the connection to Home Assistant, then hand the card the
* resulting `hass`.
*/
public setConnected(connected: boolean): void {
this._hass.setConnected(connected);
this.card.hass = this._hass.getHASS();
}
/**
* Give the card a new configuration.
*/
public setConfig(config: RawAdvancedCameraCardConfig): void {
this.card.setConfig(config);
}
/**
* Take the card off the page.
*/
public detach(): void {
this.card.remove();
}
/**
* Put the card back on the page.
*/
public attach(): void {
this._container.append(this.card);
}
/**
* Resolves once the card itself has rendered, which says nothing about the
* elements beneath it.
+18
View File
@@ -1,4 +1,5 @@
import type { RawAdvancedCameraCardConfig } from '../../src/config/types';
import type { MediaLoadedInfoEventDetail } from '../../src/types';
import { FakeHASS, type FakeEntityOptions } from './fake-hass';
export const STILL_CAMERA_ENTITY = 'camera.office';
@@ -158,6 +159,23 @@ export const deepQueryAll = <T extends Element = Element>(
...getImmediateShadowRoots(root).flatMap((child) => deepQueryAll<T>(child, selector)),
];
export const isMediaLoadedInfoEventDetail = (
detail: unknown,
): detail is MediaLoadedInfoEventDetail =>
!!detail &&
typeof detail === 'object' &&
'info' in detail &&
'signal' in detail &&
detail.signal instanceof AbortSignal;
/**
* The text of a block notification rendered in place of content (e.g. full-card
* issues).
*/
export const getBlockNotificationText = (root: ParentNode): string =>
deepQuery(root, 'advanced-camera-card-notification-block')?.shadowRoot?.textContent ??
'';
// Everything a provider can draw media on: an image, a video, or a canvas.
const MEDIA_SELECTOR = 'img, video, canvas';