Skip to content

Commit

Permalink
Precision not working correct decimal point locale (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcokreeft87 committed Sep 27, 2022
1 parent de9de6f commit 0c8afad
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "room-card",
"version": "1.05.02",
"version": "1.05.03",
"description": "Show entities in Home Assistant's Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
4 changes: 2 additions & 2 deletions room-card.js

Large diffs are not rendered by default.

Binary file modified room-card.js.gz
Binary file not shown.
19 changes: 11 additions & 8 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { secondsToDuration } from './lib/seconds_to_duration';
import { formatNumber } from './lib/format_number';
import { computeStateDisplay, computeStateDomain } from './lib/compute_state_display';
import { checkConditionalValue, evalTemplate, getValue, hideIf, isObject, isUnavailable } from './util';
import { ActionConfig, handleClick, HomeAssistant } from 'custom-card-helpers';
import { ActionConfig, handleClick, HomeAssistant, NumberFormat } from 'custom-card-helpers';
import { HomeAssistantEntity, EntityCondition, RoomCardEntity, RoomCardIcon, RoomCardConfig, EntityStyles } from './types/room-card-types';
import { html, HTMLTemplateResult, LitElement } from 'lit';
import { LAST_CHANGED, LAST_UPDATED, TIMESTAMP_FORMATS } from './lib/constants';
Expand Down Expand Up @@ -79,20 +79,23 @@ export const entityStateDisplay = (hass: HomeAssistant, entity: RoomCardEntity)
: entity.unit || entity.stateObj.attributes.unit_of_measurement;

if (entity.format) {
if (isNaN(parseFloat(value)) || !isFinite(value)) {
if (entity.format.startsWith('precision')) {

const precision = parseInt(entity.format.slice(-1), 10);
const localizedValue = hass.locale.number_format === NumberFormat.comma_decimal ? value : value.replaceAll(",",".")
value = formatNumber(localizedValue, hass.locale, {
minimumFractionDigits: precision,
maximumFractionDigits: precision,
});
}
else if (isNaN(parseFloat(value)) || !isFinite(value)) {
// do nothing if not a number
} else if (entity.format === 'brightness') {
value = Math.round((value / 255) * 100);
unit = '%';
} else if (entity.format.startsWith('duration')) {
value = secondsToDuration(entity.format === 'duration-m' ? value / 1000 : value);
unit = undefined;
} else if (entity.format.startsWith('precision')) {
const precision = parseInt(entity.format.slice(-1), 10);
value = formatNumber(parseFloat(value), hass.locale, {
minimumFractionDigits: precision,
maximumFractionDigits: precision,
});
} else if (entity.format === 'kilo') {
value = formatNumber(value / 1000, hass.locale, { maximumFractionDigits: 2 });
} else if (entity.format === 'invert') {
Expand Down
35 changes: 32 additions & 3 deletions tests/entity/renderValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Testing entity file function renderValue', () => {
hass.localize = jest.fn();
hass.locale = {
language: 'NL',
number_format: NumberFormat.comma_decimal,
number_format: NumberFormat.decimal_comma,
time_format: TimeFormat.language
}

Expand Down Expand Up @@ -86,9 +86,9 @@ describe('Testing entity file function renderValue', () => {
${'brightness'} ${'77'} ${'30 %'}
${'duration'} ${'1000'} ${'16:40'}
${'duration-m'} ${'1000'} ${'1'}
${'precision2'} ${'2.2324'} ${'2.23'}
${'precision2'} ${'2,2324'} ${'2,23'}
${'kilo'} ${'1000'} ${'1'}
${'invert'} ${'1000'} ${'-1,000'}
${'invert'} ${'1000'} ${'-1.000'}
${'position'} ${'10'} ${'90'}
${'position'} ${'notanumber'} ${'notanumber'}
Expand All @@ -103,6 +103,35 @@ describe('Testing entity file function renderValue', () => {
expect(renderValue(entity, hass)).toBe(expected);
}),
test.each`
format | state | expected
${'brightness'} ${'77'} ${'30 %'}
${'duration'} ${'1000'} ${'16:40'}
${'duration-m'} ${'1000'} ${'1'}
${'precision2'} ${'2.2324'} ${'2.23'}
${'kilo'} ${'1000'} ${'1'}
${'invert'} ${'1000'} ${'-1,000'}
${'position'} ${'10'} ${'90'}
${'position'} ${'notanumber'} ${'notanumber'}
`('Passing RoomCardEntity and HomeAssistant with English locale should return formatted value', ({format, state, expected}) => {

const hassEnglish = createMock<HomeAssistant>();
hassEnglish.localize = jest.fn();
hassEnglish.locale = {
language: 'en',
number_format: NumberFormat.comma_decimal,
time_format: TimeFormat.language
}

stateObj.state = state;
const entity: RoomCardEntity = {
stateObj: stateObj,
format: format
};

expect(renderValue(entity, hassEnglish)).toBe(expected);
}),
test.each`
unit | attribute | attribute_value | expected
${'€'} ${'money'} ${'25'} ${'25 €'}
${'€'} ${'string_attribute'} ${'test'} ${'test €'}
Expand Down

0 comments on commit 0c8afad

Please sign in to comment.