Thermostat Editor to HA - Form (#11763)

* Thermostat - Ha Form

* Update hui-thermostat-card-editor.ts

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Zack Barett 2022-02-22 03:56:04 -06:00 committed by GitHub
parent d230541256
commit 73855e6f99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,16 +1,13 @@
import "@polymer/paper-input/paper-input"; import "../../../../components/ha-form/ha-form";
import { CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct"; import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/entity/ha-entity-picker"; import type { HaFormSchema } from "../../../../components/ha-form/types";
import { HomeAssistant } from "../../../../types"; import type { HomeAssistant } from "../../../../types";
import { ThermostatCardConfig } from "../../cards/types"; import type { ThermostatCardConfig } from "../../cards/types";
import "../../components/hui-theme-select-editor"; import type { LovelaceCardEditor } from "../../types";
import { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct"; import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { EditorTarget, EntitiesEditorEvent } from "../types";
import { configElementStyle } from "./config-elements-style";
const cardConfigStruct = assign( const cardConfigStruct = assign(
baseLovelaceCardConfig, baseLovelaceCardConfig,
@ -21,7 +18,17 @@ const cardConfigStruct = assign(
}) })
); );
const includeDomains = ["climate"]; const SCHEMA: HaFormSchema[] = [
{ name: "entity", selector: { entity: { domain: "climate" } } },
{
type: "grid",
name: "",
schema: [
{ name: "name", required: false, selector: { text: {} } },
{ name: "theme", required: false, selector: { theme: {} } },
],
},
];
@customElement("hui-thermostat-card-editor") @customElement("hui-thermostat-card-editor")
export class HuiThermostatCardEditor export class HuiThermostatCardEditor
@ -37,81 +44,39 @@ export class HuiThermostatCardEditor
this._config = config; this._config = config;
} }
get _entity(): string {
return this._config!.entity || "";
}
get _name(): string {
return this._config!.name || "";
}
get _theme(): string {
return this._config!.theme || "";
}
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this.hass || !this._config) { if (!this.hass || !this._config) {
return html``; return html``;
} }
return html` return html`
<div class="card-config"> <ha-form
<ha-entity-picker .hass=${this.hass}
.label="${this.hass.localize( .data=${this._config}
"ui.panel.lovelace.editor.card.generic.entity" .schema=${SCHEMA}
)} (${this.hass.localize( .computeLabel=${this._computeLabelCallback}
"ui.panel.lovelace.editor.card.config.required" @value-changed=${this._valueChanged}
)})" ></ha-form>
.hass=${this.hass}
.value=${this._entity}
.configValue=${"entity"}
.includeDomains=${includeDomains}
@change=${this._valueChanged}
allow-custom-entity
></ha-entity-picker>
<paper-input
.label="${this.hass.localize(
"ui.panel.lovelace.editor.card.generic.name"
)} (${this.hass.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})"
.value=${this._name}
.configValue=${"name"}
@value-changed=${this._valueChanged}
></paper-input>
<hui-theme-select-editor
.hass=${this.hass}
.value=${this._theme}
.configValue=${"theme"}
@value-changed=${this._valueChanged}
></hui-theme-select-editor>
</div>
`; `;
} }
private _valueChanged(ev: EntitiesEditorEvent): void { private _valueChanged(ev: CustomEvent): void {
if (!this._config || !this.hass) { fireEvent(this, "config-changed", { config: ev.detail.value });
return;
}
const target = ev.target! as EditorTarget;
if (this[`_${target.configValue}`] === target.value) {
return;
}
if (target.configValue) {
if (target.value === "") {
this._config = { ...this._config };
delete this._config[target.configValue!];
} else {
this._config = { ...this._config, [target.configValue!]: target.value };
}
}
fireEvent(this, "config-changed", { config: this._config });
} }
static get styles(): CSSResultGroup { private _computeLabelCallback = (schema: HaFormSchema) => {
return configElementStyle; if (schema.name === "entity") {
} return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entity"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.required"
)})`;
}
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
);
};
} }
declare global { declare global {