Skip to content

Commit

Permalink
Implement new formatting options (#135, #151, #174)
Browse files Browse the repository at this point in the history
  • Loading branch information
benct committed Dec 22, 2021
1 parent e4c6cbe commit 1132141
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ The `format` option supports the following values:
| datetime | `timestamp` | Convert timestamp value to date and time |
| brightness | `number` | Convert brightness value to percentage |
| duration | `number` | Convert number of seconds to duration (`5:38:50`) |
| duration-m | `number` | Convert number of milliseconds to duration (`5:38:50`) |
| invert | `number` | Convert number from positive to negative or vice versa |
| kilo | `number` | Divide number value by 1000 (ex. `1500 W` -> `1.5 kW`) |
| position | `number` | Reverses a position percentage (ex. `70%` open -> `30%` closed) |
| precision<0-9> | `number` | Set decimal precision of number value (`precision3` -> `18.123`) |

## Examples
Expand Down
32 changes: 18 additions & 14 deletions src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const entityStateDisplay = (hass, stateObj, config) => {
return hass.localize(`state.default.${stateObj.state}`);
}

const value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
const unit =
let value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
let unit =
config.unit === false
? undefined
: config.attribute !== undefined
Expand All @@ -39,23 +39,27 @@ export const entityStateDisplay = (hass, stateObj, config) => {

if (config.format) {
if (isNaN(parseFloat(value)) || !isFinite(value)) {
return value;
}
if (config.format === 'brightness') {
return `${Math.round((value / 255) * 100)} %`;
}
if (config.format === 'duration') {
return secondsToDuration(value);
}
if (config.format.startsWith('precision')) {
// do nothing if not a number
} else if (config.format === 'brightness') {
value = Math.round((value / 255) * 100);
unit = '%';
} else if (config.format.startsWith('duration')) {
value = secondsToDuration(config.format === 'duration-m' ? value / 1000 : value);
unit = undefined;
} else if (config.format.startsWith('precision')) {
const precision = parseInt(config.format.slice(-1), 10);
const formatted = formatNumber(parseFloat(value), hass.locale, {
value = formatNumber(parseFloat(value), hass.locale, {
minimumFractionDigits: precision,
maximumFractionDigits: precision,
});
return `${formatted}${unit ? ` ${unit}` : ''}`;
} else if (config.format === 'kilo') {
value = formatNumber(value / 1000, hass.locale, { maximumFractionDigits: 2 });
} else if (config.format === 'invert') {
value = value - value * 2;
} else if (config.format === 'position') {
value = 100 - value;
}
return value;
return `${value}${unit ? ` ${unit}` : ''}`;
}

if (config.attribute) {
Expand Down

0 comments on commit 1132141

Please sign in to comment.