fix: Match state trigger/condition semantics to HA (#2565)

* Closes #2530
This commit is contained in:
Dermot Duffy
2026-07-04 10:23:51 -07:00
committed by GitHub
parent 3494706225
commit 9f956fe89f
15 changed files with 825 additions and 69 deletions
@@ -1,7 +1,10 @@
import { z } from 'zod';
import { stringOrArray } from '../../../common/string-or-array';
import { stateBaseSchema } from '../../common/state';
import {
checkStateMatchField,
stateBaseSchema,
stateMatchValueSchema,
} from '../../common/state';
import { conditionBaseSchema } from '../base';
import { entityConditionBaseSchema } from './entity-base';
@@ -13,13 +16,16 @@ export const stateConditionSchema = entityConditionBaseSchema
// If `condition` is omitted a state condition is assumed (picture-elements form).
condition: z.literal('state').optional(),
// Common to both of Home Assistant's condition dialects:
state: stringOrArray.optional(),
// Only present in HA picture elements dialect (not automation dialect), but
// respected in both usecases in this card.
// Without `attribute` these are string/list state matchers (enforced by the
// `superRefine` below); with `attribute` they compare raw against the
// attribute value, so any type is accepted.
//
// `state` is common to both of Home Assistant's condition dialects;
// `state_not` is only present in HA's picture-elements dialect (not the
// automation dialect), but respected in both usecases in this card.
// https://www.home-assistant.io/dashboards/picture-elements/#conditional-element
state_not: stringOrArray.optional(),
state: stateMatchValueSchema,
state_not: stateMatchValueSchema,
// How a list of entities is combined: `all` (the default) requires every
// entity to match, `any` requires at least one.
@@ -31,4 +37,12 @@ export const stateConditionSchema = entityConditionBaseSchema
.refine(
(data) => data.state !== undefined || data.state_not !== undefined,
'A `state` condition requires `state` or `state_not`',
);
)
// Without `attribute`, the match fields keep HA's string/list form.
.superRefine((data, ctx) => {
if (data.attribute !== undefined) {
return;
}
checkStateMatchField(ctx, 'state', data.state, { nullable: false });
checkStateMatchField(ctx, 'state_not', data.state_not, { nullable: false });
});