Fix updating history card + only update when entity changed (#6647)

This commit is contained in:
Bram Kragten 2020-08-24 17:03:42 +02:00 committed by GitHub
parent 931068dede
commit e2fd155e1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 34 deletions

View File

@ -19,6 +19,8 @@ import { processConfigEntities } from "../common/process-config-entities";
import { EntityConfig } from "../entity-rows/types";
import { LovelaceCard } from "../types";
import { HistoryGraphCardConfig } from "./types";
import { HistoryResult } from "../../../data/history";
import { hasConfigOrEntitiesChanged } from "../common/has-changed";
@customElement("hui-history-graph-card")
export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
@ -49,7 +51,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
@property({ attribute: false }) public hass?: HomeAssistant;
@internalProperty() private _stateHistory?: any;
@internalProperty() private _stateHistory?: HistoryResult;
@internalProperty() private _config?: HistoryGraphCardConfig;
@ -59,10 +61,10 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
private _cacheConfig?: CacheConfig;
private _interval?: number;
private _fetching = false;
private _date?: Date;
public getCardSize(): number {
return 4;
}
@ -97,9 +99,8 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
};
}
public disconnectedCallback(): void {
super.disconnectedCallback();
this._clearInterval();
protected shouldUpdate(changedProps: PropertyValues): boolean {
return hasConfigOrEntitiesChanged(this, changedProps);
}
protected updated(changedProps: PropertyValues) {
@ -108,21 +109,19 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
return;
}
if (!changedProps.has("_config")) {
if (!changedProps.has("_config") && !changedProps.has("hass")) {
return;
}
const oldConfig = changedProps.get("_config") as HistoryGraphCardConfig;
if (oldConfig !== this._config) {
if (changedProps.has("_config") && oldConfig !== this._config) {
this._getStateHistory();
} else if (
this._cacheConfig.refresh &&
Date.now() - this._date!.getTime() >= this._cacheConfig.refresh * 100
) {
this._getStateHistory();
this._clearInterval();
if (!this._interval && this._cacheConfig.refresh) {
this._interval = window.setInterval(() => {
this._getStateHistory();
}, this._cacheConfig.refresh * 1000);
}
}
}
@ -155,27 +154,23 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
if (this._fetching) {
return;
}
this._date = new Date();
this._fetching = true;
try {
this._stateHistory = await getRecentWithCache(
this.hass!,
this._cacheConfig!.cacheKey,
this._cacheConfig!,
this.hass!.localize,
this.hass!.language
);
this._stateHistory = {
...(await getRecentWithCache(
this.hass!,
this._cacheConfig!.cacheKey,
this._cacheConfig!,
this.hass!.localize,
this.hass!.language
)),
};
} finally {
this._fetching = false;
}
}
private _clearInterval(): void {
if (this._interval) {
window.clearInterval(this._interval);
this._interval = undefined;
}
}
static get styles(): CSSResult {
return css`
.content {

View File

@ -1,11 +1,8 @@
import { PropertyValues } from "lit-element";
import { HomeAssistant } from "../../../types";
import { processConfigEntities } from "./process-config-entities";
// Check if config or Entity changed
export function hasConfigOrEntityChanged(
element: any,
changedProps: PropertyValues
): boolean {
function hasConfigChanged(element: any, changedProps: PropertyValues): boolean {
if (changedProps.has("_config")) {
return true;
}
@ -23,9 +20,41 @@ export function hasConfigOrEntityChanged(
) {
return true;
}
return false;
}
// Check if config or Entity changed
export function hasConfigOrEntityChanged(
element: any,
changedProps: PropertyValues
): boolean {
if (hasConfigChanged(element, changedProps)) {
return true;
}
const oldHass = changedProps.get("hass") as HomeAssistant;
return (
oldHass.states[element._config!.entity] !==
element.hass!.states[element._config!.entity]
);
}
// Check if config or Entities changed
export function hasConfigOrEntitiesChanged(
element: any,
changedProps: PropertyValues
): boolean {
if (hasConfigChanged(element, changedProps)) {
return true;
}
const oldHass = changedProps.get("hass") as HomeAssistant;
const entities = processConfigEntities(element._config!.entities);
return entities.some(
(entity) =>
oldHass.states[entity.entity] !== element.hass!.states[entity.entity]
);
}