From c8d3293ae99ddf9f1bc8cb1b8037621082cd2025 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Fri, 6 Sep 2019 04:48:39 -0500 Subject: [PATCH] Throttle updates for entity-filter (#3551) * Throttle updates for entity-filter * throttle element config sets * apply changes * apply review changes --- .../lovelace/cards/hui-entity-filter-card.ts | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/panels/lovelace/cards/hui-entity-filter-card.ts b/src/panels/lovelace/cards/hui-entity-filter-card.ts index 15b8636224..a225065dd4 100644 --- a/src/panels/lovelace/cards/hui-entity-filter-card.ts +++ b/src/panels/lovelace/cards/hui-entity-filter-card.ts @@ -12,6 +12,8 @@ class EntityFilterCard extends HTMLElement implements LovelaceCard { private _config?: EntityFilterCardConfig; private _configEntities?: EntityConfig[]; private _baseCardConfig?: LovelaceCardConfig; + private _hass?: HomeAssistant; + private _oldEntities?: EntityConfig[]; public getCardSize(): number { return this._element ? this._element.getCardSize() : 1; @@ -41,6 +43,13 @@ class EntityFilterCard extends HTMLElement implements LovelaceCard { return; } + if (!this.haveEntitiesChanged(hass)) { + this._hass = hass; + return; + } + + this._hass = hass; + if (!this._configEntities) { this._configEntities = processConfigEntities(this._config.entities); } @@ -62,7 +71,16 @@ class EntityFilterCard extends HTMLElement implements LovelaceCard { } if (element.tagName !== "HUI-ERROR-CARD") { - element.setConfig({ ...this._baseCardConfig!, entities: entitiesList }); + const isSame = + this._oldEntities && + entitiesList.length === this._oldEntities.length && + entitiesList.every((entity, idx) => entity === this._oldEntities![idx]); + + if (!isSame) { + this._oldEntities = entitiesList; + element.setConfig({ ...this._baseCardConfig!, entities: entitiesList }); + } + element.isPanel = this.isPanel; element.hass = hass; } @@ -75,6 +93,27 @@ class EntityFilterCard extends HTMLElement implements LovelaceCard { this.style.display = "block"; } + private haveEntitiesChanged(hass: HomeAssistant): boolean { + if (!this._hass) { + return true; + } + + if (!this._configEntities) { + return true; + } + + for (const config of this._configEntities) { + if ( + this._hass.states[config.entity] !== hass.states[config.entity] || + this._hass.localize !== hass.localize + ) { + return true; + } + } + + return false; + } + private _cardElement(): LovelaceCard | undefined { if (!this._element && this._config) { const element = createCardElement(this._baseCardConfig!);