From e69f36047da6b168fd5a660c553f15557239c953 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Wed, 27 Jan 2021 03:59:09 -0600 Subject: [PATCH] add actions to weather-forecast-card (#7659) --- .../cards/hui-weather-forecast-card.ts | 31 +++++++++++++------ src/panels/lovelace/cards/types.ts | 3 ++ .../hui-weather-forecast-card-editor.ts | 9 +++++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/panels/lovelace/cards/hui-weather-forecast-card.ts b/src/panels/lovelace/cards/hui-weather-forecast-card.ts index b247f8b073..913812d51f 100644 --- a/src/panels/lovelace/cards/hui-weather-forecast-card.ts +++ b/src/panels/lovelace/cards/hui-weather-forecast-card.ts @@ -9,18 +9,22 @@ import { PropertyValues, TemplateResult, } from "lit-element"; +import { ifDefined } from "lit-html/directives/if-defined"; + +import type { HomeAssistant } from "../../../types"; +import type { LovelaceCard, LovelaceCardEditor } from "../types"; +import type { WeatherForecastCardConfig } from "./types"; + import { formatTime } from "../../../common/datetime/format_time"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; -import { fireEvent } from "../../../common/dom/fire_event"; import { computeStateDisplay } from "../../../common/entity/compute_state_display"; import { computeStateName } from "../../../common/entity/compute_state_name"; import { stateIcon } from "../../../common/entity/state_icon"; import { isValidEntityId } from "../../../common/entity/valid_entity_id"; import { formatNumber } from "../../../common/string/format_number"; import { debounce } from "../../../common/util/debounce"; -import "../../../components/ha-card"; -import "../../../components/ha-icon"; import { UNAVAILABLE } from "../../../data/entity"; +import { ActionHandlerEvent } from "../../../data/lovelace"; import { getSecondaryWeatherAttribute, getWeatherStateIcon, @@ -30,14 +34,16 @@ import { WeatherEntity, weatherSVGStyles, } from "../../../data/weather"; -import type { HomeAssistant } from "../../../types"; import { actionHandler } from "../common/directives/action-handler-directive"; import { findEntities } from "../common/find-entites"; +import { handleAction } from "../common/handle-action"; +import { hasAction } from "../common/has-action"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import { installResizeObserver } from "../common/install-resize-observer"; import { createEntityNotFoundWarning } from "../components/hui-warning"; -import type { LovelaceCard, LovelaceCardEditor } from "../types"; -import type { WeatherForecastCardConfig } from "./types"; + +import "../../../components/ha-card"; +import "../../../components/ha-icon"; const DAY_IN_MILLISECONDS = 86400000; @@ -189,8 +195,13 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard { return html`
@@ -339,8 +350,8 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard { `; } - private _handleAction(): void { - fireEvent(this, "hass-more-info", { entityId: this._config!.entity }); + private _handleAction(ev: ActionHandlerEvent) { + handleAction(this, this.hass!, this._config!, ev.detail.action!); } private async _attachObserver(): Promise { diff --git a/src/panels/lovelace/cards/types.ts b/src/panels/lovelace/cards/types.ts index bf0fcfbb10..05330f9dbf 100644 --- a/src/panels/lovelace/cards/types.ts +++ b/src/panels/lovelace/cards/types.ts @@ -315,4 +315,7 @@ export interface WeatherForecastCardConfig extends LovelaceCardConfig { show_forecast?: boolean; secondary_info_attribute?: string; theme?: string; + tap_action?: ActionConfig; + hold_action?: ActionConfig; + double_tap_action?: ActionConfig; } 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 55d8a97c17..ec4a99a3f5 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 @@ -18,7 +18,11 @@ import { HomeAssistant } from "../../../../types"; import { WeatherForecastCardConfig } from "../../cards/types"; import "../../components/hui-theme-select-editor"; import { LovelaceCardEditor } from "../../types"; -import { EditorTarget, EntitiesEditorEvent } from "../types"; +import { + actionConfigStruct, + EditorTarget, + EntitiesEditorEvent, +} from "../types"; import { configElementStyle } from "./config-elements-style"; const cardConfigStruct = object({ @@ -28,6 +32,9 @@ const cardConfigStruct = object({ theme: optional(string()), show_forecast: optional(boolean()), secondary_info_attribute: optional(string()), + tap_action: optional(actionConfigStruct), + hold_action: optional(actionConfigStruct), + double_tap_action: optional(actionConfigStruct), }); const includeDomains = ["weather"];