From cf55824899bcab5f957b9eecf38748a9cb0e1f1e Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Tue, 27 Aug 2024 23:43:53 -0700 Subject: [PATCH] Add more-info click to energy table and detail device graph (#21737) * feat: more info energy dashboard * add more info to device details usage * Add some more-info click to energy --------- Co-authored-by: Muka Schultze Co-authored-by: Bram Kragten --- src/components/chart/click_is_touch.ts | 6 ++ .../chart/state-history-chart-line.ts | 8 +-- .../chart/state-history-chart-timeline.ts | 7 +- src/components/chart/statistics-chart.ts | 7 +- .../hui-energy-devices-detail-graph-card.ts | 17 +++++ .../energy/hui-energy-devices-graph-card.ts | 9 ++- .../energy/hui-energy-sources-table-card.ts | 69 +++++++++++++++++-- 7 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 src/components/chart/click_is_touch.ts diff --git a/src/components/chart/click_is_touch.ts b/src/components/chart/click_is_touch.ts new file mode 100644 index 0000000000..a2ccfeb5e7 --- /dev/null +++ b/src/components/chart/click_is_touch.ts @@ -0,0 +1,6 @@ +import type { ChartEvent } from "chart.js"; + +export const clickIsTouch = (event: ChartEvent): boolean => + !(event.native instanceof MouseEvent) || + (event.native instanceof PointerEvent && + event.native.pointerType !== "mouse"); diff --git a/src/components/chart/state-history-chart-line.ts b/src/components/chart/state-history-chart-line.ts index 0923fb0702..1a72d69ac3 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -16,6 +16,7 @@ import { HaChartBase, MIN_TIME_BETWEEN_UPDATES, } from "./ha-chart-base"; +import { clickIsTouch } from "./click_is_touch"; const safeParseFloat = (value) => { const parsed = parseFloat(value); @@ -220,12 +221,7 @@ export class StateHistoryChartLine extends LitElement { // @ts-expect-error locale: numberFormatToLocale(this.hass.locale), onClick: (e: any) => { - if ( - !this.clickForMoreInfo || - !(e.native instanceof MouseEvent) || - (e.native instanceof PointerEvent && - e.native.pointerType !== "mouse") - ) { + if (!this.clickForMoreInfo || clickIsTouch(e)) { return; } diff --git a/src/components/chart/state-history-chart-timeline.ts b/src/components/chart/state-history-chart-timeline.ts index 0129ef5ca8..6d7e6178a6 100644 --- a/src/components/chart/state-history-chart-timeline.ts +++ b/src/components/chart/state-history-chart-timeline.ts @@ -16,6 +16,7 @@ import { } from "./ha-chart-base"; import type { TimeLineData } from "./timeline-chart/const"; import { computeTimelineColor } from "./timeline-chart/timeline-color"; +import { clickIsTouch } from "./click_is_touch"; @customElement("state-history-chart-timeline") export class StateHistoryChartTimeline extends LitElement { @@ -224,11 +225,7 @@ export class StateHistoryChartTimeline extends LitElement { // @ts-expect-error locale: numberFormatToLocale(this.hass.locale), onClick: (e: any) => { - if ( - !this.clickForMoreInfo || - !(e.native instanceof MouseEvent) || - (e.native instanceof PointerEvent && e.native.pointerType !== "mouse") - ) { + if (!this.clickForMoreInfo || clickIsTouch(e)) { return; } diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index 81c34f896b..959d32e36d 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -39,6 +39,7 @@ import type { ChartDatasetExtra, HaChartBase, } from "./ha-chart-base"; +import { clickIsTouch } from "./click_is_touch"; export const supportedStatTypeMap: Record = { mean: "mean", @@ -278,11 +279,7 @@ export class StatisticsChart extends LitElement { // @ts-expect-error locale: numberFormatToLocale(this.hass.locale), onClick: (e: any) => { - if ( - !this.clickForMoreInfo || - !(e.native instanceof MouseEvent) || - (e.native instanceof PointerEvent && e.native.pointerType !== "mouse") - ) { + if (!this.clickForMoreInfo || clickIsTouch(e)) { return; } diff --git a/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts b/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts index 954ccd9d1a..6482315e58 100644 --- a/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts +++ b/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts @@ -30,6 +30,7 @@ import { getStatisticLabel, Statistics, StatisticsMetaData, + isExternalStatistic, } from "../../../../data/recorder"; import { FrontendLocaleData } from "../../../../data/translation"; import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; @@ -38,7 +39,9 @@ import { LovelaceCard } from "../../types"; import { EnergyDevicesDetailGraphCardConfig } from "../types"; import { hasConfigChanged } from "../../common/has-changed"; import { getCommonOptions } from "./common/energy-chart-options"; +import { fireEvent } from "../../../../common/dom/fire_event"; import { storage } from "../../../../common/decorators/storage"; +import { clickIsTouch } from "../../../../components/chart/click_is_touch"; const UNIT = "kWh"; @@ -197,6 +200,20 @@ export class HuiEnergyDevicesDetailGraphCard }, }, }, + onClick: (event, elements, chart) => { + if (clickIsTouch(event)) return; + + const index = elements[0]?.datasetIndex ?? -1; + if (index < 0) return; + + const statisticId = + this._data?.prefs.device_consumption[index]?.stat_consumption; + + if (!statisticId || isExternalStatistic(statisticId)) return; + + fireEvent(this, "hass-more-info", { entityId: statisticId }); + chart?.canvas?.dispatchEvent(new Event("mouseout")); // to hide tooltip + }, }; return options; } diff --git a/src/panels/lovelace/cards/energy/hui-energy-devices-graph-card.ts b/src/panels/lovelace/cards/energy/hui-energy-devices-graph-card.ts index 83d287b8ea..18192156c3 100644 --- a/src/panels/lovelace/cards/energy/hui-energy-devices-graph-card.ts +++ b/src/panels/lovelace/cards/energy/hui-energy-devices-graph-card.ts @@ -31,6 +31,7 @@ import { EnergyData, getEnergyDataCollection } from "../../../../data/energy"; import { calculateStatisticSumGrowth, getStatisticLabel, + isExternalStatistic, } from "../../../../data/recorder"; import { FrontendLocaleData } from "../../../../data/translation"; import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; @@ -38,6 +39,7 @@ import { HomeAssistant } from "../../../../types"; import { LovelaceCard } from "../../types"; import { EnergyDevicesGraphCardConfig } from "../types"; import { hasConfigChanged } from "../../common/has-changed"; +import { clickIsTouch } from "../../../../components/chart/click_is_touch"; @customElement("hui-energy-devices-graph-card") export class HuiEnergyDevicesGraphCard @@ -158,15 +160,18 @@ export class HuiEnergyDevicesGraphCard // @ts-expect-error locale: numberFormatToLocale(this.hass.locale), onClick: (e: any) => { + if (clickIsTouch(e)) return; const chart = e.chart; const canvasPosition = getRelativePosition(e, chart); const index = Math.abs( chart.scales.y.getValueForPixel(canvasPosition.y) ); + // @ts-ignore + const statisticId = this._chartData?.datasets[0]?.data[index]?.y; + if (!statisticId || isExternalStatistic(statisticId)) return; fireEvent(this, "hass-more-info", { - // @ts-ignore - entityId: this._chartData?.datasets[0]?.data[index]?.y, + entityId: statisticId, }); chart.canvas.dispatchEvent(new Event("mouseout")); // to hide tooltip }, diff --git a/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts b/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts index 1a9ffb235e..06e5c29baa 100644 --- a/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts +++ b/src/panels/lovelace/cards/energy/hui-energy-sources-table-card.ts @@ -11,6 +11,7 @@ import { PropertyValues, } from "lit"; import { customElement, property, state } from "lit/decorators"; +import { classMap } from "lit/directives/class-map"; import { styleMap } from "lit/directives/style-map"; import { formatNumber } from "../../../../common/number/format_number"; import { getEnergyColor } from "./common/color"; @@ -25,12 +26,14 @@ import { import { calculateStatisticSumGrowth, getStatisticLabel, + isExternalStatistic, } from "../../../../data/recorder"; import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; import { HomeAssistant } from "../../../../types"; import { LovelaceCard } from "../../types"; import { EnergySourcesTableCardConfig } from "../types"; import { hasConfigChanged } from "../../common/has-changed"; +import { fireEvent } from "../../../../common/dom/fire_event"; const colorPropertyMap = { grid_return: "--energy-grid-return-color", @@ -225,7 +228,13 @@ export class HuiEnergySourcesTableCard 0; totalSolarCompare += compareEnergy; - return html` + return html`
+ return html`
` : ""} - +
+ return html`
+ return html`
+ return html`
+ return html`
`; } + private _handleMoreInfo(ev): void { + const entityId = ev.currentTarget?.entity; + if (entityId && !isExternalStatistic(entityId)) { + fireEvent(this, "hass-more-info", { entityId }); + } + } + static get styles(): CSSResultGroup { return css` ${unsafeCSS(dataTableStyles)} @@ -1127,6 +1179,9 @@ export class HuiEnergySourcesTableCard .mdc-data-table__row:not(.mdc-data-table__row--selected):hover { background-color: rgba(var(--rgb-primary-text-color), 0.04); } + .clickable { + cursor: pointer; + } .total { --mdc-typography-body2-font-weight: 500; }