Skip to content

Commit

Permalink
Allow multiple states in conditional card (#18273)
Browse files Browse the repository at this point in the history
* Allow multiple states in conditional card

* Update src/panels/lovelace/editor/conditions/types/ha-card-condition-state.ts

Co-authored-by: Bram Kragten <[email protected]>

---------

Co-authored-by: Bram Kragten <[email protected]>
  • Loading branch information
piitaya and bramkragten authored Oct 23, 2023
1 parent eedb42b commit 9bafbdd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/panels/lovelace/common/validate-condition.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { ensureArray } from "../../../common/array/ensure-array";
import { UNAVAILABLE } from "../../../data/entity";
import { HomeAssistant } from "../../../types";

export type Condition = StateCondition | ScreenCondition | UserCondition;

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 = {
Expand All @@ -36,8 +37,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(condition: ScreenCondition, _: HomeAssistant) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type StateConditionData = {
condition: "state";
entity: string;
invert: "true" | "false";
state?: string;
state?: string | string[];
};

@customElement("ha-card-condition-state")
Expand Down Expand Up @@ -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 | string[] | undefined) ??
(this.condition.state as string | string[] | undefined) ??
"",
};

return html`
Expand Down

0 comments on commit 9bafbdd

Please sign in to comment.