import "@polymer/paper-icon-button/paper-icon-button"; import "@polymer/paper-item/paper-item"; import "@polymer/paper-listbox/paper-listbox"; // tslint:disable-next-line import { PaperListboxElement } from "@polymer/paper-listbox/paper-listbox"; import "@polymer/paper-menu-button/paper-menu-button"; import { css, CSSResult, customElement, html, LitElement, property, } from "lit-element"; import { dynamicElement } from "../../../../common/dom/dynamic-element-directive"; import { fireEvent } from "../../../../common/dom/fire_event"; import "../../../../components/ha-card"; import { HomeAssistant } from "../../../../types"; import "./types/ha-automation-trigger-device"; import "./types/ha-automation-trigger-event"; import "./types/ha-automation-trigger-state"; import "./types/ha-automation-trigger-geo_location"; import "./types/ha-automation-trigger-homeassistant"; import "./types/ha-automation-trigger-mqtt"; import "./types/ha-automation-trigger-numeric_state"; import "./types/ha-automation-trigger-sun"; import "./types/ha-automation-trigger-template"; import "./types/ha-automation-trigger-time"; import "./types/ha-automation-trigger-time_pattern"; import "./types/ha-automation-trigger-webhook"; import "./types/ha-automation-trigger-zone"; import { Trigger } from "../../../../data/automation"; const OPTIONS = [ "device", "event", "state", "geo_location", "homeassistant", "mqtt", "numeric_state", "sun", "template", "time", "time_pattern", "webhook", "zone", ]; export interface TriggerElement extends LitElement { trigger: Trigger; } export const handleChangeEvent = (element: TriggerElement, ev: CustomEvent) => { ev.stopPropagation(); const name = (ev.target as any)?.name; if (!name) { return; } const newVal = ev.detail.value; if ((element.trigger[name] || "") === newVal) { return; } let newTrigger: Trigger; if (!newVal) { newTrigger = { ...element.trigger }; delete newTrigger[name]; } else { newTrigger = { ...element.trigger, [name]: newVal }; } fireEvent(element, "value-changed", { value: newTrigger }); }; @customElement("ha-automation-trigger-row") export default class HaAutomationTriggerRow extends LitElement { @property() public hass!: HomeAssistant; @property() public trigger!: Trigger; @property() private _yamlMode = false; protected render() { const selected = OPTIONS.indexOf(this.trigger.platform); const yamlMode = this._yamlMode || selected === -1; return html`
${yamlMode ? this.hass.localize( "ui.panel.config.automation.editor.edit_ui" ) : this.hass.localize( "ui.panel.config.automation.editor.edit_yaml" )} ${this.hass.localize( "ui.panel.config.automation.editor.triggers.duplicate" )} ${this.hass.localize( "ui.panel.config.automation.editor.triggers.delete" )}
${yamlMode ? html`
${selected === -1 ? html` ${this.hass.localize( "ui.panel.config.automation.editor.triggers.unsupported_platform", "platform", this.trigger.platform )} ` : ""}
` : html` ${OPTIONS.map( (opt) => html` ${this.hass.localize( `ui.panel.config.automation.editor.triggers.type.${opt}.label` )} ` )}
${dynamicElement( `ha-automation-trigger-${this.trigger.platform}`, { hass: this.hass, trigger: this.trigger } )}
`}
`; } private _onDelete() { if ( confirm( this.hass.localize( "ui.panel.config.automation.editor.triggers.delete_confirm" ) ) ) { fireEvent(this, "value-changed", { value: null }); } } private _typeChanged(ev: CustomEvent) { const type = ((ev.target as PaperListboxElement)?.selectedItem as any) ?.platform; if (!type) { return; } const elClass = customElements.get(`ha-automation-trigger-${type}`); if (type !== this.trigger.platform) { fireEvent(this, "value-changed", { value: { platform: type, ...elClass.defaultConfig, }, }); } } private _onYamlChange(ev: CustomEvent) { ev.stopPropagation(); fireEvent(this, "value-changed", { value: ev.detail.value }); } private _switchYamlMode() { this._yamlMode = !this._yamlMode; } static get styles(): CSSResult { return css` .card-menu { position: absolute; top: 0; right: 0; z-index: 3; color: var(--primary-text-color); } .rtl .card-menu { right: auto; left: 0; } .card-menu paper-item { cursor: pointer; } `; } } declare global { interface HTMLElementTagNameMap { "ha-automation-trigger-row": HaAutomationTriggerRow; } }