fix(templates): don't use the `numeric_precision` from the card config for `localize` in js templates by default

This commit is contained in:
Jérôme Wiedemann 2023-07-25 21:37:53 +00:00
parent e5fa12c4ec
commit 2cc384f9dc
2 changed files with 8 additions and 4 deletions

View File

@ -337,8 +337,8 @@ Inside the javascript code, you'll have access to those variables:
- `localize(entity, state?, numeric_precision?, show_units?, units?)`: a function which localizes a state (eg. `localize(entity)`) and returns a string. Takes an entity object as argument (not the state of the entity as we need context) and takes optional arguments.
- If `state` is not provided, it localizes the state of the `entity` (Eg. `localize(entity)` or `localize(states['weather.your_city'])`).
- If `state` is provided, it localizes `state` in the context of the `entity` (eg. : `localize(entity, entity.attributes.forecast[0].condition)` or `localize(states['weather.your_city'], states['weather.your_city'].attributes.forecast[0].condition)`)
- `numeric_precision` (number): For state which are numbers, force the precision instead of letting HA decide for you
- `show_units` (boolean): Will display units or not. Default is to display them.
- `numeric_precision` (number or `'card'`. Default is `undefined`): For state which are numbers, force the precision instead of letting HA decide for you. If the value is set to `'card'`, it will use the `numeric_precision` from the main config. If `undefined`, it will use the default value for the entity you're willing to display. The latter is the default.
- `show_units` (boolean. Default is `true`): Will display units or not. Default is to display them.
- `units` (string): Will force the units to be the value of that parameter.
- To skip one or multiple parameter while calling the function, use `undefined`. Eg. `localize(states['sensor.temperature'], undefined, 1, undefined, 'Celcius')`
- `formatDateTime(date)`, `formatShortDateTimeWithYear(date)`, `formatShortDateTime(date)`, `formatDateTimeWithSeconds(date)`, `formatDateTimeNumeric(date)`: Some helper functions to format a date time string or Date object. Name are pretty explicit. Example: `return formatDateTime(entity.attribute.last_changed)`

View File

@ -298,7 +298,7 @@ class ButtonCard extends LitElement {
private _localize(
stateObj: HassEntity,
state?: string,
numeric_precision?: number,
numeric_precision?: number | 'card',
show_units = true,
units?: string,
): string {
@ -309,7 +309,11 @@ class ButtonCard extends LitElement {
this._hass!.locale,
this._hass!.config,
this._hass!.entities,
{ numeric_precision: numeric_precision || this._config?.numeric_precision, show_units, units },
{
numeric_precision: numeric_precision === 'card' ? this._config?.numeric_precision : numeric_precision,
show_units,
units,
},
state,
);
}