From 9fe8f507ec6607593ce160bb495b464395c5894d Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Wed, 22 May 2024 05:10:22 -0700 Subject: [PATCH] Add configurable actions to Gauge Card (#20833) * Add actions to gauge card * struct support * tap_action in UI * Apply suggestions from code review Co-authored-by: Paul Bottein * typo --------- Co-authored-by: Paul Bottein --- src/panels/lovelace/cards/hui-gauge-card.ts | 34 ++++++++++++++++--- src/panels/lovelace/cards/types.ts | 3 ++ .../config-elements/hui-gauge-card-editor.ts | 27 +++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/panels/lovelace/cards/hui-gauge-card.ts b/src/panels/lovelace/cards/hui-gauge-card.ts index 0204408063..d4e0c2aacc 100644 --- a/src/panels/lovelace/cards/hui-gauge-card.ts +++ b/src/panels/lovelace/cards/hui-gauge-card.ts @@ -8,17 +8,22 @@ import { nothing, } from "lit"; import { customElement, property, state } from "lit/decorators"; +import { ifDefined } from "lit/directives/if-defined"; +import { classMap } from "lit/directives/class-map"; import { styleMap } from "lit/directives/style-map"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; -import { fireEvent } from "../../../common/dom/fire_event"; import { computeStateName } from "../../../common/entity/compute_state_name"; import { isValidEntityId } from "../../../common/entity/valid_entity_id"; import { getNumberFormatOptions } from "../../../common/number/format_number"; import "../../../components/ha-card"; import "../../../components/ha-gauge"; import { UNAVAILABLE } from "../../../data/entity"; +import { ActionHandlerEvent } from "../../../data/lovelace/action_handler"; import type { HomeAssistant } from "../../../types"; +import { actionHandler } from "../common/directives/action-handler-directive"; import { findEntities } from "../common/find-entities"; +import { handleAction } from "../common/handle-action"; +import { hasAction } from "../common/has-action"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import { createEntityNotFoundWarning } from "../components/hui-warning"; import type { LovelaceCard, LovelaceCardEditor } from "../types"; @@ -123,10 +128,26 @@ class HuiGaugeCard extends LitElement implements LovelaceCard { const name = this._config.name ?? computeStateName(stateObj); + const hasAnyAction = + !this._config.tap_action || + hasAction(this._config.tap_action) || + hasAction(this._config.hold_action) || + hasAction(this._config.double_tap_action); + // Use `stateObj.state` as value to keep formatting (e.g trailing zeros) // for consistent value display across gauge, entity, entity-row, etc. return html` - + + TAP_ACTIONS.includes(value.action) + ) + ), + hold_action: optional(actionConfigStruct), + double_tap_action: optional(actionConfigStruct), }) ); @@ -121,6 +133,15 @@ export class HuiGaugeCardEditor }, ] as const) : []), + { + name: "tap_action", + selector: { + ui_action: { + actions: TAP_ACTIONS, + default_action: "more-info", + }, + }, + }, ] as const ); @@ -210,6 +231,12 @@ export class HuiGaugeCardEditor return this.hass!.localize( "ui.panel.lovelace.editor.card.generic.unit" ); + case "tap_action": + return `${this.hass!.localize( + `ui.panel.lovelace.editor.card.generic.${schema.name}` + )} (${this.hass!.localize( + "ui.panel.lovelace.editor.card.config.optional" + )})`; default: // "green" | "yellow" | "red" return this.hass!.localize(