Fix history chart fetching changes (#7235)

This commit is contained in:
Bram Kragten 2020-10-08 16:41:25 +02:00 committed by GitHub
parent 0405adcd16
commit 66633273e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 13 deletions

View File

@ -10,7 +10,6 @@ import {
} from "./history"; } from "./history";
export interface CacheConfig { export interface CacheConfig {
refresh: number;
cacheKey: string; cacheKey: string;
hoursToShow: number; hoursToShow: number;
} }

View File

@ -78,7 +78,6 @@ export class MoreInfoHistory extends LitElement {
this.hass!, this.hass!,
this.entityId, this.entityId,
{ {
refresh: 60,
cacheKey: `more_info.${this.entityId}`, cacheKey: `more_info.${this.entityId}`,
hoursToShow: 24, hoursToShow: 24,
}, },

View File

@ -21,6 +21,7 @@ import { LovelaceCard } from "../types";
import { HistoryGraphCardConfig } from "./types"; import { HistoryGraphCardConfig } from "./types";
import { HistoryResult } from "../../../data/history"; import { HistoryResult } from "../../../data/history";
import { hasConfigOrEntitiesChanged } from "../common/has-changed"; import { hasConfigOrEntitiesChanged } from "../common/has-changed";
import { throttle } from "../../../common/util/throttle";
@customElement("hui-history-graph-card") @customElement("hui-history-graph-card")
export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
@ -63,7 +64,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
private _fetching = false; private _fetching = false;
private _date?: Date; private _throttleGetStateHistory?: () => void;
public getCardSize(): number { public getCardSize(): number {
return 4; return 4;
@ -92,10 +93,13 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
} }
}); });
this._throttleGetStateHistory = throttle(() => {
this._getStateHistory();
}, config.refresh_interval || 10 * 1000);
this._cacheConfig = { this._cacheConfig = {
cacheKey: _entities.join(), cacheKey: _entities.join(),
hoursToShow: config.hours_to_show || 24, hoursToShow: config.hours_to_show || 24,
refresh: config.refresh_interval || 0,
}; };
} }
@ -105,7 +109,12 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
protected updated(changedProps: PropertyValues) { protected updated(changedProps: PropertyValues) {
super.updated(changedProps); super.updated(changedProps);
if (!this._config || !this.hass || !this._cacheConfig) { if (
!this._config ||
!this.hass ||
!this._throttleGetStateHistory ||
!this._cacheConfig
) {
return; return;
} }
@ -113,15 +122,19 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
return; return;
} }
const oldConfig = changedProps.get("_config") as HistoryGraphCardConfig; const oldConfig = changedProps.get("_config") as
| HistoryGraphCardConfig
| undefined;
if (changedProps.has("_config") && oldConfig !== this._config) { if (
this._getStateHistory(); changedProps.has("_config") &&
} else if ( (oldConfig?.entities !== this._config.entities ||
this._cacheConfig.refresh && oldConfig?.hours_to_show !== this._config.hours_to_show)
Date.now() - this._date!.getTime() >= this._cacheConfig.refresh * 100
) { ) {
this._getStateHistory(); this._throttleGetStateHistory();
} else if (changedProps.has("hass")) {
// wait for commit of data (we only account for the default setting of 1 sec)
setTimeout(this._throttleGetStateHistory, 1000);
} }
} }
@ -154,7 +167,6 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
if (this._fetching) { if (this._fetching) {
return; return;
} }
this._date = new Date();
this._fetching = true; this._fetching = true;
try { try {
this._stateHistory = { this._stateHistory = {