From 7e441b5ade793eb87c934df6ff6c4fc6ec44fafa Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 30 Jan 2023 16:07:43 +0100 Subject: [PATCH] Fix entity list not updated when using graph card editor (#15217) * Fix entity list not updated when using graph card editor * Do not use async updated * Fix names type * Better error handling * Feedbacks --- .../chart/state-history-chart-line.ts | 2 +- .../chart/state-history-chart-timeline.ts | 2 +- src/components/chart/state-history-charts.ts | 2 +- src/components/chart/statistics-chart.ts | 2 +- .../lovelace/cards/hui-history-graph-card.ts | 81 +++++++++---------- src/translations/en.json | 3 +- 6 files changed, 44 insertions(+), 48 deletions(-) diff --git a/src/components/chart/state-history-chart-line.ts b/src/components/chart/state-history-chart-line.ts index dc24388d13..c4b735d488 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -22,7 +22,7 @@ class StateHistoryChartLine extends LitElement { @property({ attribute: false }) public data: LineChartEntity[] = []; - @property() public names: boolean | Record = false; + @property() public names?: Record; @property() public unit?: string; diff --git a/src/components/chart/state-history-chart-timeline.ts b/src/components/chart/state-history-chart-timeline.ts index 78b3831ba0..7b5a25e1ae 100644 --- a/src/components/chart/state-history-chart-timeline.ts +++ b/src/components/chart/state-history-chart-timeline.ts @@ -19,7 +19,7 @@ export class StateHistoryChartTimeline extends LitElement { @property() public narrow!: boolean; - @property() public names: boolean | Record = false; + @property() public names?: Record; @property() public unit?: string; diff --git a/src/components/chart/state-history-charts.ts b/src/components/chart/state-history-charts.ts index 4da124dc90..19421ef9b2 100644 --- a/src/components/chart/state-history-charts.ts +++ b/src/components/chart/state-history-charts.ts @@ -45,7 +45,7 @@ class StateHistoryCharts extends LitElement { @property() public narrow!: boolean; - @property({ type: Boolean }) public names = false; + @property() public names?: Record; @property({ type: Boolean, attribute: "virtualize", reflect: true }) public virtualize = false; diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index 2c77389482..9b78e8ab02 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -66,7 +66,7 @@ class StatisticsChart extends LitElement { StatisticsMetaData >; - @property() public names: boolean | Record = false; + @property() public names?: Record; @property() public unit?: string; diff --git a/src/panels/lovelace/cards/hui-history-graph-card.ts b/src/panels/lovelace/cards/hui-history-graph-card.ts index 2e6df7db54..4d145385e3 100644 --- a/src/panels/lovelace/cards/hui-history-graph-card.ts +++ b/src/panels/lovelace/cards/hui-history-graph-card.ts @@ -8,18 +8,17 @@ import { } from "lit"; import { customElement, property, state } from "lit/decorators"; import { classMap } from "lit/directives/class-map"; -import "../../../components/ha-card"; -import "../../../components/chart/state-history-charts"; import { isComponentLoaded } from "../../../common/config/is_component_loaded"; +import "../../../components/chart/state-history-charts"; +import "../../../components/ha-card"; import { + computeHistory, HistoryResult, subscribeHistoryStatesTimeWindow, - computeHistory, } from "../../../data/history"; import { HomeAssistant } from "../../../types"; import { hasConfigOrEntitiesChanged } from "../common/has-changed"; import { processConfigEntities } from "../common/process-config-entities"; -import { EntityConfig } from "../entity-rows/types"; import { LovelaceCard } from "../types"; import { HistoryGraphCardConfig } from "./types"; @@ -41,7 +40,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { @state() private _config?: HistoryGraphCardConfig; - private _configEntities?: EntityConfig[]; + @state() private _error?: { code: string; message: string }; private _names: Record = {}; @@ -49,16 +48,12 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { private _hoursToShow = 24; - private _error?: string; - private _interval?: number; private _subscribed?: Promise<(() => Promise) | void>; public getCardSize(): number { - return this._config?.title - ? 2 - : 0 + 2 * (this._configEntities?.length || 1); + return this._config?.title ? 2 : 0 + 2 * (this._entityIds?.length || 1); } public setConfig(config: HistoryGraphCardConfig): void { @@ -70,11 +65,12 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { throw new Error("You must include at least one entity"); } - this._configEntities = config.entities + const configEntities = config.entities ? processConfigEntities(config.entities) : []; - this._configEntities.forEach((entity) => { + this._entityIds = []; + configEntities.forEach((entity) => { this._entityIds.push(entity.entity); if (entity.name) { this._names[entity.entity] = entity.name; @@ -89,16 +85,16 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { public connectedCallback() { super.connectedCallback(); if (this.hasUpdated) { - this._subscribeHistoryTimeWindow(); + this._subscribeHistory(); } } public disconnectedCallback() { super.disconnectedCallback(); - this._unsubscribeHistoryTimeWindow(); + this._unsubscribeHistory(); } - private _subscribeHistoryTimeWindow() { + private async _subscribeHistory() { if (!isComponentLoaded(this.hass!, "history") || this._subscribed) { return; } @@ -136,17 +132,12 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { this._interval = window.setInterval(() => this._redrawGraph(), 1000 * 60); } - private _unsubscribeHistoryTimeWindow() { - if (!this._subscribed) { - return; - } + private _unsubscribeHistory() { clearInterval(this._interval); - this._subscribed.then((unsubscribe) => { - if (unsubscribe) { - unsubscribe(); - } + if (this._subscribed) { + this._subscribed.then((unsub) => unsub?.()); this._subscribed = undefined; - }); + } } protected shouldUpdate(changedProps: PropertyValues): boolean { @@ -177,12 +168,11 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { if ( changedProps.has("_config") && - (!this._subscribed || - oldConfig?.entities !== this._config.entities || - oldConfig?.hours_to_show !== this._hoursToShow) + (oldConfig?.entities !== this._config.entities || + oldConfig?.hours_to_show !== this._config.hours_to_show) ) { - this._unsubscribeHistoryTimeWindow(); - this._subscribeHistoryTimeWindow(); + this._unsubscribeHistory(); + this._subscribeHistory(); } } @@ -191,10 +181,6 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { return html``; } - if (this._error) { - return html`
${this._error}
`; - } - return html`
- + ${this._error + ? html` +
+ ${this.hass.localize("ui.components.history_charts.error")} : + ${this._error.message || this._error.code} +
+ ` + : html` + + `}
`; diff --git a/src/translations/en.json b/src/translations/en.json index b893a20476..d9cd6215e1 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -488,7 +488,8 @@ "history_charts": { "history_disabled": "History integration disabled", "loading_history": "Loading state history…", - "no_history_found": "No state history found." + "no_history_found": "No state history found.", + "error": "Unable to load history" }, "statistics_charts": { "loading_statistics": "Loading statistics…",