Allow selecting multiple entities for state trigger (#12334)

Co-authored-by: Zack Barett <zackbarett@hey.com>
This commit is contained in:
Franck Nijhof 2022-04-15 21:03:14 +02:00 committed by GitHub
parent 76e1721c58
commit 511368da13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -69,7 +69,7 @@ export interface BaseTrigger {
export interface StateTrigger extends BaseTrigger { export interface StateTrigger extends BaseTrigger {
platform: "state"; platform: "state";
entity_id: string; entity_id: string | string[];
attribute?: string; attribute?: string;
from?: string | number; from?: string | number;
to?: string | string[] | number; to?: string | string[] | number;

View File

@ -1,6 +1,7 @@
import { html, LitElement, PropertyValues } from "lit"; import { html, LitElement, PropertyValues } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { import {
array,
assert, assert,
assign, assign,
literal, literal,
@ -10,6 +11,7 @@ import {
union, union,
} from "superstruct"; } from "superstruct";
import memoizeOne from "memoize-one"; import memoizeOne from "memoize-one";
import { ensureArray } from "../../../../../common/ensure-array";
import { fireEvent } from "../../../../../common/dom/fire_event"; import { fireEvent } from "../../../../../common/dom/fire_event";
import { hasTemplate } from "../../../../../common/string/has-template"; import { hasTemplate } from "../../../../../common/string/has-template";
import { StateTrigger } from "../../../../../data/automation"; import { StateTrigger } from "../../../../../data/automation";
@ -24,7 +26,7 @@ const stateTriggerStruct = assign(
baseTriggerStruct, baseTriggerStruct,
object({ object({
platform: literal("state"), platform: literal("state"),
entity_id: optional(string()), entity_id: optional(union([string(), array(string())])),
attribute: optional(string()), attribute: optional(string()),
from: optional(string()), from: optional(string()),
to: optional(string()), to: optional(string()),
@ -39,11 +41,15 @@ export class HaStateTrigger extends LitElement implements TriggerElement {
@property() public trigger!: StateTrigger; @property() public trigger!: StateTrigger;
public static get defaultConfig() { public static get defaultConfig() {
return { entity_id: "" }; return { entity_id: [] };
} }
private _schema = memoizeOne((entityId) => [ private _schema = memoizeOne((entityId) => [
{ name: "entity_id", required: true, selector: { entity: {} } }, {
name: "entity_id",
required: true,
selector: { entity: { multiple: true } },
},
{ {
name: "attribute", name: "attribute",
selector: { attribute: { entity_id: entityId } }, selector: { attribute: { entity_id: entityId } },
@ -85,7 +91,11 @@ export class HaStateTrigger extends LitElement implements TriggerElement {
protected render() { protected render() {
const trgFor = createDurationData(this.trigger.for); const trgFor = createDurationData(this.trigger.for);
const data = { ...this.trigger, ...{ for: trgFor } }; const data = {
...this.trigger,
entity_id: ensureArray(this.trigger.entity_id),
for: trgFor,
};
const schema = this._schema(this.trigger.entity_id); const schema = this._schema(this.trigger.entity_id);
return html` return html`