mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-23 09:16:38 +00:00
Fix history chart fetching changes (#7235)
This commit is contained in:
parent
0405adcd16
commit
66633273e2
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
},
|
},
|
||||||
|
@ -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 = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user