mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-14 21:06:34 +00:00
Coerce all energy distribution values to the same unit (#26117)
This commit is contained in:
parent
15dabe372c
commit
5cf8b39703
@ -1109,21 +1109,31 @@ export const computeConsumptionSingle = (data: {
|
|||||||
export const formatConsumptionShort = (
|
export const formatConsumptionShort = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
consumption: number | null,
|
consumption: number | null,
|
||||||
unit: string
|
unit: string,
|
||||||
|
targetUnit?: string
|
||||||
): string => {
|
): string => {
|
||||||
if (!consumption) {
|
|
||||||
return `0 ${unit}`;
|
|
||||||
}
|
|
||||||
const units = ["Wh", "kWh", "MWh", "GWh", "TWh"];
|
const units = ["Wh", "kWh", "MWh", "GWh", "TWh"];
|
||||||
let pickedUnit = unit;
|
let pickedUnit = unit;
|
||||||
let val = consumption;
|
let val = consumption || 0;
|
||||||
|
let targetUnitIndex = -1;
|
||||||
|
if (targetUnit) {
|
||||||
|
targetUnitIndex = units.findIndex((u) => u === targetUnit);
|
||||||
|
}
|
||||||
let unitIndex = units.findIndex((u) => u === unit);
|
let unitIndex = units.findIndex((u) => u === unit);
|
||||||
if (unitIndex >= 0) {
|
if (unitIndex >= 0) {
|
||||||
while (Math.abs(val) < 1 && unitIndex > 0) {
|
while (
|
||||||
|
targetUnitIndex > -1
|
||||||
|
? targetUnitIndex < unitIndex
|
||||||
|
: Math.abs(val) < 1 && unitIndex > 0
|
||||||
|
) {
|
||||||
val *= 1000;
|
val *= 1000;
|
||||||
unitIndex--;
|
unitIndex--;
|
||||||
}
|
}
|
||||||
while (Math.abs(val) >= 1000 && unitIndex < units.length - 1) {
|
while (
|
||||||
|
targetUnitIndex > -1
|
||||||
|
? targetUnitIndex > unitIndex
|
||||||
|
: Math.abs(val) >= 1000 && unitIndex < units.length - 1
|
||||||
|
) {
|
||||||
val /= 1000;
|
val /= 1000;
|
||||||
unitIndex++;
|
unitIndex++;
|
||||||
}
|
}
|
||||||
|
@ -255,6 +255,20 @@ class HuiEnergyDistrubutionCard
|
|||||||
(batteryFromGrid || 0) +
|
(batteryFromGrid || 0) +
|
||||||
(batteryToGrid || 0);
|
(batteryToGrid || 0);
|
||||||
|
|
||||||
|
// Coerce all energy numbers to the same unit (the biggest)
|
||||||
|
const maxEnergy = Math.max(
|
||||||
|
lowCarbonEnergy || 0,
|
||||||
|
totalSolarProduction || 0,
|
||||||
|
returnedToGrid || 0,
|
||||||
|
totalFromGrid || 0,
|
||||||
|
totalHomeConsumption,
|
||||||
|
totalBatteryIn || 0,
|
||||||
|
totalBatteryOut || 0
|
||||||
|
);
|
||||||
|
const targetEnergyUnit = formatConsumptionShort(this.hass, maxEnergy, "kWh")
|
||||||
|
.split(" ")
|
||||||
|
.pop();
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-card .header=${this._config.title}>
|
<ha-card .header=${this._config.title}>
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
@ -281,7 +295,8 @@ class HuiEnergyDistrubutionCard
|
|||||||
${formatConsumptionShort(
|
${formatConsumptionShort(
|
||||||
this.hass,
|
this.hass,
|
||||||
lowCarbonEnergy,
|
lowCarbonEnergy,
|
||||||
"kWh"
|
"kWh",
|
||||||
|
targetEnergyUnit
|
||||||
)}
|
)}
|
||||||
</a>
|
</a>
|
||||||
<svg width="80" height="30">
|
<svg width="80" height="30">
|
||||||
@ -300,7 +315,8 @@ class HuiEnergyDistrubutionCard
|
|||||||
${formatConsumptionShort(
|
${formatConsumptionShort(
|
||||||
this.hass,
|
this.hass,
|
||||||
totalSolarProduction,
|
totalSolarProduction,
|
||||||
"kWh"
|
"kWh",
|
||||||
|
targetEnergyUnit
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>`
|
</div>`
|
||||||
@ -396,7 +412,8 @@ class HuiEnergyDistrubutionCard
|
|||||||
>${formatConsumptionShort(
|
>${formatConsumptionShort(
|
||||||
this.hass,
|
this.hass,
|
||||||
returnedToGrid,
|
returnedToGrid,
|
||||||
"kWh"
|
"kWh",
|
||||||
|
targetEnergyUnit
|
||||||
)}
|
)}
|
||||||
</span>`
|
</span>`
|
||||||
: ""}
|
: ""}
|
||||||
@ -409,7 +426,8 @@ class HuiEnergyDistrubutionCard
|
|||||||
: ""}${formatConsumptionShort(
|
: ""}${formatConsumptionShort(
|
||||||
this.hass,
|
this.hass,
|
||||||
totalFromGrid,
|
totalFromGrid,
|
||||||
"kWh"
|
"kWh",
|
||||||
|
targetEnergyUnit
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
@ -432,7 +450,8 @@ class HuiEnergyDistrubutionCard
|
|||||||
${formatConsumptionShort(
|
${formatConsumptionShort(
|
||||||
this.hass,
|
this.hass,
|
||||||
totalHomeConsumption,
|
totalHomeConsumption,
|
||||||
"kWh"
|
"kWh",
|
||||||
|
targetEnergyUnit
|
||||||
)}
|
)}
|
||||||
${homeSolarCircumference !== undefined ||
|
${homeSolarCircumference !== undefined ||
|
||||||
homeLowCarbonCircumference !== undefined
|
homeLowCarbonCircumference !== undefined
|
||||||
@ -535,7 +554,8 @@ class HuiEnergyDistrubutionCard
|
|||||||
>${formatConsumptionShort(
|
>${formatConsumptionShort(
|
||||||
this.hass,
|
this.hass,
|
||||||
totalBatteryIn,
|
totalBatteryIn,
|
||||||
"kWh"
|
"kWh",
|
||||||
|
targetEnergyUnit
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
<span class="battery-out">
|
<span class="battery-out">
|
||||||
@ -546,7 +566,8 @@ class HuiEnergyDistrubutionCard
|
|||||||
>${formatConsumptionShort(
|
>${formatConsumptionShort(
|
||||||
this.hass,
|
this.hass,
|
||||||
totalBatteryOut,
|
totalBatteryOut,
|
||||||
"kWh"
|
"kWh",
|
||||||
|
targetEnergyUnit
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
@ -70,8 +70,10 @@ describe("Energy Short Format Test", () => {
|
|||||||
const hass = { locale: defaultLocale } as HomeAssistant;
|
const hass = { locale: defaultLocale } as HomeAssistant;
|
||||||
it("No Unit conversion", () => {
|
it("No Unit conversion", () => {
|
||||||
assert.strictEqual(formatConsumptionShort(hass, 0, "Wh"), "0 Wh");
|
assert.strictEqual(formatConsumptionShort(hass, 0, "Wh"), "0 Wh");
|
||||||
assert.strictEqual(formatConsumptionShort(hass, 0, "kWh"), "0 kWh");
|
assert.strictEqual(formatConsumptionShort(hass, 0, "kWh"), "0 Wh");
|
||||||
assert.strictEqual(formatConsumptionShort(hass, 0, "GWh"), "0 GWh");
|
assert.strictEqual(formatConsumptionShort(hass, 0, "kWh", "kWh"), "0 kWh");
|
||||||
|
assert.strictEqual(formatConsumptionShort(hass, 0, "GWh"), "0 Wh");
|
||||||
|
assert.strictEqual(formatConsumptionShort(hass, 0, "GWh", "GWh"), "0 GWh");
|
||||||
assert.strictEqual(formatConsumptionShort(hass, 0, "gal"), "0 gal");
|
assert.strictEqual(formatConsumptionShort(hass, 0, "gal"), "0 gal");
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
@ -139,6 +141,36 @@ describe("Energy Short Format Test", () => {
|
|||||||
"-1.23 Wh"
|
"-1.23 Wh"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
it("Conversion with target unit", () => {
|
||||||
|
assert.strictEqual(
|
||||||
|
formatConsumptionShort(hass, 0.00012, "kWh", "Wh"),
|
||||||
|
"0.12 Wh"
|
||||||
|
);
|
||||||
|
assert.strictEqual(
|
||||||
|
formatConsumptionShort(hass, 0.00012, "kWh", "kWh"),
|
||||||
|
"0 kWh"
|
||||||
|
);
|
||||||
|
assert.strictEqual(
|
||||||
|
formatConsumptionShort(hass, 0.01012, "kWh", "kWh"),
|
||||||
|
"0.01 kWh"
|
||||||
|
);
|
||||||
|
assert.strictEqual(
|
||||||
|
formatConsumptionShort(hass, 0.00012, "kWh", "MWh"),
|
||||||
|
"0 MWh"
|
||||||
|
);
|
||||||
|
assert.strictEqual(
|
||||||
|
formatConsumptionShort(hass, 10.12345, "kWh", "kWh"),
|
||||||
|
"10.1 kWh"
|
||||||
|
);
|
||||||
|
assert.strictEqual(
|
||||||
|
formatConsumptionShort(hass, 10.12345, "kWh", "ZZZZZWh"),
|
||||||
|
"10.1 kWh"
|
||||||
|
);
|
||||||
|
assert.strictEqual(
|
||||||
|
formatConsumptionShort(hass, 151234.5678, "kWh", "MWh"),
|
||||||
|
"151 MWh"
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Energy Usage Calculation Tests", () => {
|
describe("Energy Usage Calculation Tests", () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user