From 8e7d7c51882b54fe2579a49879fef41024225de7 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Tue, 11 Dec 2018 14:15:18 -0600 Subject: [PATCH] :sparkles: UI Editor for `thermostat` card (#2258) --- .../lovelace/cards/hui-thermostat-card.ts | 13 ++- .../hui-thermostat-card-editor.ts | 106 ++++++++++++++++++ 2 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts diff --git a/src/panels/lovelace/cards/hui-thermostat-card.ts b/src/panels/lovelace/cards/hui-thermostat-card.ts index 91f0cc0766..3afff64e5b 100644 --- a/src/panels/lovelace/cards/hui-thermostat-card.ts +++ b/src/panels/lovelace/cards/hui-thermostat-card.ts @@ -13,7 +13,7 @@ import computeStateName from "../../../common/entity/compute_state_name"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import { HomeAssistant, ClimateEntity } from "../../../types"; import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; -import { LovelaceCard } from "../types"; +import { LovelaceCard, LovelaceCardEditor } from "../types"; import { LovelaceCardConfig } from "../../../data/lovelace"; import "../../../components/ha-card"; @@ -43,7 +43,7 @@ const modeIcons = { idle: "hass:power-sleep", }; -interface Config extends LovelaceCardConfig { +export interface Config extends LovelaceCardConfig { entity: string; theme?: string; name?: string; @@ -55,6 +55,15 @@ function formatTemp(temps: string[]): string { export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { + public static async getConfigElement(): Promise { + await import("../editor/config-elements/hui-thermostat-card-editor"); + return document.createElement("hui-thermostat-card-editor"); + } + + public static getStubConfig(): object { + return { entity: "" }; + } + public hass?: HomeAssistant; private _config?: Config; private _roundSliderStyle?: TemplateResult; diff --git a/src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts new file mode 100644 index 0000000000..f4aee2a515 --- /dev/null +++ b/src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts @@ -0,0 +1,106 @@ +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; +import { struct } from "../../common/structs/struct"; +import "@polymer/paper-input/paper-input"; + +import { EntitiesEditorEvent, EditorTarget } from "../types"; +import { hassLocalizeLitMixin } from "../../../../mixins/lit-localize-mixin"; +import { HomeAssistant } from "../../../../types"; +import { LovelaceCardEditor } from "../../types"; +import { fireEvent } from "../../../../common/dom/fire_event"; +import { Config } from "../../cards/hui-thermostat-card"; +import { configElementStyle } from "./config-elements-style"; + +import "../../components/hui-theme-select-editor"; +import "../../../../components/entity/ha-entity-picker"; + +const cardConfigStruct = struct({ + type: "string", + entity: "string", + name: "string?", + theme: "string?", +}); + +export class HuiThermostatCardEditor extends hassLocalizeLitMixin(LitElement) + implements LovelaceCardEditor { + public hass?: HomeAssistant; + private _config?: Config; + + public setConfig(config: Config): void { + config = cardConfigStruct(config); + this._config = { type: "thermostat", ...config }; + } + + static get properties(): PropertyDeclarations { + return { hass: {}, _config: {} }; + } + + get _entity(): string { + return this._config!.entity || ""; + } + + get _name(): string { + return this._config!.name || ""; + } + + get _theme(): string { + return this._config!.theme || "default"; + } + + protected render(): TemplateResult { + if (!this.hass) { + return html``; + } + + return html` + ${configElementStyle} +
+ +
+ + +
+
+ `; + } + + 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-thermostat-card-editor": HuiThermostatCardEditor; + } +} + +customElements.define("hui-thermostat-card-editor", HuiThermostatCardEditor);