From 5233111cb679656c92189100aa5e8d147e7ca5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isabella=20Gross=20Alstr=C3=B6m?= Date: Fri, 14 Aug 2020 12:07:36 +0200 Subject: [PATCH] fix for wrong entity in config --- krisinfo-card.js | 267 +++++++++++++++++++++++++---------------------- 1 file changed, 140 insertions(+), 127 deletions(-) diff --git a/krisinfo-card.js b/krisinfo-card.js index d130cf6..7c77af4 100644 --- a/krisinfo-card.js +++ b/krisinfo-card.js @@ -1,142 +1,155 @@ customElements.whenDefined('card-tools').then(() => { -let cardTools = customElements.get('card-tools'); + let cardTools = customElements.get('card-tools'); + + class KrisinfoCard extends cardTools.LitElement { + + setConfig(config) { -class KrisinfoCard extends cardTools.LitElement { - - setConfig(config) { - if (!config.entity) { - throw new Error('Please define entity'); + if (!config.entity) { + throw new Error('Please define entity'); + } + this.config = config; } - this.config = config; - } - - render(){ - return cardTools.LitHtml - ` - ${this._renderStyle()} - ${this.config.only_alerts == false || this.config.only_alerts == null - ? cardTools.LitHtml - ` -
-
- ${this.header} -
-
- ${this.messages.length == 0 - ? cardTools.LitHtml`

Inga meddelanden just nu

` - : this.messages.map(message => - cardTools.LitHtml`
- ${message.SenderName} -
Skickat: ${message.Published} -

${message.Event == "Alert" ? cardTools.LitHtml`` : ''}${message.Headline}

-

${message.Message}

- Läs mer -
- `)} -
` - : // if only alerts - cardTools.LitHtml + + render(){ + if (!this.entity) + return html` + + ${this._hass.localize("ui.panel.lovelace.warning.entity_not_found", + "entity", + this.config.entity + )} + ` - ${this.state == "Alert" ? + return cardTools.LitHtml + ` + ${this._renderStyle()} + ${this.config.only_alerts == false || this.config.only_alerts == null + ? cardTools.LitHtml + ` +
+
+ ${this.header} +
+
+ ${this.messages.length == 0 + ? cardTools.LitHtml`

Inga meddelanden just nu

` + : this.messages.map(message => + cardTools.LitHtml`
+ ${message.SenderName} +
Skickat: ${message.Published} +

${message.Event == "Alert" ? cardTools.LitHtml`` : ''}${message.Headline}

+

${message.Message}

+ Läs mer +
+ `)} +
` + : // if only alerts cardTools.LitHtml ` - -
-
- ${this.state} - ${this.header} + ${this.state == "Alert" ? + cardTools.LitHtml + ` + +
+
+ ${this.state} - ${this.header} +
-
- ${this.messages.length == 0 - ? cardTools.LitHtml`

Inga meddelanden just nu

` - : this.messages.map(message => - cardTools.LitHtml` - ${message.Event == "Alert" ? - cardTools.LitHtml`
- ${message.SenderName} -
Skickat: ${message.Published} -

${message.Event == "Alert" ? cardTools.LitHtml`` : ''}${message.Headline}

-

${message.Message}

- Läs mer -
- ` - : ''}` - ) - - } - + ${this.messages.length == 0 + ? cardTools.LitHtml`

Inga meddelanden just nu

` + : this.messages.map(message => + cardTools.LitHtml` + ${message.Event == "Alert" ? + cardTools.LitHtml`
+ ${message.SenderName} +
Skickat: ${message.Published} +

${message.Event == "Alert" ? cardTools.LitHtml`` : ''}${message.Headline}

+

${message.Message}

+ Läs mer +
+ ` + : ''}` + ) + + } + + ` + : '' + } ` - : '' } + `; + } + + _renderStyle() { + return cardTools.LitHtml ` + + `; } - `; - } - - _renderStyle() { - return cardTools.LitHtml - ` - - `; + + set hass(hass) { + this._hass = hass; + this.entity = this.config.entity in hass.states ? hass.states[this.config.entity] : null; + if(this.entity) + { + this.header = this.entity.attributes.friendly_name; + this.messages = this.entity.attributes.messages; + this.state = this.entity.state + } + + this.requestUpdate(); } - set hass(hass) { - this._hass = hass; - - const entity = hass.states[this.config.entity]; - this.header = entity.attributes.friendly_name; - this.messages = entity.attributes.messages; - this.state = entity.state - this.requestUpdate(); - } - - - - // @TODO: This requires more intelligent logic - getCardSize() { - return 3; + + + // @TODO: This requires more intelligent logic + getCardSize() { + return 3; + } } -} - -customElements.define('krisinfo-card', KrisinfoCard); -}); - -window.setTimeout(() => { - if(customElements.get('card-tools')) return; - customElements.define('krisinfo-card', class extends HTMLElement{ - setConfig() { throw new Error("Can't find card-tools. See https://github.com/thomasloven/lovelace-card-tools");} + + customElements.define('krisinfo-card', KrisinfoCard); }); -}, 2000); \ No newline at end of file + + window.setTimeout(() => { + if(customElements.get('card-tools')) return; + customElements.define('krisinfo-card', class extends HTMLElement{ + setConfig() { throw new Error("Can't find card-tools. See https://github.com/thomasloven/lovelace-card-tools");} + }); + }, 2000); \ No newline at end of file