From 976fd4b32d3ef669012d842b57531b7b317ad584 Mon Sep 17 00:00:00 2001 From: Zack Barett Date: Thu, 24 Feb 2022 15:49:20 -0600 Subject: [PATCH] weather forecast editor to HA form (#11823) --- .../hui-weather-forecast-card-editor.ts | 290 ++++++++---------- 1 file changed, 124 insertions(+), 166 deletions(-) diff --git a/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts index cb1a53bede..cd6c2ab227 100644 --- a/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-weather-forecast-card-editor.ts @@ -1,22 +1,18 @@ -import { CSSResultGroup, html, LitElement, TemplateResult } from "lit"; +import "../../../../components/ha-form/ha-form"; +import { html, LitElement, TemplateResult } from "lit"; import { customElement, property, state } from "lit/decorators"; import { assert, boolean, object, optional, string, assign } from "superstruct"; +import { memoize } from "@fullcalendar/common"; import { fireEvent } from "../../../../common/dom/fire_event"; -import { computeRTLDirection } from "../../../../common/util/compute_rtl"; -import "../../../../components/entity/ha-entity-attribute-picker"; -import "../../../../components/entity/ha-entity-picker"; -import "../../../../components/ha-formfield"; -import "../../../../components/ha-radio"; -import { HomeAssistant } from "../../../../types"; -import { WeatherForecastCardConfig } from "../../cards/types"; -import "../../components/hui-theme-select-editor"; -import { LovelaceCardEditor } from "../../types"; +import type { HomeAssistant } from "../../../../types"; +import type { WeatherForecastCardConfig } from "../../cards/types"; +import type { LovelaceCardEditor } from "../../types"; import { actionConfigStruct } from "../structs/action-struct"; -import { EditorTarget, EntitiesEditorEvent } from "../types"; -import { configElementStyle } from "./config-elements-style"; import { baseLovelaceCardConfig } from "../structs/base-card-struct"; import { UNAVAILABLE } from "../../../../data/entity"; -import { WeatherEntity } from "../../../../data/weather"; +import type { WeatherEntity } from "../../../../data/weather"; +import type { LocalizeFunc } from "../../../../common/translations/localize"; +import type { HaFormSchema } from "../../../../components/ha-form/types"; const cardConfigStruct = assign( baseLovelaceCardConfig, @@ -33,8 +29,6 @@ const cardConfigStruct = assign( }) ); -const includeDomains = ["weather"]; - @customElement("hui-weather-forecast-card-editor") export class HuiWeatherForecastCardEditor extends LitElement @@ -61,30 +55,6 @@ export class HuiWeatherForecastCardEditor } } - get _entity(): string { - return this._config!.entity || ""; - } - - get _name(): string { - return this._config!.name || ""; - } - - get _theme(): string { - return this._config!.theme || ""; - } - - get _show_current(): boolean { - return this._config!.show_current ?? true; - } - - get _show_forecast(): boolean { - return this._config!.show_forecast ?? this._has_forecast === true; - } - - get _secondary_info_attribute(): string { - return this._config!.secondary_info_attribute || ""; - } - get _has_forecast(): boolean | undefined { if (this.hass && this._config) { const stateObj = this.hass.states[this._config.entity] as WeatherEntity; @@ -95,146 +65,134 @@ export class HuiWeatherForecastCardEditor return undefined; } + private _schema = memoize( + ( + entity: string, + localize: LocalizeFunc, + hasForecast?: boolean + ): HaFormSchema[] => { + const schema: HaFormSchema[] = [ + { + name: "entity", + required: true, + selector: { entity: { domain: "weather" } }, + }, + { name: "name", selector: { text: {} } }, + { + name: "", + type: "grid", + schema: [ + { + name: "secondary_info_attribute", + selector: { attribute: { entity_id: entity } }, + }, + { name: "theme", selector: { theme: {} } }, + ], + }, + ]; + if (hasForecast) { + schema.push({ + name: "forecast", + selector: { + select: { + options: [ + { + value: "show_both", + label: localize( + "ui.panel.lovelace.editor.card.weather-forecast.show_both" + ), + }, + { + value: "show_current", + label: localize( + "ui.panel.lovelace.editor.card.weather-forecast.show_only_current" + ), + }, + { + value: "show_forecast", + label: localize( + "ui.panel.lovelace.editor.card.weather-forecast.show_only_forecast" + ), + }, + ], + }, + }, + }); + } + return schema; + } + ); + protected render(): TemplateResult { if (!this.hass || !this._config) { return html``; } + const schema = this._schema( + this._config.entity, + this.hass.localize, + this._has_forecast + ); + + const data: WeatherForecastCardConfig = { + show_current: true, + show_forecast: this._has_forecast, + ...this._config, + }; + + data.forecast = + data.show_current && data.show_forecast + ? "show_both" + : data.show_current + ? "show_current" + : "show_forecast"; + return html` -
- -
- - - -
-
- - - - - - -
-
+ `; } - private _valueChanged(ev: EntitiesEditorEvent): void { - if (!this._config || !this.hass) { - return; + private _valueChanged(ev: CustomEvent): void { + const config = ev.detail.value; + if (config.forecast === "show_both") { + config.show_current = true; + config.show_forecast = true; + } else if (config.forecast === "show_current") { + config.show_current = true; + config.show_forecast = false; + } else { + config.show_current = false; + config.show_forecast = true; } - const target = ev.currentTarget! as EditorTarget; - if (this[`_${target.configValue}`] === target.value) { - return; - } - if (target.configValue) { - if (target.configValue.startsWith("show_")) { - this._config = { ...this._config }; - if (target.configValue === "show_both") { - /* delete since true is default */ - delete this._config.show_current; - delete this._config.show_forecast; - } else if (target.configValue === "show_current") { - delete this._config.show_current; - this._config.show_forecast = false; - } else if (target.configValue === "show_forecast") { - delete this._config.show_forecast; - this._config.show_current = false; - } - } else if (target.value === "") { - this._config = { ...this._config }; - delete this._config[target.configValue!]; - } else { - this._config = { - ...this._config, - [target.configValue!]: - target.checked !== undefined ? target.checked : target.value, - }; - } - } - fireEvent(this, "config-changed", { config: this._config }); + delete config.forecast; + fireEvent(this, "config-changed", { config }); } - static get styles(): CSSResultGroup { - return configElementStyle; - } + private _computeLabelCallback = (schema: HaFormSchema) => { + 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}` + ) || + this.hass!.localize( + `ui.panel.lovelace.editor.card.weather_forecast.${schema.name}` + ) + ); + }; } declare global {