From 13d3e57ff60fca95780e1834b97be700c0a02338 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Thu, 21 Sep 2023 12:07:59 +0200 Subject: [PATCH] Add option to select preset list --- ...limate-preset-modes-tile-feature-editor.ts | 42 ++++++++++++++++--- .../hui-climate-preset-modes-tile-feature.ts | 27 +++++++----- src/panels/lovelace/tile-features/types.ts | 1 + src/translations/en.json | 3 +- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/panels/lovelace/editor/config-elements/hui-climate-preset-modes-tile-feature-editor.ts b/src/panels/lovelace/editor/config-elements/hui-climate-preset-modes-tile-feature-editor.ts index a2fa83907143..6807fcae2722 100644 --- a/src/panels/lovelace/editor/config-elements/hui-climate-preset-modes-tile-feature-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-climate-preset-modes-tile-feature-editor.ts @@ -1,7 +1,9 @@ +import { HassEntity } from "home-assistant-js-websocket"; import { html, LitElement, nothing } from "lit"; import { customElement, property, state } from "lit/decorators"; import memoizeOne from "memoize-one"; import { fireEvent } from "../../../../common/dom/fire_event"; +import { FormatEntityAttributeValueFunc } from "../../../../common/translations/entity-state"; import { LocalizeFunc } from "../../../../common/translations/localize"; import "../../../../components/ha-form/ha-form"; import type { @@ -31,7 +33,11 @@ export class HuiClimatePresetModesTileFeatureEditor } private _schema = memoizeOne( - (localize: LocalizeFunc) => + ( + localize: LocalizeFunc, + formatEntityAttributeValue: FormatEntityAttributeValueFunc, + stateObj?: HassEntity + ) => [ { name: "style", @@ -48,6 +54,24 @@ export class HuiClimatePresetModesTileFeatureEditor }, }, }, + { + name: "preset_modes", + selector: { + select: { + multiple: true, + mode: "list", + options: + stateObj?.attributes.preset_modes?.map((mode) => ({ + value: mode, + label: formatEntityAttributeValue( + stateObj, + "preset_mode", + mode + ), + })) || [], + }, + }, + }, ] as const satisfies readonly HaFormSchema[] ); @@ -56,12 +80,21 @@ export class HuiClimatePresetModesTileFeatureEditor return nothing; } + const stateObj = this.context?.entity_id + ? this.hass.states[this.context?.entity_id] + : undefined; + const data: ClimatePresetModesTileFeatureConfig = { style: "dropdown", + preset_modes: [], ...this._config, }; - const schema = this._schema(this.hass.localize); + const schema = this._schema( + this.hass.localize, + this.hass.formatEntityAttributeValue, + stateObj + ); return html` { switch (schema.name) { case "style": + case "preset_modes": return this.hass!.localize( `ui.panel.lovelace.editor.card.tile.features.types.climate-preset-modes.${schema.name}` ); default: - return this.hass!.localize( - `ui.panel.lovelace.editor.card.generic.${schema.name}` - ); + return ""; } }; } diff --git a/src/panels/lovelace/tile-features/hui-climate-preset-modes-tile-feature.ts b/src/panels/lovelace/tile-features/hui-climate-preset-modes-tile-feature.ts index 485986dcd413..1879b9307377 100644 --- a/src/panels/lovelace/tile-features/hui-climate-preset-modes-tile-feature.ts +++ b/src/panels/lovelace/tile-features/hui-climate-preset-modes-tile-feature.ts @@ -43,9 +43,14 @@ class HuiClimatePresetModeTileFeature @query("ha-control-select-menu", true) private _haSelect?: HaControlSelectMenu; - static getStubConfig(): ClimatePresetModesTileFeatureConfig { + static getStubConfig( + _, + stateObj?: HassEntity + ): ClimatePresetModesTileFeatureConfig { return { type: "climate-preset-modes", + style: "dropdown", + preset_modes: stateObj?.attributes.preset_modes || [], }; } @@ -124,15 +129,17 @@ class HuiClimatePresetModeTileFeature const modes = stateObj.attributes.preset_modes || []; - const options = modes.map((mode) => ({ - value: mode, - label: this.hass!.formatEntityAttributeValue( - this.stateObj!, - "preset_mode", - mode - ), - path: computePresetModeIcon(mode), - })); + const options = modes + .filter((mode) => (this._config!.preset_modes || []).includes(mode)) + .map((mode) => ({ + value: mode, + label: this.hass!.formatEntityAttributeValue( + this.stateObj!, + "preset_mode", + mode + ), + path: computePresetModeIcon(mode), + })); if (this._config.style === "icons") { return html` diff --git a/src/panels/lovelace/tile-features/types.ts b/src/panels/lovelace/tile-features/types.ts index 981cfd1d6d36..893791f235d9 100644 --- a/src/panels/lovelace/tile-features/types.ts +++ b/src/panels/lovelace/tile-features/types.ts @@ -43,6 +43,7 @@ export interface ClimateHvacModesTileFeatureConfig { export interface ClimatePresetModesTileFeatureConfig { type: "climate-preset-modes"; style?: "dropdown" | "icons"; + preset_modes?: string[]; } export interface SelectOptionsTileFeatureConfig { diff --git a/src/translations/en.json b/src/translations/en.json index c285c2686514..1dc047936853 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5043,7 +5043,8 @@ "style_list": { "dropdown": "Dropdown", "icons": "Icons" - } + }, + "preset_modes": "Preset modes" }, "target-temperature": { "label": "Target temperature"