diff --git a/clockwork-card.js b/clockwork-card.js index 4431b1c..3072711 100644 --- a/clockwork-card.js +++ b/clockwork-card.js @@ -2,124 +2,128 @@ // https://github.com/robmarkoski/ha-clockwork-card class ClockWorkCard extends HTMLElement { - constructor() { - super(); - this.attachShadow({ - mode: 'open' - }); + constructor() { + super(); + this.attachShadow({ + mode: "open", + }); + } + + /* This is called every time sensor is updated */ + set hass(hass) { + const config = this.config; + const locale = config.locale; + const _locale = locale ? locale : undefined; + var _other_timezones = config.other_time; + + const entityId = config.entity; + + // Need to check for safari as safari dates are parsed as being UTC when not specified. + // Therefore all dates are adjusted + //var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); + + if (entityId) { + const state = hass.states[entityId]; + const stateStr = state ? state.state : "Unavailable"; + if (stateStr == "Unavailable") { + throw new Error("Sensor State Unavailable"); + } + // if (isSafari) { + // var _stateStr_utc = new Date(stateStr).toLocaleString(locale, {timeZone: "Etc/UTC"}); + // var _date_time = new Date(_stateStr_utc); + // } else { + // var _date_time = new Date(stateStr); + // } + var _date_time = new Date(stateStr); + } else { + var _date_time = new Date(); } - /* This is called every time sensor is updated */ - set hass(hass) { - - const config = this.config; - const locale = config.locale; - const _locale = locale ? locale : undefined; - var _other_timezones = config.other_time; - - const entityId = config.entity; - - // Need to check for safari as safari dates are parsed as being UTC when not specified. - // Therefore all dates are adjusted - //var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); - - if (entityId) { - const state = hass.states[entityId]; - const stateStr = state ? state.state : "Unavailable"; - if (stateStr == "Unavailable") { - throw new Error("Sensor State Unavailable"); - } - // if (isSafari) { - // var _stateStr_utc = new Date(stateStr).toLocaleString(locale, {timeZone: "Etc/UTC"}); - // var _date_time = new Date(_stateStr_utc); - // } else { - // var _date_time = new Date(stateStr); - // } - var _date_time = new Date(stateStr); - } else { - var _date_time = new Date(); - } - - - - - if (_date_time == "Invalid Date") { - throw new Error("Invalid date. Ensure its a ISO Date") - } - - //Format the Time - var _time = _date_time.toLocaleTimeString(_locale, { - hour: 'numeric', - minute: 'numeric' - }); - - //Format the Date - var _date = _date_time.toLocaleDateString(_locale, { - weekday : 'long', - day : 'numeric', - month : 'long' - }); - - //Build List of Other Timezones - // - var otherclocks = ` + if (_date_time == "Invalid Date") { + throw new Error("Invalid date. Ensure its a ISO Date"); + } + + //Format the Time + var _time = _date_time.toLocaleTimeString(_locale, { + hour: "numeric", + minute: "numeric", + }); + + if (_time == "Invalid Time") throw new Error("_time has a invalid value: "+_date_time.toLocaleTimeString()); + + //Format the Date + var _date = _date_time.toLocaleDateString(_locale, { + weekday: "long", + day: "numeric", + month: "long", + }); + + if (_time == "Invalid Date") throw new Error("_date has a invalid value: "+_date_time.toLocaleTimeString()); + + //Build List of Other Timezones + // + var otherclocks = `