From 9af2dcffbf1047db866f8c8893920b0b94d12155 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 18 Oct 2023 12:12:58 +0200 Subject: [PATCH 1/3] Allow multiple states in conditional card --- src/panels/lovelace/common/validate-condition.ts | 13 +++++++------ .../conditions/types/ha-card-condition-state.ts | 5 ++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/panels/lovelace/common/validate-condition.ts b/src/panels/lovelace/common/validate-condition.ts index 1d17f44b1047..33827c033f67 100644 --- a/src/panels/lovelace/common/validate-condition.ts +++ b/src/panels/lovelace/common/validate-condition.ts @@ -1,3 +1,4 @@ +import { ensureArray } from "../../../common/array/ensure-array"; import { UNAVAILABLE } from "../../../data/entity"; import { HomeAssistant } from "../../../types"; @@ -5,15 +6,15 @@ export type Condition = StateCondition | ScreenCondition; export type LegacyCondition = { entity?: string; - state?: string; - state_not?: string; + state?: string | string[]; + state_not?: string | string[]; }; export type StateCondition = { condition: "state"; entity?: string; - state?: string; - state_not?: string; + state?: string | string[]; + state_not?: string | string[]; }; export type ScreenCondition = { @@ -31,8 +32,8 @@ function checkStateCondition( : UNAVAILABLE; return condition.state != null - ? state === condition.state - : state !== condition.state_not; + ? ensureArray(condition.state).includes(state) + : ensureArray(condition.state_not).includes(state); } function checkScreenCondition( diff --git a/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts b/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts index ed4d4718ec93..cc3b91a690a6 100644 --- a/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts +++ b/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts @@ -103,7 +103,10 @@ export class HaCardConditionState extends LitElement { ...content, entity: this.condition.entity ?? "", invert: this.condition.state_not ? "true" : "false", - state: this.condition.state_not ?? this.condition.state ?? "", + state: + (this.condition.state_not as string | undefined) ?? + (this.condition.state as string | undefined) ?? + "", }; return html` From 0d76c4932e243109ef5a2618bb6cffa90f8264bd Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 23 Oct 2023 15:02:05 +0200 Subject: [PATCH 2/3] Update src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts Co-authored-by: Bram Kragten --- .../editor/conditions/types/ha-card-condition-state.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts b/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts index cc3b91a690a6..cf96a952608d 100644 --- a/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts +++ b/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts @@ -104,8 +104,8 @@ export class HaCardConditionState extends LitElement { entity: this.condition.entity ?? "", invert: this.condition.state_not ? "true" : "false", state: - (this.condition.state_not as string | undefined) ?? - (this.condition.state as string | undefined) ?? + (this.condition.state_not as string | string[] | undefined) ?? + (this.condition.state as string | string[] | undefined) ?? "", }; From 0ed5cff4e5733cd0192785adf787dffc8da06058 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 23 Oct 2023 15:35:54 +0200 Subject: [PATCH 3/3] Fix types --- .../lovelace/editor/conditions/types/ha-card-condition-state.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts b/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts index cf96a952608d..8c9234d221b3 100644 --- a/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts +++ b/src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts @@ -21,7 +21,7 @@ type StateConditionData = { condition: "state"; entity: string; invert: "true" | "false"; - state?: string; + state?: string | string[]; }; @customElement("ha-card-condition-state")