UI Editor for light card (#2232)

* UI Editor for `light` card

* Remove `id`
This commit is contained in:
Ian Richardson 2018-12-11 14:22:21 -06:00 committed by Paulus Schoutsen
parent db4c1e45f5
commit 8679f10f10
2 changed files with 120 additions and 2 deletions

View File

@ -10,7 +10,7 @@ import { fireEvent } from "../../../common/dom/fire_event";
import { styleMap } from "lit-html/directives/styleMap"; import { styleMap } from "lit-html/directives/styleMap";
import { HomeAssistant, LightEntity } from "../../../types"; import { HomeAssistant, LightEntity } from "../../../types";
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
import { LovelaceCard } from "../types"; import { LovelaceCard, LovelaceCardEditor } from "../types";
import { LovelaceCardConfig } from "../../../data/lovelace"; import { LovelaceCardConfig } from "../../../data/lovelace";
import { longPress } from "../common/directives/long-press-directive"; import { longPress } from "../common/directives/long-press-directive";
import { hasConfigOrEntityChanged } from "../common/has-changed"; import { hasConfigOrEntityChanged } from "../common/has-changed";
@ -38,7 +38,7 @@ const lightConfig = {
showTooltip: false, showTooltip: false,
}; };
interface Config extends LovelaceCardConfig { export interface Config extends LovelaceCardConfig {
entity: string; entity: string;
name?: string; name?: string;
theme?: string; theme?: string;
@ -46,6 +46,14 @@ interface Config extends LovelaceCardConfig {
export class HuiLightCard extends hassLocalizeLitMixin(LitElement) export class HuiLightCard extends hassLocalizeLitMixin(LitElement)
implements LovelaceCard { implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("../editor/config-elements/hui-light-card-editor");
return document.createElement("hui-light-card-editor");
}
public static getStubConfig(): object {
return {};
}
public hass?: HomeAssistant; public hass?: HomeAssistant;
private _config?: Config; private _config?: Config;
private _brightnessTimout?: number; private _brightnessTimout?: number;

View File

@ -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}
<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=${"light"}
@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-light-card-editor": HuiLightCardEditor;
}
}
customElements.define("hui-light-card-editor", HuiLightCardEditor);