From 75455b2ce7cd547e9a2d275a37f7f7664dc763e2 Mon Sep 17 00:00:00 2001 From: benct Date: Sun, 8 Mar 2020 01:17:05 +0100 Subject: [PATCH] Support format config for date/time values (closes #43) --- README.md | 3 +++ multiple-entity-row.js | 54 ++++++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 799e2bb..bf341c1 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ This card produces an `entity-row` and must therefore be configured as an entity | show_state | bool | `true` | Set to `false` to hide the entity state | state_header | string | | Show header text above the main entity state | state_color | bool | `false` | Enable colored icon when entity is active +| format | string | | Format timestamp value (`relative`, `date`, `time`, `datetime`) | | | | | entities | list | *see below* | Additional entity IDs or entity object(s) | secondary_info | string/object | *see below* | Custom `secondary_info` entity object @@ -63,6 +64,7 @@ attribute value instead of the state value. `icon` lets you display an icon inst | icon | string/bool | `false` | Display default or custom icon instead of state or attribute value | state_color | bool | `false` | Enable colored icon when entity is active | tap_action | object | *see below* | Custom entity tap action +| format | string | | Format timestamp value (`relative`, `date`, `time`, `datetime`) ### Secondary Info @@ -76,6 +78,7 @@ or an object containing the following configuration: | attribute | string | | A valid attribute key for the entity | name | string/bool | `friendly_name` | Override entity `friendly_name`, or `false` to hide | unit | string | `unit_of_measurement` | Override entity `unit_of_measurement` +| format | string | | Format timestamp value (`relative`, `date`, `time`, `datetime`) ### Tap Action diff --git a/multiple-entity-row.js b/multiple-entity-row.js index 1847424..05961c4 100644 --- a/multiple-entity-row.js +++ b/multiple-entity-row.js @@ -84,34 +84,57 @@
${this.state.name} -
- ${this.lastChanged - ? html`` - : (this.state.info && `${this.state.info.name ? `${this.state.info.name} ` : ''}${this.state.info.value}`)} -
+
${this.renderSecondaryInfo()}
${this.state.entities.map(entity => this.renderEntity(entity))} ${this.state.value ? html`
${this.stateHeader && html`${this.stateHeader}`} - ${this.state.toggle - ? html`
` - : html`
${this.state.value}
`} + ${this.renderMainState()}
` : null}
`; } + renderMainState() { + if (this.state.toggle) return html`
${this.renderToggle(this.state.stateObj)}
`; + else if (this._config.format) return this.renderTimestamp(this.state.value, this._config.format); + else return html`
${this.state.value}
`; + } + + renderSecondaryInfo() { + return this.lastChanged + ? html`` + : this.state.info && this.state.info.format + ? this.renderTimestamp(this.state.info.value, this.state.info.format) + : (this.state.info && `${this.state.info.name ? `${this.state.info.name} ` : ''}${this.state.info.value}`) + } + + renderToggle(stateObj) { + return html``; + } + + renderTimestamp(value, format) { + return !['unknown', 'unavailable'].includes(value.toLowerCase()) + ? html`` + : html`${value}`; + } + + renderIcon(entity) { + return html``; + } + + renderEntityValue(entity) { + if (entity.toggle) return this.renderToggle(entity.stateObj); + else if (entity.icon) return this.renderIcon(entity); + else if (entity.format) return this.renderTimestamp(entity.value, entity.format); + else return html`${entity.value}`; + } + renderEntity(entity) { return entity ? html`
${entity.name} -
- ${entity.toggle - ? html`` - : entity.icon - ? html`` - : html`${entity.value}`} -
+
${this.renderEntityValue(entity)}
` : null; } @@ -187,6 +210,7 @@ : this.entityStateValue(stateObj, config.unit), toggle: this.checkToggle(config, stateObj), icon: config.icon === true ? stateObj.attributes.icon : config.icon, + format: config.format || false, state_color: config.state_color || false, onClick: this.getAction(config.tap_action, stateObj.entity_id), };