From 8d2d45ae4edcc36ddbb17b2ec4876f7ea6073bb0 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Fri, 8 Dec 2023 13:43:59 +0100 Subject: [PATCH] Add dropdown style to hvac_modes feature (#18963) --- .../hui-climate-hvac-modes-card-feature.ts | 65 ++++++++++++++++++- src/panels/lovelace/card-features/types.ts | 1 + ...-climate-hvac-modes-card-feature-editor.ts | 41 ++++++++++-- src/translations/en.json | 7 +- 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/src/panels/lovelace/card-features/hui-climate-hvac-modes-card-feature.ts b/src/panels/lovelace/card-features/hui-climate-hvac-modes-card-feature.ts index 5b13039385..4477359e1d 100644 --- a/src/panels/lovelace/card-features/hui-climate-hvac-modes-card-feature.ts +++ b/src/panels/lovelace/card-features/hui-climate-hvac-modes-card-feature.ts @@ -1,11 +1,15 @@ +import { mdiThermostat } from "@mdi/js"; import { HassEntity } from "home-assistant-js-websocket"; import { css, html, LitElement, PropertyValues, TemplateResult } from "lit"; -import { customElement, property, state } from "lit/decorators"; +import { customElement, property, query, state } from "lit/decorators"; import { styleMap } from "lit/directives/style-map"; +import { stopPropagation } from "../../../common/dom/stop_propagation"; import { computeDomain } from "../../../common/entity/compute_domain"; import { stateColorCss } from "../../../common/entity/state_color"; import "../../../components/ha-control-select"; +import "../../../components/ha-control-select-menu"; import type { ControlSelectOption } from "../../../components/ha-control-select"; +import type { HaControlSelectMenu } from "../../../components/ha-control-select-menu"; import { ClimateEntity, compareClimateHvacModes, @@ -35,6 +39,9 @@ class HuiClimateHvacModesCardFeature @state() _currentHvacMode?: HvacMode; + @query("ha-control-select-menu", true) + private _haSelect?: HaControlSelectMenu; + static getStubConfig( _, stateObj?: HassEntity @@ -66,8 +73,23 @@ class HuiClimateHvacModesCardFeature } } + protected updated(changedProps: PropertyValues) { + super.updated(changedProps); + if (this._haSelect && changedProps.has("hass")) { + const oldHass = changedProps.get("hass") as HomeAssistant | undefined; + if ( + this.hass && + this.hass.formatEntityAttributeValue !== + oldHass?.formatEntityAttributeValue + ) { + this._haSelect.layoutOptions(); + } + } + } + private async _valueChanged(ev: CustomEvent) { - const mode = (ev.detail as any).value as HvacMode; + const mode = + (ev.detail as any).value ?? ((ev.target as any).value as HvacMode); if (mode === this.stateObj!.state) return; @@ -111,6 +133,37 @@ class HuiClimateHvacModesCardFeature path: computeHvacModeIcon(mode), })); + if (this._config.style === "dropdown") { + return html` +
+ + + ${options.map( + (option) => html` + + + ${option.label} + + ` + )} + +
+ `; + } + return html`
+ ( + localize: LocalizeFunc, + formatEntityState: FormatEntityStateFunc, + stateObj?: HassEntity + ) => [ + { + name: "style", + selector: { + select: { + multiple: false, + mode: "list", + options: ["dropdown", "icons"].map((mode) => ({ + value: mode, + label: localize( + `ui.panel.lovelace.editor.features.types.climate-preset-modes.style_list.${mode}` + ), + })), + }, + }, + }, { name: "hvac_modes", selector: { @@ -59,12 +79,22 @@ export class HuiClimateHvacModesCardFeatureEditor ? this.hass.states[this.context?.entity_id] : undefined; - const schema = this._schema(this.hass.formatEntityState, stateObj); + const data: ClimateHvacModesCardFeatureConfig = { + style: "icons", + hvac_modes: [], + ...this._config, + }; + + const schema = this._schema( + this.hass.localize, + this.hass.formatEntityState, + stateObj + ); return html` { switch (schema.name) { case "hvac_modes": + case "style": return this.hass!.localize( `ui.panel.lovelace.editor.features.types.climate-hvac-modes.${schema.name}` ); default: - return this.hass!.localize( - `ui.panel.lovelace.editor.card.generic.${schema.name}` - ); + return ""; } }; } diff --git a/src/translations/en.json b/src/translations/en.json index 41ed918e23..af46f7f134 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5280,7 +5280,12 @@ }, "climate-hvac-modes": { "label": "Climate HVAC modes", - "hvac_modes": "HVAC modes" + "hvac_modes": "HVAC modes", + "style": "[%key:ui::panel::lovelace::editor::features::types::climate-preset-modes::style%]", + "style_list": { + "dropdown": "[%key:ui::panel::lovelace::editor::features::types::climate-preset-modes::style_list::dropdown%]", + "icons": "[%key:ui::panel::lovelace::editor::features::types::climate-preset-modes::style_list::icons%]" + } }, "climate-preset-modes": { "label": "Climate preset modes",