mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Handle multiple triggers in trigger condition UI (#16983)
This commit is contained in:
parent
33d6ad1b0b
commit
07d37dd89f
@ -4,6 +4,7 @@ import { css, html, LitElement } from "lit";
|
|||||||
import { customElement, property, query } from "lit/decorators";
|
import { customElement, property, query } from "lit/decorators";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import { stopPropagation } from "../../common/dom/stop_propagation";
|
import { stopPropagation } from "../../common/dom/stop_propagation";
|
||||||
|
import { ensureArray } from "../../common/array/ensure-array";
|
||||||
import type { SelectOption, SelectSelector } from "../../data/selector";
|
import type { SelectOption, SelectSelector } from "../../data/selector";
|
||||||
import type { HomeAssistant } from "../../types";
|
import type { HomeAssistant } from "../../types";
|
||||||
import "../ha-checkbox";
|
import "../ha-checkbox";
|
||||||
@ -40,7 +41,7 @@ export class HaSelectSelector extends LitElement {
|
|||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
const options =
|
const options =
|
||||||
this.selector.select?.options.map((option) =>
|
this.selector.select?.options?.map((option) =>
|
||||||
typeof option === "object"
|
typeof option === "object"
|
||||||
? (option as SelectOption)
|
? (option as SelectOption)
|
||||||
: ({ value: option, label: option } as SelectOption)
|
: ({ value: option, label: option } as SelectOption)
|
||||||
@ -77,7 +78,8 @@ export class HaSelectSelector extends LitElement {
|
|||||||
${this._renderHelper()}
|
${this._renderHelper()}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
const value =
|
||||||
|
!this.value || this.value === "" ? [] : ensureArray(this.value);
|
||||||
return html`
|
return html`
|
||||||
<div>
|
<div>
|
||||||
${this.label}
|
${this.label}
|
||||||
@ -85,7 +87,7 @@ export class HaSelectSelector extends LitElement {
|
|||||||
(item: SelectOption) => html`
|
(item: SelectOption) => html`
|
||||||
<ha-formfield .label=${item.label}>
|
<ha-formfield .label=${item.label}>
|
||||||
<ha-checkbox
|
<ha-checkbox
|
||||||
.checked=${this.value?.includes(item.value)}
|
.checked=${value.includes(item.value)}
|
||||||
.value=${item.value}
|
.value=${item.value}
|
||||||
.disabled=${item.disabled || this.disabled}
|
.disabled=${item.disabled || this.disabled}
|
||||||
@change=${this._checkboxChanged}
|
@change=${this._checkboxChanged}
|
||||||
@ -100,7 +102,7 @@ export class HaSelectSelector extends LitElement {
|
|||||||
|
|
||||||
if (this.selector.select?.multiple) {
|
if (this.selector.select?.multiple) {
|
||||||
const value =
|
const value =
|
||||||
!this.value || this.value === "" ? [] : (this.value as string[]);
|
!this.value || this.value === "" ? [] : ensureArray(this.value);
|
||||||
|
|
||||||
const optionItems = options.filter(
|
const optionItems = options.filter(
|
||||||
(option) => !option.disabled && !value?.includes(option.value)
|
(option) => !option.disabled && !value?.includes(option.value)
|
||||||
@ -231,19 +233,19 @@ export class HaSelectSelector extends LitElement {
|
|||||||
const value: string = ev.target.value;
|
const value: string = ev.target.value;
|
||||||
const checked = ev.target.checked;
|
const checked = ev.target.checked;
|
||||||
|
|
||||||
|
const oldValue =
|
||||||
|
!this.value || this.value === "" ? [] : ensureArray(this.value);
|
||||||
|
|
||||||
if (checked) {
|
if (checked) {
|
||||||
if (!this.value) {
|
if (oldValue.includes(value)) {
|
||||||
newValue = [value];
|
|
||||||
} else if (this.value.includes(value)) {
|
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
newValue = [...this.value, value];
|
|
||||||
}
|
}
|
||||||
|
newValue = [...oldValue, value];
|
||||||
} else {
|
} else {
|
||||||
if (!this.value?.includes(value)) {
|
if (!oldValue?.includes(value)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
newValue = (this.value as string[]).filter((v) => v !== value);
|
newValue = oldValue.filter((v) => v !== value);
|
||||||
}
|
}
|
||||||
|
|
||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
@ -252,7 +254,7 @@ export class HaSelectSelector extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async _removeItem(ev) {
|
private async _removeItem(ev) {
|
||||||
const value: string[] = [...(this.value! as string[])];
|
const value: string[] = [...ensureArray(this.value!)];
|
||||||
value.splice(ev.target.idx, 1);
|
value.splice(ev.target.idx, 1);
|
||||||
|
|
||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
@ -277,7 +279,10 @@ export class HaSelectSelector extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newValue !== undefined && this.value?.includes(newValue)) {
|
const currentValue =
|
||||||
|
!this.value || this.value === "" ? [] : ensureArray(this.value);
|
||||||
|
|
||||||
|
if (newValue !== undefined && currentValue.includes(newValue)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,9 +291,6 @@ export class HaSelectSelector extends LitElement {
|
|||||||
this.comboBox.setInputValue("");
|
this.comboBox.setInputValue("");
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
const currentValue =
|
|
||||||
!this.value || this.value === "" ? [] : (this.value as string[]);
|
|
||||||
|
|
||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
value: [...currentValue, newValue],
|
value: [...currentValue, newValue],
|
||||||
});
|
});
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import "@material/mwc-list/mwc-list-item";
|
import "@material/mwc-list/mwc-list-item";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import { html, LitElement } from "lit";
|
import { html, LitElement } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { fireEvent } from "../../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../../common/dom/fire_event";
|
||||||
import { ensureArray } from "../../../../../common/array/ensure-array";
|
import { ensureArray } from "../../../../../common/array/ensure-array";
|
||||||
|
import type { SchemaUnion } from "../../../../../components/ha-form/types";
|
||||||
import "../../../../../components/ha-select";
|
import "../../../../../components/ha-select";
|
||||||
import type {
|
import type {
|
||||||
AutomationConfig,
|
AutomationConfig,
|
||||||
@ -30,6 +32,22 @@ export class HaTriggerCondition extends LitElement {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _schema = memoizeOne(
|
||||||
|
(triggers: Trigger[]) =>
|
||||||
|
[
|
||||||
|
{
|
||||||
|
name: "id",
|
||||||
|
selector: {
|
||||||
|
select: {
|
||||||
|
multiple: true,
|
||||||
|
options: triggers.map((trigger) => trigger.id!),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
] as const
|
||||||
|
);
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
super.connectedCallback();
|
super.connectedCallback();
|
||||||
const details = { callback: (config) => this._automationUpdated(config) };
|
const details = { callback: (config) => this._automationUpdated(config) };
|
||||||
@ -45,30 +63,33 @@ export class HaTriggerCondition extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
const { id } = this.condition;
|
|
||||||
|
|
||||||
if (!this._triggers.length) {
|
if (!this._triggers.length) {
|
||||||
return this.hass.localize(
|
return this.hass.localize(
|
||||||
"ui.panel.config.automation.editor.conditions.type.trigger.no_triggers"
|
"ui.panel.config.automation.editor.conditions.type.trigger.no_triggers"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return html`<ha-select
|
|
||||||
.label=${this.hass.localize(
|
const schema = this._schema(this._triggers);
|
||||||
"ui.panel.config.automation.editor.conditions.type.trigger.id"
|
|
||||||
)}
|
return html`
|
||||||
.value=${id}
|
<ha-form
|
||||||
.disabled=${this.disabled}
|
.schema=${schema}
|
||||||
@selected=${this._triggerPicked}
|
.data=${this.condition}
|
||||||
>
|
.hass=${this.hass}
|
||||||
${this._triggers.map(
|
.disabled=${this.disabled}
|
||||||
(trigger) =>
|
.computeLabel=${this._computeLabelCallback}
|
||||||
html`
|
@value-changed=${this._valueChanged}
|
||||||
<mwc-list-item .value=${trigger.id}> ${trigger.id} </mwc-list-item>
|
></ha-form>
|
||||||
`
|
`;
|
||||||
)}
|
|
||||||
</ha-select>`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _computeLabelCallback = (
|
||||||
|
schema: SchemaUnion<ReturnType<typeof this._schema>>
|
||||||
|
): string =>
|
||||||
|
this.hass.localize(
|
||||||
|
`ui.panel.config.automation.editor.conditions.type.trigger.${schema.name}`
|
||||||
|
);
|
||||||
|
|
||||||
private _automationUpdated(config?: AutomationConfig) {
|
private _automationUpdated(config?: AutomationConfig) {
|
||||||
const seenIds = new Set();
|
const seenIds = new Set();
|
||||||
this._triggers = config?.trigger
|
this._triggers = config?.trigger
|
||||||
@ -78,18 +99,24 @@ export class HaTriggerCondition extends LitElement {
|
|||||||
: [];
|
: [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private _triggerPicked(ev) {
|
private _valueChanged(ev: CustomEvent): void {
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
if (!ev.target.value) {
|
const newValue = ev.detail.value;
|
||||||
return;
|
|
||||||
|
if (typeof newValue.id === "string") {
|
||||||
|
if (!this._triggers.some((trigger) => trigger.id === newValue.id)) {
|
||||||
|
newValue.id = "";
|
||||||
|
}
|
||||||
|
} else if (Array.isArray(newValue.id)) {
|
||||||
|
newValue.id = newValue.id.filter((id) =>
|
||||||
|
this._triggers.some((trigger) => trigger.id === id)
|
||||||
|
);
|
||||||
|
if (!newValue.id.length) {
|
||||||
|
newValue.id = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const newTrigger = ev.target.value;
|
|
||||||
if (this.condition.id === newTrigger) {
|
fireEvent(this, "value-changed", { value: newValue });
|
||||||
return;
|
|
||||||
}
|
|
||||||
fireEvent(this, "value-changed", {
|
|
||||||
value: { ...this.condition, id: newTrigger },
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user