Skip to content

Commit

Permalink
Add support for specifying number of decimals to use. Fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
nervetattoo committed Mar 22, 2019
1 parent 9668fd3 commit e7042e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ resources:

- `entity` _string_: The thermostat entity id **required**
- `name` _string|false_: Override the card name, or disable showing a name at all. Default is to use the friendly_name of the thermostat provided
- `decimals` _number_: Specify number of decimals to use: 1 or 0
- `icon` _string|object_: Show an icon next to the card name. You can also pass an object to specify state-specific icons. Defaults state-specific icons radiator/radiator-disabled/snowflake
- `idle`: _string_: Use this icon for state idle
- `heat`: _string_ Use this icon for state heat
Expand Down
30 changes: 22 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import { Debouncer } from '@polymer/polymer/lib/utils/debounce'

import { renderStyles, renderNotFoundStyles } from './src/styles'

function formatNumber(number) {
const [int, dec] = String(number).split('.')
return `${int}.${dec || '0'}`
}

const DEBOUNCE_TIMEOUT = 1000
const STEP_SIZE = 0.5
const DECIMALS = 1
const UPDATE_PROPS = ['entity', 'sensors', '_values']
const modeIcons = {
auto: 'hass:autorenew',
Expand Down Expand Up @@ -53,6 +49,18 @@ function getEntityType(attributes) {
return 'single'
}

function formatNumber(number, decimals = 1) {
const [int, dec] = String(number).split('.')
if (Number.isNaN(int)) {
return 'N/A'
}
if (decimals) {
return `${int}.${dec || '0'}`
} else {
return Math.round(int)
}
}

class SimpleThermostat extends LitElement {
static get properties() {
return {
Expand Down Expand Up @@ -230,7 +238,10 @@ class SimpleThermostat extends LitElement {
const sensorHtml = [
_hide.temperature
? null
: this.renderInfoItem(`${formatNumber(current)}${unit}`, 'Temperature'),
: this.renderInfoItem(
`${formatNumber(current, config.decimals)}${unit}`,
'Temperature'
),
_hide.state
? null
: this.renderInfoItem(this.localize(state, 'state.climate.'), 'State'),
Expand Down Expand Up @@ -263,7 +274,7 @@ class SimpleThermostat extends LitElement {
<div @click=${() => this.openEntityPopover()}>
<h3 class="current--value">
${formatNumber(value)}
${formatNumber(value, config.decimals)}
</h3>
</div>
<paper-icon-button
Expand Down Expand Up @@ -466,7 +477,10 @@ class SimpleThermostat extends LitElement {
if (!config.entity) {
throw new Error('You need to define an entity')
}
this.config = config
this.config = {
decimals: DECIMALS,
...config,
}
}

// The height of your card. Home Assistant uses this to automatically
Expand Down

0 comments on commit e7042e0

Please sign in to comment.