diff --git a/src/panels/lovelace/cards/hui-light-card.ts b/src/panels/lovelace/cards/hui-light-card.ts index 4dbdbc40c1..aea91b9825 100644 --- a/src/panels/lovelace/cards/hui-light-card.ts +++ b/src/panels/lovelace/cards/hui-light-card.ts @@ -10,7 +10,7 @@ import { fireEvent } from "../../../common/dom/fire_event"; import { styleMap } from "lit-html/directives/styleMap"; import { HomeAssistant, LightEntity } from "../../../types"; import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; -import { LovelaceCard } from "../types"; +import { LovelaceCard, LovelaceCardEditor } from "../types"; import { LovelaceCardConfig } from "../../../data/lovelace"; import { longPress } from "../common/directives/long-press-directive"; import { hasConfigOrEntityChanged } from "../common/has-changed"; @@ -38,7 +38,7 @@ const lightConfig = { showTooltip: false, }; -interface Config extends LovelaceCardConfig { +export interface Config extends LovelaceCardConfig { entity: string; name?: string; theme?: string; @@ -46,6 +46,14 @@ interface Config extends LovelaceCardConfig { export class HuiLightCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { + public static async getConfigElement(): Promise { + await import("../editor/config-elements/hui-light-card-editor"); + return document.createElement("hui-light-card-editor"); + } + public static getStubConfig(): object { + return {}; + } + public hass?: HomeAssistant; private _config?: Config; private _brightnessTimout?: number; diff --git a/src/panels/lovelace/editor/config-elements/hui-light-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-light-card-editor.ts new file mode 100644 index 0000000000..df798d2224 --- /dev/null +++ b/src/panels/lovelace/editor/config-elements/hui-light-card-editor.ts @@ -0,0 +1,110 @@ +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-light-card"; +import { configElementStyle } from "./config-elements-style"; + +import "../../components/hui-theme-select-editor"; +import "../../components/hui-entity-editor"; + +const cardConfigStruct = struct({ + type: "string", + name: "string?", + entity: "string?", + theme: "string?", +}); + +export class HuiLightCardEditor extends hassLocalizeLitMixin(LitElement) + implements LovelaceCardEditor { + public hass?: HomeAssistant; + private _config?: Config; + + public setConfig(config: Config): void { + config = cardConfigStruct(config); + + this._config = { type: "light", ...config }; + } + + static get properties(): PropertyDeclarations { + return { hass: {}, _config: {}, _configEntities: {} }; + } + + get _name(): string { + return this._config!.name || ""; + } + + get _theme(): string { + return this._config!.theme || "default"; + } + + get _entity(): string { + return this._config!.entity || ""; + } + + 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-light-card-editor": HuiLightCardEditor; + } +} + +customElements.define("hui-light-card-editor", HuiLightCardEditor);