diff --git a/src/components/ha-form/ha-form.ts b/src/components/ha-form/ha-form.ts index c824d8fb89..5c82f214df 100644 --- a/src/components/ha-form/ha-form.ts +++ b/src/components/ha-form/ha-form.ts @@ -109,7 +109,7 @@ export class HaForm extends LitElement implements HaFormElement { schema: item, data: getValue(this.data, item), label: this._computeLabel(item, this.data), - disabled: this.disabled, + disabled: this.disabled || item.disabled, hass: this.hass, computeLabel: this.computeLabel, computeHelper: this.computeHelper, diff --git a/src/components/ha-form/types.ts b/src/components/ha-form/types.ts index 5d9572147a..7bafa60895 100644 --- a/src/components/ha-form/types.ts +++ b/src/components/ha-form/types.ts @@ -19,6 +19,7 @@ export interface HaFormBaseSchema { // This value is applied if no data is submitted for this field default?: HaFormData; required?: boolean; + disabled?: boolean; description?: { suffix?: string; // This value will be set initially when form is loaded 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..61387526e6 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,6 +1,8 @@ -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 memoizeOne from "memoize-one"; import { fireEvent } from "../../../../common/dom/fire_event"; import { computeRTLDirection } from "../../../../common/util/compute_rtl"; import "../../../../components/entity/ha-entity-attribute-picker"; @@ -12,11 +14,10 @@ import { WeatherForecastCardConfig } from "../../cards/types"; import "../../components/hui-theme-select-editor"; import { 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 { HaFormSchema } from "../../../../components/ha-form/types"; const cardConfigStruct = assign( baseLovelaceCardConfig, @@ -61,6 +62,29 @@ export class HuiWeatherForecastCardEditor } } + private _schema = memoizeOne( + (entity: string, hasForecast: boolean): HaFormSchema[] => [ + { + name: "entity", + required: true, + selector: { entity: { domain: "weather" } }, + }, + { name: "name", selector: { text: {} } }, + { + type: "grid", + name: "", + schema: [ + { + name: "attribute", + selector: { attribute: { entity_id: entity } }, + }, + { name: "theme", selector: { theme: {} } }, + ], + }, + { name: "Forecast", type: "select", disabled: !hasForecast, options: [] }, + ] + ); + get _entity(): string { return this._config!.entity || ""; } @@ -100,7 +124,21 @@ export class HuiWeatherForecastCardEditor return html``; } + const stateObj = this.hass.states[this._config.entity] as WeatherEntity; + + const data = { ...this._config }; + const schema = this._schema( + stateObj?.state !== UNAVAILABLE && !!stateObj.attributes.forecast?.length + ); + return html` +
config[k] === "" && delete config[k]); + 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}` + ); + }; } declare global {