Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #322. Adding conversion rate and rounding changing displayed units. #347

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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