|
| 1 | +import "@polymer/paper-input/paper-input"; |
| 2 | +import "@polymer/paper-item/paper-item"; |
| 3 | +import "@polymer/paper-item/paper-item-body"; |
| 4 | +import "@polymer/paper-listbox/paper-listbox"; |
| 5 | +import { |
| 6 | + LitElement, |
| 7 | + TemplateResult, |
| 8 | + html, |
| 9 | + css, |
| 10 | + CSSResult, |
| 11 | + customElement, |
| 12 | + property, |
| 13 | +} from "lit-element"; |
| 14 | +import { HomeAssistant } from "../../types"; |
| 15 | +import { fireEvent } from "../../common/dom/fire_event"; |
| 16 | +import { |
| 17 | + DeviceTrigger, |
| 18 | + fetchDeviceTriggers, |
| 19 | + deviceAutomationTriggersEqual, |
| 20 | + localizeDeviceAutomationTrigger, |
| 21 | +} from "../../data/device_automation"; |
| 22 | +import "../../components/ha-paper-dropdown-menu"; |
| 23 | + |
| 24 | +const NO_TRIGGER_KEY = "NO_TRIGGER"; |
| 25 | +const UNKNOWN_TRIGGER_KEY = "UNKNOWN_TRIGGER"; |
| 26 | + |
| 27 | +@customElement("ha-device-trigger-picker") |
| 28 | +class HaDeviceTriggerPicker extends LitElement { |
| 29 | + public hass!: HomeAssistant; |
| 30 | + @property() public label?: string; |
| 31 | + @property() public deviceId?: string; |
| 32 | + @property() public value?: DeviceTrigger; |
| 33 | + @property() private _triggers: DeviceTrigger[] = []; |
| 34 | + |
| 35 | + // Trigger an empty render so we start with a clean DOM. |
| 36 | + // paper-listbox does not like changing things around. |
| 37 | + @property() private _renderEmpty = false; |
| 38 | + |
| 39 | + private get _key() { |
| 40 | + if (!this.value) { |
| 41 | + return NO_TRIGGER_KEY; |
| 42 | + } |
| 43 | + |
| 44 | + const idx = this._triggers.findIndex((trigger) => |
| 45 | + deviceAutomationTriggersEqual(trigger, this.value!) |
| 46 | + ); |
| 47 | + |
| 48 | + if (idx === -1) { |
| 49 | + return UNKNOWN_TRIGGER_KEY; |
| 50 | + } |
| 51 | + |
| 52 | + return `${this._triggers[idx].device_id}_${idx}`; |
| 53 | + } |
| 54 | + |
| 55 | + protected render(): TemplateResult | void { |
| 56 | + if (this._renderEmpty) { |
| 57 | + return html``; |
| 58 | + } |
| 59 | + return html` |
| 60 | + <ha-paper-dropdown-menu |
| 61 | + .label=${this.label} |
| 62 | + .value=${this.value |
| 63 | + ? localizeDeviceAutomationTrigger(this.hass, this.value) |
| 64 | + : ""} |
| 65 | + ?disabled=${this._triggers.length === 0} |
| 66 | + > |
| 67 | + <paper-listbox |
| 68 | + slot="dropdown-content" |
| 69 | + .selected=${this._key} |
| 70 | + attr-for-selected="key" |
| 71 | + @iron-select=${this._triggerChanged} |
| 72 | + > |
| 73 | + <paper-item key=${NO_TRIGGER_KEY} .trigger=${this._noTrigger} hidden> |
| 74 | + No triggers |
| 75 | + </paper-item> |
| 76 | + <paper-item key=${UNKNOWN_TRIGGER_KEY} .trigger=${this.value} hidden> |
| 77 | + Unknown trigger |
| 78 | + </paper-item> |
| 79 | + ${this._triggers.map( |
| 80 | + (trigger, idx) => html` |
| 81 | + <paper-item key=${`${this.deviceId}_${idx}`} .trigger=${trigger}> |
| 82 | + ${localizeDeviceAutomationTrigger(this.hass, trigger)} |
| 83 | + </paper-item> |
| 84 | + ` |
| 85 | + )} |
| 86 | + </paper-listbox> |
| 87 | + </ha-paper-dropdown-menu> |
| 88 | + `; |
| 89 | + } |
| 90 | + |
| 91 | + protected updated(changedProps) { |
| 92 | + super.updated(changedProps); |
| 93 | + |
| 94 | + if (changedProps.has("deviceId")) { |
| 95 | + this._updateDeviceInfo(); |
| 96 | + } |
| 97 | + |
| 98 | + // The value has changed, force the listbox to update |
| 99 | + if (changedProps.has("value") || changedProps.has("_renderEmpty")) { |
| 100 | + const listbox = this.shadowRoot!.querySelector("paper-listbox")!; |
| 101 | + if (listbox) { |
| 102 | + listbox._selectSelected(this._key); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private async _updateDeviceInfo() { |
| 108 | + this._triggers = this.deviceId |
| 109 | + ? await fetchDeviceTriggers(this.hass!, this.deviceId!) |
| 110 | + : // No device, clear the list of triggers |
| 111 | + []; |
| 112 | + |
| 113 | + // If there is no value, or if we have changed the device ID, reset the value. |
| 114 | + if (!this.value || this.value.device_id !== this.deviceId) { |
| 115 | + this._setValue( |
| 116 | + this._triggers.length ? this._triggers[0] : this._noTrigger |
| 117 | + ); |
| 118 | + } |
| 119 | + this._renderEmpty = true; |
| 120 | + await this.updateComplete; |
| 121 | + this._renderEmpty = false; |
| 122 | + } |
| 123 | + |
| 124 | + private get _noTrigger() { |
| 125 | + return { |
| 126 | + device_id: this.deviceId || "", |
| 127 | + platform: "device", |
| 128 | + domain: "", |
| 129 | + entity_id: "", |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + private _triggerChanged(ev) { |
| 134 | + this._setValue(ev.detail.item.trigger); |
| 135 | + } |
| 136 | + |
| 137 | + private _setValue(trigger: DeviceTrigger) { |
| 138 | + if (this.value && deviceAutomationTriggersEqual(trigger, this.value)) { |
| 139 | + return; |
| 140 | + } |
| 141 | + this.value = trigger; |
| 142 | + setTimeout(() => { |
| 143 | + fireEvent(this, "change"); |
| 144 | + }, 0); |
| 145 | + } |
| 146 | + |
| 147 | + static get styles(): CSSResult { |
| 148 | + return css` |
| 149 | + ha-paper-dropdown-menu { |
| 150 | + width: 100%; |
| 151 | + } |
| 152 | + paper-listbox { |
| 153 | + min-width: 200px; |
| 154 | + } |
| 155 | + paper-item { |
| 156 | + cursor: pointer; |
| 157 | + } |
| 158 | + `; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +declare global { |
| 163 | + interface HTMLElementTagNameMap { |
| 164 | + "ha-device-trigger-picker": HaDeviceTriggerPicker; |
| 165 | + } |
| 166 | +} |
0 commit comments