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);