From 97da26eba71e4fcc77c8eb2aa06bbe39a6f067e4 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Tue, 11 Dec 2018 14:15:29 -0600 Subject: [PATCH] UI Editor for `alarm-panel` card (#2257) * UI Editor for `alarm-panel` card * Adding of states Can't get the last available state to be recognized as being a selection change * Ability to remove states * Clean up * Clean up * Remove id --- .../lovelace/cards/hui-alarm-panel-card.js | 15 ++ .../hui-alarm-panel-card-editor.ts | 192 ++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 src/panels/lovelace/editor/config-elements/hui-alarm-panel-card-editor.ts diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.js b/src/panels/lovelace/cards/hui-alarm-panel-card.js index 3d1e858f43..6c7b9679c8 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.js +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.js @@ -21,7 +21,22 @@ const Icons = { triggered: "hass:bell-ring", }; +export const Config = { + name: "", + entity: "", + states: "", +}; + class HuiAlarmPanelCard extends LocalizeMixin(EventsMixin(PolymerElement)) { + static async getConfigElement() { + await import("../editor/config-elements/hui-alarm-panel-card-editor"); + return document.createElement("hui-alarm-panel-card-editor"); + } + + static getStubConfig() { + return { states: ["arm_home", "arm_away"] }; + } + static get template() { return html` + `; + } + + private _stateRemoved(ev: EntitiesEditorEvent): void { + if (!this._config || !this._states || !this.hass) { + return; + } + + const target = ev.target! as EditorTarget; + const index = Number(target.value); + if (index > -1) { + const newStates = this._states; + newStates.splice(index, 1); + this._config = { + ...this._config, + states: newStates, + }; + fireEvent(this, "config-changed", { config: this._config }); + } + } + + private _stateAdded(ev: EntitiesEditorEvent): void { + if (!this._config || !this.hass) { + return; + } + const target = ev.target! as EditorTarget; + if (!target.value || this._states.indexOf(target.value) >= 0) { + return; + } + const newStates = this._states; + newStates.push(target.value); + this._config = { + ...this._config, + states: newStates, + }; + target.value = ""; + fireEvent(this, "config-changed", { config: this._config }); + } + + private _valueChanged(ev: EntitiesEditorEvent): void { + if (!this._config || !this.hass) { + return; + } + const target = ev.target! as EditorTarget; + if (this[`_${target.configValue}`] === target.value) { + return; + } + if (target.configValue) { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } + fireEvent(this, "config-changed", { config: this._config }); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-alarm-panel-card-editor": HuiAlarmPanelCardEditor; + } +} + +customElements.define("hui-alarm-panel-card-editor", HuiAlarmPanelCardEditor);