Skip to content

Commit

Permalink
Fix incorrect handling of datetime string values (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
benct committed Nov 15, 2020
1 parent c20aa7d commit c2d9d7f
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,40 @@ class MultipleEntityRow extends LitElement {
return html`<ha-entity-toggle .stateObj="${stateObj}" .hass="${this._hass}"></ha-entity-toggle>`;
}
if (config.format) {
const value = entityValue(stateObj, config);
const unit = entityUnit(stateObj, config);
return this.renderFormat(stateObj, config);
}
return entityStateDisplay(this._hass, stateObj, config);
}

if (isNaN(parseFloat(value)) || !isFinite(value)) {
renderFormat(stateObj, config) {
const value = entityValue(stateObj, config);

if (['relative', 'total', 'date', 'time', 'datetime'].includes(config.format)) {
const timestamp = new Date(value);
if (!(timestamp instanceof Date) || isNaN(timestamp.getTime())) {
return value;
}
if (config.format === 'brightness') {
return `${Math.round((value / 255) * 100)} %`;
}
if (config.format === 'duration') {
return secondsToDuration(value);
}
if (config.format.startsWith('precision')) {
const precision = parseInt(config.format.slice(-1), 10);
return `${parseFloat(value).toFixed(precision)}${unit ? ` ${unit}` : ''}`;
}
return html`<hui-timestamp-display
.ts=${new Date(value)}
.ts=${timestamp}
.format=${config.format}
.hass=${this._hass}
></hui-timestamp-display>`;
}
return entityStateDisplay(this._hass, stateObj, config);
if (isNaN(parseFloat(value)) || !isFinite(value)) {
return value;
}
if (config.format === 'brightness') {
return `${Math.round((value / 255) * 100)} %`;
}
if (config.format === 'duration') {
return secondsToDuration(value);
}
if (config.format.startsWith('precision')) {
const unit = entityUnit(stateObj, config);
const precision = parseInt(config.format.slice(-1), 10);
return `${parseFloat(value).toFixed(precision)}${unit ? ` ${unit}` : ''}`;
}
return value;
}

renderIcon(stateObj, config) {
Expand Down

0 comments on commit c2d9d7f

Please sign in to comment.