UI Editor for sensor card (#2267)

This commit is contained in:
Ian Richardson 2018-12-11 14:39:18 -06:00 committed by Paulus Schoutsen
parent 80dd15306e
commit bd46e3b8e0
2 changed files with 205 additions and 2 deletions

View File

@ -8,7 +8,7 @@ import {
import { TemplateResult } from "lit-html"; import { TemplateResult } from "lit-html";
import "@polymer/paper-spinner/paper-spinner"; import "@polymer/paper-spinner/paper-spinner";
import { LovelaceCard } from "../types"; import { LovelaceCard, LovelaceCardEditor } from "../types";
import { LovelaceCardConfig } from "../../../data/lovelace"; import { LovelaceCardConfig } from "../../../data/lovelace";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { fireEvent } from "../../../common/dom/fire_event"; import { fireEvent } from "../../../common/dom/fire_event";
@ -133,7 +133,7 @@ const coordinates = (
return calcPoints(history, hours, width, detail, min, max); return calcPoints(history, hours, width, detail, min, max);
}; };
interface Config extends LovelaceCardConfig { export interface Config extends LovelaceCardConfig {
entity: string; entity: string;
name?: string; name?: string;
icon?: string; icon?: string;
@ -145,6 +145,15 @@ interface Config extends LovelaceCardConfig {
} }
class HuiSensorCard extends LitElement implements LovelaceCard { class HuiSensorCard extends LitElement implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("../editor/config-elements/hui-sensor-card-editor");
return document.createElement("hui-sensor-card-editor");
}
public static getStubConfig(): object {
return {};
}
public hass?: HomeAssistant; public hass?: HomeAssistant;
private _config?: Config; private _config?: Config;
private _history?: any; private _history?: any;

View File

@ -0,0 +1,194 @@
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 "@polymer/paper-dropdown-menu/paper-dropdown-menu";
import "@polymer/paper-item/paper-item";
import "@polymer/paper-listbox/paper-listbox";
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-sensor-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?",
icon: "string?",
graph: "string?",
unit: "string?",
detail: "number?",
theme: "string?",
hours_to_show: "number?",
});
export class HuiSensorCardEditor extends hassLocalizeLitMixin(LitElement)
implements LovelaceCardEditor {
public hass?: HomeAssistant;
private _config?: Config;
public setConfig(config: Config): void {
config = cardConfigStruct(config);
this._config = { type: "sensor", ...config };
}
static get properties(): PropertyDeclarations {
return { hass: {}, _config: {} };
}
get _entity(): string {
return this._config!.entity || "";
}
get _name(): string {
return this._config!.name || "";
}
get _icon(): string {
return this._config!.icon || "";
}
get _graph(): string {
return this._config!.graph || "line";
}
get _unit(): string {
return this._config!.unit || "";
}
get _detail(): number {
return this._config!.number || 1;
}
get _theme(): string {
return this._config!.theme || "default";
}
get _hours_to_show(): number {
return this._config!.hours_to_show || 24;
}
protected render(): TemplateResult {
if (!this.hass) {
return html``;
}
const graphs = ["line", "none"];
return html`
${configElementStyle}
<div class="card-config">
<div class="side-by-side">
<paper-input
label="Name"
value="${this._name}"
.configValue="${"name"}"
@value-changed="${this._valueChanged}"
></paper-input>
<ha-entity-picker
.hass="${this.hass}"
.value="${this._entity}"
.configValue=${"entity"}
.domainFilter=${"sensor"}
@change="${this._valueChanged}"
allow-custom-entity
></ha-entity-picker>
</div>
<div class="side-by-side">
<paper-input
label="Icon"
value="${this._icon}"
.configValue="${"icon"}"
@value-changed="${this._valueChanged}"
></paper-input>
<paper-dropdown-menu
.label="Graph Type"
.configValue="${"graph"}"
@value-changed="${this._valueChanged}"
>
<paper-listbox
slot="dropdown-content"
.selected="${graphs.indexOf(this._graph)}"
>
${
graphs.map((graph) => {
return html`
<paper-item>${graph}</paper-item>
`;
})
}
</paper-listbox>
</paper-dropdown-menu>
</div>
<div class="side-by-side">
<paper-input
label="Units"
value="${this._unit}"
.configValue="${"unit"}"
@value-changed="${this._valueChanged}"
></paper-input>
<paper-input
label="Graph Detail"
type="number"
value="${this._detail}"
.configValue="${"detail"}"
@value-changed="${this._valueChanged}"
></paper-input>
</div>
<div class="side-by-side">
<hui-theme-select-editor
.hass="${this.hass}"
.value="${this._theme}"
.configValue="${"theme"}"
@theme-changed="${this._valueChanged}"
></hui-theme-select-editor>
<paper-input
label="Hours To Show"
type="number"
value="${this._hours_to_show}"
.configValue="${"hours_to_show"}"
@value-changed="${this._valueChanged}"
></paper-input>
</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) {
let value: any = target.value;
if (target.type === "number") {
value = Number(value);
}
this._config = {
...this._config,
[target.configValue!]: value,
};
}
fireEvent(this, "config-changed", { config: this._config });
}
}
declare global {
interface HTMLElementTagNameMap {
"hui-sensor-card-editor": HuiSensorCardEditor;
}
}
customElements.define("hui-sensor-card-editor", HuiSensorCardEditor);