mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-17 14:26:35 +00:00
Measurement number format (#10459)
This commit is contained in:
parent
4624c3d75b
commit
b188c4ec81
@ -4,7 +4,7 @@ import { FrontendLocaleData } from "../../data/translation";
|
|||||||
import { formatDate } from "../datetime/format_date";
|
import { formatDate } from "../datetime/format_date";
|
||||||
import { formatDateTime } from "../datetime/format_date_time";
|
import { formatDateTime } from "../datetime/format_date_time";
|
||||||
import { formatTime } from "../datetime/format_time";
|
import { formatTime } from "../datetime/format_time";
|
||||||
import { formatNumber } from "../number/format_number";
|
import { formatNumber, isNumericState } from "../number/format_number";
|
||||||
import { LocalizeFunc } from "../translations/localize";
|
import { LocalizeFunc } from "../translations/localize";
|
||||||
import { computeStateDomain } from "./compute_state_domain";
|
import { computeStateDomain } from "./compute_state_domain";
|
||||||
|
|
||||||
@ -20,7 +20,8 @@ export const computeStateDisplay = (
|
|||||||
return localize(`state.default.${compareState}`);
|
return localize(`state.default.${compareState}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stateObj.attributes.unit_of_measurement) {
|
// Entities with a `unit_of_measurement` or `state_class` are numeric values and should use `formatNumber`
|
||||||
|
if (isNumericState(stateObj)) {
|
||||||
if (stateObj.attributes.device_class === "monetary") {
|
if (stateObj.attributes.device_class === "monetary") {
|
||||||
try {
|
try {
|
||||||
return formatNumber(compareState, locale, {
|
return formatNumber(compareState, locale, {
|
||||||
@ -31,8 +32,10 @@ export const computeStateDisplay = (
|
|||||||
// fallback to default
|
// fallback to default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return `${formatNumber(compareState, locale)} ${
|
return `${formatNumber(compareState, locale)}${
|
||||||
stateObj.attributes.unit_of_measurement
|
stateObj.attributes.unit_of_measurement
|
||||||
|
? " " + stateObj.attributes.unit_of_measurement
|
||||||
|
: ""
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
|
import { HassEntity } from "home-assistant-js-websocket";
|
||||||
import { FrontendLocaleData, NumberFormat } from "../../data/translation";
|
import { FrontendLocaleData, NumberFormat } from "../../data/translation";
|
||||||
import { round } from "./round";
|
import { round } from "./round";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the entity is considered numeric based on the attributes it has
|
||||||
|
* @param stateObj The entity state object
|
||||||
|
*/
|
||||||
|
export const isNumericState = (stateObj: HassEntity): boolean =>
|
||||||
|
!!stateObj.attributes.unit_of_measurement ||
|
||||||
|
!!stateObj.attributes.state_class;
|
||||||
|
|
||||||
export const numberFormatToLocale = (
|
export const numberFormatToLocale = (
|
||||||
localeOptions: FrontendLocaleData
|
localeOptions: FrontendLocaleData
|
||||||
): string | string[] | undefined => {
|
): string | string[] | undefined => {
|
||||||
|
@ -14,7 +14,10 @@ import secondsToDuration from "../../common/datetime/seconds_to_duration";
|
|||||||
import { computeStateDisplay } from "../../common/entity/compute_state_display";
|
import { computeStateDisplay } from "../../common/entity/compute_state_display";
|
||||||
import { computeStateDomain } from "../../common/entity/compute_state_domain";
|
import { computeStateDomain } from "../../common/entity/compute_state_domain";
|
||||||
import { computeStateName } from "../../common/entity/compute_state_name";
|
import { computeStateName } from "../../common/entity/compute_state_name";
|
||||||
import { formatNumber } from "../../common/number/format_number";
|
import {
|
||||||
|
formatNumber,
|
||||||
|
isNumericState,
|
||||||
|
} from "../../common/number/format_number";
|
||||||
import { UNAVAILABLE, UNKNOWN } from "../../data/entity";
|
import { UNAVAILABLE, UNKNOWN } from "../../data/entity";
|
||||||
import { timerTimeRemaining } from "../../data/timer";
|
import { timerTimeRemaining } from "../../data/timer";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
@ -145,7 +148,7 @@ export class HaStateLabelBadge extends LitElement {
|
|||||||
return entityState.state === UNKNOWN ||
|
return entityState.state === UNKNOWN ||
|
||||||
entityState.state === UNAVAILABLE
|
entityState.state === UNAVAILABLE
|
||||||
? "-"
|
? "-"
|
||||||
: entityState.attributes.unit_of_measurement
|
: isNumericState(entityState)
|
||||||
? formatNumber(entityState.state, this.hass!.locale)
|
? formatNumber(entityState.state, this.hass!.locale)
|
||||||
: computeStateDisplay(
|
: computeStateDisplay(
|
||||||
this.hass!.localize,
|
this.hass!.localize,
|
||||||
|
@ -15,7 +15,10 @@ import { computeStateDisplay } from "../../../common/entity/compute_state_displa
|
|||||||
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
|
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
|
||||||
import { computeStateName } from "../../../common/entity/compute_state_name";
|
import { computeStateName } from "../../../common/entity/compute_state_name";
|
||||||
import { isValidEntityId } from "../../../common/entity/valid_entity_id";
|
import { isValidEntityId } from "../../../common/entity/valid_entity_id";
|
||||||
import { formatNumber } from "../../../common/number/format_number";
|
import {
|
||||||
|
formatNumber,
|
||||||
|
isNumericState,
|
||||||
|
} from "../../../common/number/format_number";
|
||||||
import { iconColorCSS } from "../../../common/style/icon_color_css";
|
import { iconColorCSS } from "../../../common/style/icon_color_css";
|
||||||
import "../../../components/ha-card";
|
import "../../../components/ha-card";
|
||||||
import "../../../components/ha-icon";
|
import "../../../components/ha-icon";
|
||||||
@ -143,7 +146,7 @@ export class HuiEntityCard extends LitElement implements LovelaceCard {
|
|||||||
stateObj.attributes[this._config.attribute!]
|
stateObj.attributes[this._config.attribute!]
|
||||||
)
|
)
|
||||||
: this.hass.localize("state.default.unknown")
|
: this.hass.localize("state.default.unknown")
|
||||||
: stateObj.attributes.unit_of_measurement
|
: isNumericState(stateObj)
|
||||||
? formatNumber(stateObj.state, this.hass.locale)
|
? formatNumber(stateObj.state, this.hass.locale)
|
||||||
: computeStateDisplay(
|
: computeStateDisplay(
|
||||||
this.hass.localize,
|
this.hass.localize,
|
||||||
|
@ -96,6 +96,20 @@ describe("computeStateDisplay", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Localizes and formats numeric sensor value with state_class", () => {
|
||||||
|
const stateObj: any = {
|
||||||
|
entity_id: "sensor.test",
|
||||||
|
state: "1234.5",
|
||||||
|
attributes: {
|
||||||
|
state_class: "measurement",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
assert.strictEqual(
|
||||||
|
computeStateDisplay(localize, stateObj, localeData),
|
||||||
|
"1,234.5 m"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("Localizes unknown sensor value with units", () => {
|
it("Localizes unknown sensor value with units", () => {
|
||||||
const altLocalize = (message, ...args) => {
|
const altLocalize = (message, ...args) => {
|
||||||
if (message === "state.sensor.unknown") {
|
if (message === "state.sensor.unknown") {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user