Skip to content

Commit

Permalink
fix denysdovhan#322. Adding conversion rate and rounding to allow dis…
Browse files Browse the repository at this point in the history
…playing hours when the platform supplies seconds
  • Loading branch information
gcorgnet committed Mar 25, 2022
1 parent 001e917 commit 453052d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 16 additions & 2 deletions src/vacuum-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,23 +323,37 @@ 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;
}

const value = entity_id
? this.hass.states[entity_id].state
: get(this.entity.attributes, attribute);



return html`
<div class="stats-block">
<span class="stats-value">${value}</span>
<span class="stats-value">${this.formatValue(value, conversion, rounding)}</span>
${unit}
<div class="stats-subtitle">${subtitle}</div>
</div>
Expand Down

0 comments on commit 453052d

Please sign in to comment.