From 453052dfcf84399ea4ab6bee28e47767ad162229 Mon Sep 17 00:00:00 2001 From: Guillaume Corgnet Date: Sat, 26 Mar 2022 06:58:24 +1300 Subject: [PATCH] fix #322. Adding conversion rate and rounding to allow displaying hours when the platform supplies seconds --- README.md | 2 ++ src/vacuum-card.js | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index af73acb5..100e8797 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,8 @@ You can use any attribute of vacuum or even any entity by `entity_id` to display | `attribute` | `string` | Optional | Attribute name of the stat, i.e. `filter_left`. | | `unit` | `string` | Optional | Unit of measure, i.e. `hours`. | | `subtitle` | `string` | Optional | Friendly name of the stat, i.e. `Filter`. | +| `conversion`| `number` | Optional | A factor to divide the current value by when displaying. (Can be `3600` if the platform provides times in seconds and we need them displayed in hours)| +| `rounding`| `number` | Optional | The number of decimal points used when displaying the value| ### `actions` object diff --git a/src/vacuum-card.js b/src/vacuum-card.js index df0d8622..69ab4e97 100755 --- a/src/vacuum-card.js +++ b/src/vacuum-card.js @@ -323,12 +323,24 @@ class VacuumCard extends LitElement { return nothing; } + formatValue(value, conversion, rounding){ + if (conversion && !isNaN(conversion)) { + value = value / conversion; + } + + if (!isNaN(rounding)) { + return value.toFixed(rounding) + } + + return value; + } + renderStats(state) { const { stats = {} } = this.config; const statsList = stats[state] || stats.default || []; - return statsList.map(({ entity_id, attribute, unit, subtitle }) => { + return statsList.map(({ entity_id, attribute, unit, subtitle, conversion, rounding }) => { if (!entity_id && !attribute) { return nothing; } @@ -336,10 +348,12 @@ class VacuumCard extends LitElement { const value = entity_id ? this.hass.states[entity_id].state : get(this.entity.attributes, attribute); + + return html`
- ${value} + ${this.formatValue(value, conversion, rounding)} ${unit}
${subtitle}