feat: Force the `numeric_precision` for states which are numbers

This commit is contained in:
Jérôme Wiedemann 2023-07-24 19:20:45 +00:00
parent a448c8e826
commit 24d75c2651
5 changed files with 27 additions and 4 deletions

View File

@ -106,6 +106,7 @@ Lovelace Button card for your entities.
| `label` | string | optional | Any string that you want | Display a label below the card. See [Layouts](#layout) for more information. Supports templates, see [templates](#javascript-templates) |
| `show_name` | boolean | `true` | `true` \| `false` | Wether to show the name or not. Will pick entity_id's name by default, unless redefined in the `name` property or in any state `name` property |
| `show_state` | boolean | `false` | `true` \| `false` | Show the state on the card. defaults to false if not set |
| `numeric_precision` | number | none | any number | Force the display precision of the state to be with `numeric_precision` decimals |
| `show_icon` | boolean | `true` | `true` \| `false` | Wether to show the icon or not. Unless redefined in `icon`, uses the default entity icon from hass |
| `show_units` | boolean | `true` | `true` \| `false` | Display or hide the units of a sensor, if any. |
| `show_label` | boolean | `false` | `true` \| `false` | Display or hide the `label` |

View File

@ -302,6 +302,7 @@ class ButtonCard extends LitElement {
this._hass!.locale,
this._hass!.config,
this._hass!.entities,
this._config?.numeric_precision,
state,
);
}
@ -553,6 +554,7 @@ class ButtonCard extends LitElement {
this._hass!.locale,
this._hass!.config,
this._hass!.entities,
this._config?.numeric_precision,
);
} else {
stateString = this._computeTimeDisplay(stateObj);
@ -563,6 +565,7 @@ class ButtonCard extends LitElement {
this._hass!.locale,
this._hass!.config,
this._hass!.entities,
this._config?.numeric_precision,
)})`;
}
}
@ -575,6 +578,7 @@ class ButtonCard extends LitElement {
this._hass!.locale,
this._hass!.config,
this._hass!.entities,
this._config?.numeric_precision,
);
}
}

View File

@ -24,6 +24,7 @@ export const computeStateDisplaySingleEntity = (
locale: FrontendLocaleData,
config: HassConfig,
entity: EntityRegistryDisplayEntry | undefined,
numeric_precision: number | undefined,
state?: string,
): string =>
computeStateDisplayFromEntityAttributes(
@ -33,6 +34,7 @@ export const computeStateDisplaySingleEntity = (
entity,
stateObj.entity_id,
stateObj.attributes,
numeric_precision,
state !== undefined ? state : stateObj.state,
);
@ -42,6 +44,7 @@ export const computeStateDisplay = (
locale: FrontendLocaleData,
config: HassConfig,
entities: HomeAssistant['entities'],
numeric_precision: number | undefined,
state?: string,
): string => {
const entity = entities[stateObj.entity_id] as EntityRegistryDisplayEntry | undefined;
@ -53,6 +56,7 @@ export const computeStateDisplay = (
entity,
stateObj.entity_id,
stateObj.attributes,
numeric_precision,
state !== undefined ? state : stateObj.state,
);
};
@ -64,6 +68,7 @@ export const computeStateDisplayFromEntityAttributes = (
entity: EntityRegistryDisplayEntry | undefined,
entityId: string,
attributes: any,
numeric_precision: number | undefined,
state: string,
): string => {
if (state === UNKNOWN || state === UNAVAILABLE) {
@ -91,7 +96,7 @@ export const computeStateDisplayFromEntityAttributes = (
currency: attributes.unit_of_measurement,
minimumFractionDigits: 2,
// Override monetary options with number format
...getNumberFormatOptions({ state, attributes } as HassEntity, entity),
...getNumberFormatOptions({ state, attributes } as HassEntity, numeric_precision, entity),
});
} catch (_err) {
// fallback to default
@ -102,7 +107,11 @@ export const computeStateDisplayFromEntityAttributes = (
: attributes.unit_of_measurement === '%'
? blankBeforePercent(locale) + '%'
: ` ${attributes.unit_of_measurement}`;
return `${formatNumber(state, locale, getNumberFormatOptions({ state, attributes } as HassEntity, entity))}${unit}`;
return `${formatNumber(
state,
locale,
getNumberFormatOptions({ state, attributes } as HassEntity, numeric_precision, entity),
)}${unit}`;
}
const domain = computeDomain(entityId);
@ -149,7 +158,11 @@ export const computeStateDisplayFromEntityAttributes = (
// `counter` `number` and `input_number` domains do not have a unit of measurement but should still use `formatNumber`
if (domain === 'counter' || domain === 'number' || domain === 'input_number') {
// Format as an integer if the value and step are integers
return formatNumber(state, locale, getNumberFormatOptions({ state, attributes } as HassEntity, entity));
return formatNumber(
state,
locale,
getNumberFormatOptions({ state, attributes } as HassEntity, numeric_precision, entity),
);
}
// state is a timestamp

View File

@ -89,9 +89,13 @@ export const formatNumber = (
*/
export const getNumberFormatOptions = (
entityState: HassEntity,
numeric_precision: number | undefined,
entity?: EntityRegistryDisplayEntry,
): Intl.NumberFormatOptions | undefined => {
const precision = entity?.display_precision;
let precision = entity?.display_precision;
if (numeric_precision !== undefined) {
precision = numeric_precision;
}
if (precision != null) {
return {
maximumFractionDigits: precision,

View File

@ -27,6 +27,7 @@ export interface ButtonCardConfig {
show_label?: boolean;
show_live_stream?: boolean;
label?: string;
numeric_precision?: number;
entity_picture?: string;
units?: string;
state_display?: string;