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
This commit is contained in:
Paul Bottein 2023-01-30 16:07:43 +01:00 committed by GitHub
parent 82ed14e705
commit 7e441b5ade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 48 deletions

View File

@ -22,7 +22,7 @@ class StateHistoryChartLine extends LitElement {
@property({ attribute: false }) public data: LineChartEntity[] = []; @property({ attribute: false }) public data: LineChartEntity[] = [];
@property() public names: boolean | Record<string, string> = false; @property() public names?: Record<string, string>;
@property() public unit?: string; @property() public unit?: string;

View File

@ -19,7 +19,7 @@ export class StateHistoryChartTimeline extends LitElement {
@property() public narrow!: boolean; @property() public narrow!: boolean;
@property() public names: boolean | Record<string, string> = false; @property() public names?: Record<string, string>;
@property() public unit?: string; @property() public unit?: string;

View File

@ -45,7 +45,7 @@ class StateHistoryCharts extends LitElement {
@property() public narrow!: boolean; @property() public narrow!: boolean;
@property({ type: Boolean }) public names = false; @property() public names?: Record<string, string>;
@property({ type: Boolean, attribute: "virtualize", reflect: true }) @property({ type: Boolean, attribute: "virtualize", reflect: true })
public virtualize = false; public virtualize = false;

View File

@ -66,7 +66,7 @@ class StatisticsChart extends LitElement {
StatisticsMetaData StatisticsMetaData
>; >;
@property() public names: boolean | Record<string, string> = false; @property() public names?: Record<string, string>;
@property() public unit?: string; @property() public unit?: string;

View File

@ -8,18 +8,17 @@ import {
} from "lit"; } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map"; 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 { isComponentLoaded } from "../../../common/config/is_component_loaded";
import "../../../components/chart/state-history-charts";
import "../../../components/ha-card";
import { import {
computeHistory,
HistoryResult, HistoryResult,
subscribeHistoryStatesTimeWindow, subscribeHistoryStatesTimeWindow,
computeHistory,
} from "../../../data/history"; } from "../../../data/history";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { hasConfigOrEntitiesChanged } from "../common/has-changed"; import { hasConfigOrEntitiesChanged } from "../common/has-changed";
import { processConfigEntities } from "../common/process-config-entities"; import { processConfigEntities } from "../common/process-config-entities";
import { EntityConfig } from "../entity-rows/types";
import { LovelaceCard } from "../types"; import { LovelaceCard } from "../types";
import { HistoryGraphCardConfig } from "./types"; import { HistoryGraphCardConfig } from "./types";
@ -41,7 +40,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
@state() private _config?: HistoryGraphCardConfig; @state() private _config?: HistoryGraphCardConfig;
private _configEntities?: EntityConfig[]; @state() private _error?: { code: string; message: string };
private _names: Record<string, string> = {}; private _names: Record<string, string> = {};
@ -49,16 +48,12 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
private _hoursToShow = 24; private _hoursToShow = 24;
private _error?: string;
private _interval?: number; private _interval?: number;
private _subscribed?: Promise<(() => Promise<void>) | void>; private _subscribed?: Promise<(() => Promise<void>) | void>;
public getCardSize(): number { public getCardSize(): number {
return this._config?.title return this._config?.title ? 2 : 0 + 2 * (this._entityIds?.length || 1);
? 2
: 0 + 2 * (this._configEntities?.length || 1);
} }
public setConfig(config: HistoryGraphCardConfig): void { 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"); throw new Error("You must include at least one entity");
} }
this._configEntities = config.entities const configEntities = config.entities
? processConfigEntities(config.entities) ? processConfigEntities(config.entities)
: []; : [];
this._configEntities.forEach((entity) => { this._entityIds = [];
configEntities.forEach((entity) => {
this._entityIds.push(entity.entity); this._entityIds.push(entity.entity);
if (entity.name) { if (entity.name) {
this._names[entity.entity] = entity.name; this._names[entity.entity] = entity.name;
@ -89,16 +85,16 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
public connectedCallback() { public connectedCallback() {
super.connectedCallback(); super.connectedCallback();
if (this.hasUpdated) { if (this.hasUpdated) {
this._subscribeHistoryTimeWindow(); this._subscribeHistory();
} }
} }
public disconnectedCallback() { public disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
this._unsubscribeHistoryTimeWindow(); this._unsubscribeHistory();
} }
private _subscribeHistoryTimeWindow() { private async _subscribeHistory() {
if (!isComponentLoaded(this.hass!, "history") || this._subscribed) { if (!isComponentLoaded(this.hass!, "history") || this._subscribed) {
return; return;
} }
@ -136,17 +132,12 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
this._interval = window.setInterval(() => this._redrawGraph(), 1000 * 60); this._interval = window.setInterval(() => this._redrawGraph(), 1000 * 60);
} }
private _unsubscribeHistoryTimeWindow() { private _unsubscribeHistory() {
if (!this._subscribed) {
return;
}
clearInterval(this._interval); clearInterval(this._interval);
this._subscribed.then((unsubscribe) => { if (this._subscribed) {
if (unsubscribe) { this._subscribed.then((unsub) => unsub?.());
unsubscribe();
}
this._subscribed = undefined; this._subscribed = undefined;
}); }
} }
protected shouldUpdate(changedProps: PropertyValues): boolean { protected shouldUpdate(changedProps: PropertyValues): boolean {
@ -177,12 +168,11 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
if ( if (
changedProps.has("_config") && changedProps.has("_config") &&
(!this._subscribed || (oldConfig?.entities !== this._config.entities ||
oldConfig?.entities !== this._config.entities || oldConfig?.hours_to_show !== this._config.hours_to_show)
oldConfig?.hours_to_show !== this._hoursToShow)
) { ) {
this._unsubscribeHistoryTimeWindow(); this._unsubscribeHistory();
this._subscribeHistoryTimeWindow(); this._subscribeHistory();
} }
} }
@ -191,10 +181,6 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
return html``; return html``;
} }
if (this._error) {
return html`<div class="errors">${this._error}</div>`;
}
return html` return html`
<ha-card .header=${this._config.title}> <ha-card .header=${this._config.title}>
<div <div
@ -202,16 +188,25 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
"has-header": !!this._config.title, "has-header": !!this._config.title,
})}" })}"
> >
<state-history-charts ${this._error
.hass=${this.hass} ? html`
.isLoadingData=${!this._stateHistory} <div>
.historyData=${this._stateHistory} ${this.hass.localize("ui.components.history_charts.error")} :
.names=${this._names} ${this._error.message || this._error.code}
up-to-now </div>
.showNames=${this._config.show_names !== undefined `
? this._config.show_names : html`
: true} <state-history-charts
></state-history-charts> .hass=${this.hass}
.isLoadingData=${!this._stateHistory}
.historyData=${this._stateHistory}
.names=${this._names}
up-to-now
.showNames=${this._config.show_names !== undefined
? this._config.show_names
: true}
></state-history-charts>
`}
</div> </div>
</ha-card> </ha-card>
`; `;

View File

@ -488,7 +488,8 @@
"history_charts": { "history_charts": {
"history_disabled": "History integration disabled", "history_disabled": "History integration disabled",
"loading_history": "Loading state history…", "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": { "statistics_charts": {
"loading_statistics": "Loading statistics…", "loading_statistics": "Loading statistics…",