UI Editor for weather-forecast card (#2285)

*  UI Editor for `weather-forecast` card

* Missed name

* Fix typo

* Address review comments

* Remove unused optional configs with no defaults

* cleanup

* Name chunk
This commit is contained in:
Ian Richardson 2018-12-18 07:26:34 -06:00 committed by Paulus Schoutsen
parent 603cf7ba0f
commit 7ced08a899
2 changed files with 118 additions and 0 deletions

View File

@ -2,7 +2,22 @@ import "../../../cards/ha-weather-card";
import LegacyWrapperCard from "./hui-legacy-wrapper-card";
// should be interface when converted to TS
export const Config = {
entity: "",
name: "",
};
class HuiWeatherForecastCard extends LegacyWrapperCard {
static async getConfigElement() {
await import(/* webpackChunkName: "hui-weather-forecast-card-editor" */ "../editor/config-elements/hui-weather-forecast-card-editor");
return document.createElement("hui-weather-forecast-card-editor");
}
static getStubConfig() {
return {};
}
constructor() {
super("ha-weather-card", "weather");
}

View File

@ -0,0 +1,103 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import { TemplateResult } from "lit-html";
import { struct } from "../../common/structs/struct";
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-weather-forecast-card";
import { configElementStyle } from "./config-elements-style";
import "../../../../components/entity/ha-entity-picker";
const cardConfigStruct = struct({
type: "string",
entity: "string?",
name: "string?",
});
export class HuiWeatherForecastCardEditor
extends hassLocalizeLitMixin(LitElement)
implements LovelaceCardEditor {
public hass?: HomeAssistant;
private _config?: Config;
public setConfig(config: Config): void {
config = cardConfigStruct(config);
this._config = config;
}
static get properties(): PropertyDeclarations {
return { hass: {}, _config: {} };
}
get _entity(): string {
return this._config!.entity || "";
}
get _name(): string {
return this._config!.name || "";
}
protected render(): TemplateResult {
if (!this.hass) {
return html``;
}
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"}
domain-filter="weather"
@change="${this._valueChanged}"
allow-custom-entity
></ha-entity-picker>
</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) {
if (target.value === "") {
delete this._config[target.configValue!];
} else {
this._config = {
...this._config,
[target.configValue!]: target.value,
};
}
}
fireEvent(this, "config-changed", { config: this._config });
}
}
declare global {
interface HTMLElementTagNameMap {
"hui-weather-forecast-card-editor": HuiWeatherForecastCardEditor;
}
}
customElements.define(
"hui-weather-forecast-card-editor",
HuiWeatherForecastCardEditor
);