Short-format negative and small numbers in energy-distribution-card (#25862)

This commit is contained in:
Leon 2025-06-25 15:22:21 +02:00 committed by GitHub
parent 2dfe5f50a6
commit e7a04eb3d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 15 deletions

View File

@ -1114,12 +1114,16 @@ export const formatConsumptionShort = (
if (!consumption) { if (!consumption) {
return `0 ${unit}`; return `0 ${unit}`;
} }
const units = ["kWh", "MWh", "GWh", "TWh"]; const units = ["Wh", "kWh", "MWh", "GWh", "TWh"];
let pickedUnit = unit; let pickedUnit = unit;
let val = consumption; let val = consumption;
let unitIndex = units.findIndex((u) => u === unit); let unitIndex = units.findIndex((u) => u === unit);
if (unitIndex >= 0) { if (unitIndex >= 0) {
while (val >= 1000 && unitIndex < units.length - 1) { while (Math.abs(val) < 1 && unitIndex > 0) {
val *= 1000;
unitIndex--;
}
while (Math.abs(val) >= 1000 && unitIndex < units.length - 1) {
val /= 1000; val /= 1000;
unitIndex++; unitIndex++;
} }
@ -1127,7 +1131,8 @@ export const formatConsumptionShort = (
} }
return ( return (
formatNumber(val, hass.locale, { formatNumber(val, hass.locale, {
maximumFractionDigits: val < 10 ? 2 : val < 100 ? 1 : 0, maximumFractionDigits:
Math.abs(val) < 10 ? 2 : Math.abs(val) < 100 ? 1 : 0,
}) + }) +
" " + " " +
pickedUnit pickedUnit

View File

@ -68,15 +68,18 @@ describe("Energy Short Format Test", () => {
}; };
const hass = { locale: defaultLocale } as HomeAssistant; const hass = { locale: defaultLocale } as HomeAssistant;
it("Formats", () => { it("No Unit conversion", () => {
assert.strictEqual(formatConsumptionShort(hass, 0, "Wh"), "0 Wh");
assert.strictEqual(formatConsumptionShort(hass, 0, "kWh"), "0 kWh"); assert.strictEqual(formatConsumptionShort(hass, 0, "kWh"), "0 kWh");
assert.strictEqual(formatConsumptionShort(hass, 0, "GWh"), "0 GWh"); assert.strictEqual(formatConsumptionShort(hass, 0, "GWh"), "0 GWh");
assert.strictEqual(formatConsumptionShort(hass, 0, "gal"), "0 gal"); assert.strictEqual(formatConsumptionShort(hass, 0, "gal"), "0 gal");
assert.strictEqual( assert.strictEqual(
formatConsumptionShort(hass, 0.12345, "kWh"), formatConsumptionShort(hass, 10000.12345, "gal"),
"0.12 kWh" "10,000 gal"
); );
assert.strictEqual(formatConsumptionShort(hass, 1.2345, "kWh"), "1.23 kWh");
assert.strictEqual( assert.strictEqual(
formatConsumptionShort(hass, 10.12345, "kWh"), formatConsumptionShort(hass, 10.12345, "kWh"),
"10.1 kWh" "10.1 kWh"
@ -85,6 +88,10 @@ describe("Energy Short Format Test", () => {
formatConsumptionShort(hass, 500.12345, "kWh"), formatConsumptionShort(hass, 500.12345, "kWh"),
"500 kWh" "500 kWh"
); );
assert.strictEqual(formatConsumptionShort(hass, 10.01, "kWh"), "10 kWh");
});
it("Upward Unit conversion", () => {
assert.strictEqual( assert.strictEqual(
formatConsumptionShort(hass, 1512.34567, "kWh"), formatConsumptionShort(hass, 1512.34567, "kWh"),
"1.51 MWh" "1.51 MWh"
@ -105,23 +112,31 @@ describe("Energy Short Format Test", () => {
formatConsumptionShort(hass, 15123456789.9, "kWh"), formatConsumptionShort(hass, 15123456789.9, "kWh"),
"15.1 TWh" "15.1 TWh"
); );
assert.strictEqual( assert.strictEqual(
formatConsumptionShort(hass, 15123456789000.9, "kWh"), formatConsumptionShort(hass, 15123456789000.9, "kWh"),
"15,123 TWh" "15,123 TWh"
); );
});
assert.strictEqual(formatConsumptionShort(hass, 1000.1, "GWh"), "1 TWh"); it("Downward Unit conversion", () => {
assert.strictEqual(formatConsumptionShort(hass, 0.00012, "kWh"), "0.12 Wh");
assert.strictEqual(formatConsumptionShort(hass, 0.12345, "kWh"), "123 Wh");
assert.strictEqual( assert.strictEqual(
formatConsumptionShort(hass, 10000.12345, "gal"), formatConsumptionShort(hass, 0.00001234, "TWh"),
"10,000 gal" "12.3 MWh"
);
});
it("Negativ Consumption", () => {
assert.strictEqual(
formatConsumptionShort(hass, -500.123, "kWh"),
"-500 kWh"
); );
// Don't really modify negative numbers, but make sure it's something sane.
assert.strictEqual( assert.strictEqual(
formatConsumptionShort(hass, -1234.56, "kWh"), formatConsumptionShort(hass, -1234.56, "kWh"),
"-1,234.56 kWh" "-1.23 MWh"
);
assert.strictEqual(
formatConsumptionShort(hass, -0.001234, "kWh"),
"-1.23 Wh"
); );
}); });
}); });