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

When giving precision and unit error was thrown #70

Merged
merged 2 commits into from
Sep 27, 2022
Merged
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
8 changes: 8 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

### Features

{% if version_installed.replace("v", "").replace(".","") | int < 10504 %}
- Fixed `When giving precision and unit error was thrown`
{% endif %}

{% if version_installed.replace("v", "").replace(".","") | int < 10503 %}
- Fixed `Precision format not working correct when using decimal point locale`
{% endif %}

{% if version_installed.replace("v", "").replace(".","") | int < 10502 %}
- Fixed `Some custom cards (like gauge and thermostat) gave the error 'n.setConfig is not a function'`
- Added `New version numbering method (1.05.02 in stead of 1.5.2) to make smaller updates possible`
Expand Down
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.04",
"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.toString().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
48 changes: 45 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 All @@ -118,6 +147,19 @@ describe('Testing entity file function renderValue', () => {
};

expect(renderValue(entity, hass)).toBe(expected);
}),
test('Passing hass and state on entity with unit and format should return expected', () => {

stateObj.state = 'on';
stateObj.attributes['money'] = 25.2323;
const entity: RoomCardEntity = {
stateObj: stateObj,
attribute: 'money',
unit: 'L',
format: 'precision2'
};

expect(renderValue(entity, hass)).toBe('25,23 L');
})
})