mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-01 13:37:47 +00:00
Weather forecast editor to h form
This commit is contained in:
parent
eaf97ee7f5
commit
19a8bb59ad
@ -109,7 +109,7 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
schema: item,
|
schema: item,
|
||||||
data: getValue(this.data, item),
|
data: getValue(this.data, item),
|
||||||
label: this._computeLabel(item, this.data),
|
label: this._computeLabel(item, this.data),
|
||||||
disabled: this.disabled,
|
disabled: this.disabled || item.disabled,
|
||||||
hass: this.hass,
|
hass: this.hass,
|
||||||
computeLabel: this.computeLabel,
|
computeLabel: this.computeLabel,
|
||||||
computeHelper: this.computeHelper,
|
computeHelper: this.computeHelper,
|
||||||
|
@ -19,6 +19,7 @@ export interface HaFormBaseSchema {
|
|||||||
// This value is applied if no data is submitted for this field
|
// This value is applied if no data is submitted for this field
|
||||||
default?: HaFormData;
|
default?: HaFormData;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
|
disabled?: boolean;
|
||||||
description?: {
|
description?: {
|
||||||
suffix?: string;
|
suffix?: string;
|
||||||
// This value will be set initially when form is loaded
|
// This value will be set initially when form is loaded
|
||||||
|
@ -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 { customElement, property, state } from "lit/decorators";
|
||||||
import { assert, boolean, object, optional, string, assign } from "superstruct";
|
import { assert, boolean, object, optional, string, assign } from "superstruct";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
|
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
|
||||||
import "../../../../components/entity/ha-entity-attribute-picker";
|
import "../../../../components/entity/ha-entity-attribute-picker";
|
||||||
@ -12,11 +14,10 @@ import { WeatherForecastCardConfig } from "../../cards/types";
|
|||||||
import "../../components/hui-theme-select-editor";
|
import "../../components/hui-theme-select-editor";
|
||||||
import { LovelaceCardEditor } from "../../types";
|
import { LovelaceCardEditor } from "../../types";
|
||||||
import { actionConfigStruct } from "../structs/action-struct";
|
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 { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { UNAVAILABLE } from "../../../../data/entity";
|
import { UNAVAILABLE } from "../../../../data/entity";
|
||||||
import { WeatherEntity } from "../../../../data/weather";
|
import { WeatherEntity } from "../../../../data/weather";
|
||||||
|
import { HaFormSchema } from "../../../../components/ha-form/types";
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
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 {
|
get _entity(): string {
|
||||||
return this._config!.entity || "";
|
return this._config!.entity || "";
|
||||||
}
|
}
|
||||||
@ -100,7 +124,21 @@ export class HuiWeatherForecastCardEditor
|
|||||||
return html``;
|
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`
|
return html`
|
||||||
|
<ha-form
|
||||||
|
.hass=${this.hass}
|
||||||
|
.data=${data}
|
||||||
|
.schema=${schema}
|
||||||
|
.computeLabel=${this._computeLabelCallback}
|
||||||
|
@value-changed=${this._valueChanged}
|
||||||
|
></ha-form>
|
||||||
<div class="card-config">
|
<div class="card-config">
|
||||||
<ha-entity-picker
|
<ha-entity-picker
|
||||||
.label="${this.hass.localize(
|
.label="${this.hass.localize(
|
||||||
@ -195,46 +233,40 @@ export class HuiWeatherForecastCardEditor
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _valueChanged(ev: EntitiesEditorEvent): void {
|
private _valueChanged(ev: CustomEvent): void {
|
||||||
if (!this._config || !this.hass) {
|
const config = ev.detail.value;
|
||||||
return;
|
|
||||||
}
|
|
||||||
const target = ev.currentTarget! as EditorTarget;
|
|
||||||
|
|
||||||
if (this[`_${target.configValue}`] === target.value) {
|
if (config.forecast === "show_both") {
|
||||||
return;
|
/* delete since true is default */
|
||||||
|
delete config.show_current;
|
||||||
|
delete config.show_forecast;
|
||||||
|
} else if (config.forecast === "show_current") {
|
||||||
|
delete config.show_current;
|
||||||
|
config.show_forecast = false;
|
||||||
|
} else if (config.forecast === "show_forecast") {
|
||||||
|
delete config.show_forecast;
|
||||||
|
config.show_current = false;
|
||||||
}
|
}
|
||||||
if (target.configValue) {
|
|
||||||
if (target.configValue.startsWith("show_")) {
|
delete config.forecast;
|
||||||
this._config = { ...this._config };
|
|
||||||
if (target.configValue === "show_both") {
|
Object.keys(config).forEach((k) => config[k] === "" && delete config[k]);
|
||||||
/* delete since true is default */
|
fireEvent(this, "config-changed", { config });
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user