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
@@ -1,8 +1,19 @@
import type { InitializedBase } from '../../../config/schema/condition-trigger/common/initialized';
import type { ConditionsEvaluationResult, ConditionState } from '../types';
import type { ConditionEvaluator } from './types';
export class InitializedConditionEvaluator implements ConditionEvaluator {
private _condition: InitializedBase;
constructor(condition: InitializedBase) {
this._condition = condition;
}
public evaluate(newState?: ConditionState): ConditionsEvaluationResult {
return { result: !!newState?.initialized };
return {
result: !!(this._condition.ever
? newState?.everInitialized
: newState?.initialized),
};
}
}
+3 -1
View File
@@ -62,7 +62,7 @@ export const createConditionEvaluator = (
case 'user_agent':
return new UserAgentConditionEvaluator(condition);
case 'initialized':
return new InitializedConditionEvaluator();
return new InitializedConditionEvaluator(condition);
case 'template':
return new TemplateConditionEvaluator(condition, context);
case 'or':
@@ -123,6 +123,8 @@ export const createConditionEvaluatorForTrigger = (
return new ExpandConditionEvaluator(trigger);
case 'fullscreen':
return new FullscreenConditionEvaluator(trigger);
case 'initialized':
return new InitializedConditionEvaluator(trigger);
case 'interaction':
return new InteractionConditionEvaluator(trigger);
case 'key':
+6 -1
View File
@@ -26,7 +26,6 @@ export interface ConditionState {
displayMode?: ViewDisplayMode;
expand?: boolean;
fullscreen?: boolean;
initialized?: boolean;
interaction?: boolean;
keys?: KeysState;
mediaLoadedInfo?: MediaLoadedInfo | null;
@@ -34,6 +33,12 @@ export interface ConditionState {
panel?: boolean;
hass?: HomeAssistant;
// Initialization:
// - Currently initialized.
initialized?: boolean;
// - Ever initialized.
everInitialized?: boolean;
// Generic media target identifier. See @view/target-id for details.
targetID?: string;
triggered?: Set<string>;
@@ -6,6 +6,6 @@ export class InitializedTrigger extends ConditionStateTriggerBase<
TriggerOfType<'initialized'>
> {
protected _getValue(state: ConditionState): unknown {
return state.initialized;
return this._trigger.ever ? state.everInitialized : state.initialized;
}
}