From e7a04eb3d22b09a69f439858960638532c931139 Mon Sep 17 00:00:00 2001 From: Leon <29leon42005+github@gmail.com> Date: Wed, 25 Jun 2025 15:22:21 +0200 Subject: [PATCH] Short-format negative and small numbers in energy-distribution-card (#25862) --- src/data/energy.ts | 11 ++++++++--- test/data/energy.test.ts | 39 +++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/data/energy.ts b/src/data/energy.ts index 0ee60fde90..3510c92125 100644 --- a/src/data/energy.ts +++ b/src/data/energy.ts @@ -1114,12 +1114,16 @@ export const formatConsumptionShort = ( if (!consumption) { return `0 ${unit}`; } - const units = ["kWh", "MWh", "GWh", "TWh"]; + const units = ["Wh", "kWh", "MWh", "GWh", "TWh"]; let pickedUnit = unit; let val = consumption; let unitIndex = units.findIndex((u) => u === unit); 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; unitIndex++; } @@ -1127,7 +1131,8 @@ export const formatConsumptionShort = ( } return ( 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 diff --git a/test/data/energy.test.ts b/test/data/energy.test.ts index d70ad81593..02d8fe170e 100644 --- a/test/data/energy.test.ts +++ b/test/data/energy.test.ts @@ -68,15 +68,18 @@ describe("Energy Short Format Test", () => { }; 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, "GWh"), "0 GWh"); assert.strictEqual(formatConsumptionShort(hass, 0, "gal"), "0 gal"); assert.strictEqual( - formatConsumptionShort(hass, 0.12345, "kWh"), - "0.12 kWh" + formatConsumptionShort(hass, 10000.12345, "gal"), + "10,000 gal" ); + + assert.strictEqual(formatConsumptionShort(hass, 1.2345, "kWh"), "1.23 kWh"); assert.strictEqual( formatConsumptionShort(hass, 10.12345, "kWh"), "10.1 kWh" @@ -85,6 +88,10 @@ describe("Energy Short Format Test", () => { formatConsumptionShort(hass, 500.12345, "kWh"), "500 kWh" ); + + assert.strictEqual(formatConsumptionShort(hass, 10.01, "kWh"), "10 kWh"); + }); + it("Upward Unit conversion", () => { assert.strictEqual( formatConsumptionShort(hass, 1512.34567, "kWh"), "1.51 MWh" @@ -105,23 +112,31 @@ describe("Energy Short Format Test", () => { formatConsumptionShort(hass, 15123456789.9, "kWh"), "15.1 TWh" ); - assert.strictEqual( formatConsumptionShort(hass, 15123456789000.9, "kWh"), "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( - formatConsumptionShort(hass, 10000.12345, "gal"), - "10,000 gal" + formatConsumptionShort(hass, 0.00001234, "TWh"), + "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( formatConsumptionShort(hass, -1234.56, "kWh"), - "-1,234.56 kWh" + "-1.23 MWh" + ); + assert.strictEqual( + formatConsumptionShort(hass, -0.001234, "kWh"), + "-1.23 Wh" ); }); });