From a538fa45c3f0472bb5fb6677b0b02c50a2ca523e Mon Sep 17 00:00:00 2001 From: Kendell R Date: Wed, 22 Jul 2020 14:51:21 -0700 Subject: [PATCH] Fixes the bug where HA breaks when entity's not defined This fixes #35 and it also fixes #23 --- dual-gauge-card.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dual-gauge-card.js b/dual-gauge-card.js index bbe4b05..d64ddb3 100644 --- a/dual-gauge-card.js +++ b/dual-gauge-card.js @@ -59,6 +59,31 @@ class DualGaugeCard extends HTMLElement { } _update() { + if (this._hass.states[this.config['inner'].entity] == undefined || + this._hass.states[this.config['outer'].entity] == undefined) { + console.warn("Undefined entity"); + if (this.card) { + this.card.remove(); + } + + this.card = document.createElement('ha-card'); + if (this.config.header) { + this.card.header = this.config.header; + } + + const content = document.createElement('p'); + content.style.background = "#e8e87a"; + content.style.padding = "8px"; + content.innerHTML = "Error finding these entities:
- " + + this.config['inner'].entity + + "
- " + this.config['outer'].entity; + this.card.appendChild(content); + + this.appendChild(this.card); + return; + } else if (this.card && this.card.firstElementChild.tagName.toLowerCase() == "p") { + this._createCard(); + } this._updateGauge('inner'); this._updateGauge('outer'); }