UI Editor for thermostat card (#2258)

This commit is contained in:
Ian Richardson 2018-12-11 14:15:18 -06:00 committed by Paulus Schoutsen
parent 767307ef47
commit 8e7d7c5188
2 changed files with 117 additions and 2 deletions

View File

@ -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<LovelaceCardEditor> {
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;

View File

@ -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}
<div class="card-config">
<paper-input
label="Name"
value="${this._name}"
.configValue="${"name"}"
@value-changed="${this._valueChanged}"
></paper-input>
<div class="side-by-side">
<ha-entity-picker
.hass="${this.hass}"
.value="${this._entity}"
.configValue=${"entity"}
.domainFilter=${"climate"}
@change="${this._valueChanged}"
allow-custom-entity
></ha-entity-picker>
<hui-theme-select-editor
.hass="${this.hass}"
.value="${this._theme}"
.configValue="${"theme"}"
@theme-changed="${this._valueChanged}"
></hui-theme-select-editor>
</div>
</div>
`;
}
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);