From 4c3d6b8257fd930fed3d7e51afad66ad04cf254d Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 1 Jan 2023 02:42:33 +0000 Subject: [PATCH] Add checking for entity_sun. --- package.json | 2 +- src/const.ts | 2 +- src/platinum-weather-card.ts | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index ddd1618..e61661e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "platinum-weather-card", - "version": "1.0.4", + "version": "1.0.5", "description": "platinum-weather-card", "keywords": [ "home-assistant", diff --git a/src/const.ts b/src/const.ts index 905c7f7..b056562 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1 +1 @@ -export const CARD_VERSION = '1.0.4'; +export const CARD_VERSION = '1.0.5'; diff --git a/src/platinum-weather-card.ts b/src/platinum-weather-card.ts index 8e79942..f7d643e 100644 --- a/src/platinum-weather-card.ts +++ b/src/platinum-weather-card.ts @@ -1745,22 +1745,22 @@ export class PlatinumWeatherCard extends LitElement { var nextSunRise: string; switch (this.timeFormat) { case '12hour': - nextSunSet = this._config.entity_sun ? new Date(this.hass.states[this._config.entity_sun].attributes.next_setting).toLocaleTimeString(this.locale, { hour: 'numeric', minute: '2-digit', hour12: true }).replace(" am", "am").replace(" pm", "pm") : ""; - nextSunRise = this._config.entity_sun ? new Date(this.hass.states[this._config.entity_sun].attributes.next_rising).toLocaleTimeString(this.locale, { hour: 'numeric', minute: '2-digit', hour12: true }).replace(" am", "am").replace(" pm", "pm") : ""; + nextSunSet = this._config.entity_sun && (this.hass.states[this._config.entity_sun] !== undefined) ? new Date(this.hass.states[this._config.entity_sun].attributes.next_setting).toLocaleTimeString(this.locale, { hour: 'numeric', minute: '2-digit', hour12: true }).replace(" am", "am").replace(" pm", "pm") : ""; + nextSunRise = this._config.entity_sun && (this.hass.states[this._config.entity_sun] !== undefined) ? new Date(this.hass.states[this._config.entity_sun].attributes.next_rising).toLocaleTimeString(this.locale, { hour: 'numeric', minute: '2-digit', hour12: true }).replace(" am", "am").replace(" pm", "pm") : ""; break; case '24hour': - nextSunSet = this._config.entity_sun ? new Date(this.hass.states[this._config.entity_sun].attributes.next_setting).toLocaleTimeString(this.locale, { hour: '2-digit', minute: '2-digit', hour12: false }) : ""; - nextSunRise = this._config.entity_sun ? new Date(this.hass.states[this._config.entity_sun].attributes.next_rising).toLocaleTimeString(this.locale, { hour: '2-digit', minute: '2-digit', hour12: false }) : ""; + nextSunSet = this._config.entity_sun && (this.hass.states[this._config.entity_sun] !== undefined) ? new Date(this.hass.states[this._config.entity_sun].attributes.next_setting).toLocaleTimeString(this.locale, { hour: '2-digit', minute: '2-digit', hour12: false }) : ""; + nextSunRise = this._config.entity_sun && (this.hass.states[this._config.entity_sun] !== undefined) ? new Date(this.hass.states[this._config.entity_sun].attributes.next_rising).toLocaleTimeString(this.locale, { hour: '2-digit', minute: '2-digit', hour12: false }) : ""; break; case 'system': - nextSunSet = this._config.entity_sun ? new Date(this.hass.states[this._config.entity_sun].attributes.next_setting).toLocaleTimeString(navigator.language, { timeStyle: 'short' }).replace(" am", "am").replace(" pm", "pm") : ""; - nextSunRise = this._config.entity_sun ? new Date(this.hass.states[this._config.entity_sun].attributes.next_rising).toLocaleTimeString(navigator.language, { timeStyle: 'short' }).replace(" am", "am").replace(" pm", "pm") : ""; + nextSunSet = this._config.entity_sun && (this.hass.states[this._config.entity_sun] !== undefined) ? new Date(this.hass.states[this._config.entity_sun].attributes.next_setting).toLocaleTimeString(navigator.language, { timeStyle: 'short' }).replace(" am", "am").replace(" pm", "pm") : ""; + nextSunRise = this._config.entity_sun && (this.hass.states[this._config.entity_sun] !== undefined) ? new Date(this.hass.states[this._config.entity_sun].attributes.next_rising).toLocaleTimeString(navigator.language, { timeStyle: 'short' }).replace(" am", "am").replace(" pm", "pm") : ""; break; } var nextDate = new Date(); nextDate.setDate(nextDate.getDate() + 1); - if (this._config.entity_sun) { - if (this.hass.states[this._config.entity_sun].state == "above_horizon") { + if ((this._config.entity_sun) && (this.hass.states[this._config.entity_sun] !== undefined)) { + if (this.hass.states[this._config.entity_sun].state === "above_horizon") { nextSunRise = nextDate.toLocaleDateString(this.locale, { weekday: 'short' }) + " " + nextSunRise; return { 'next': html`