Render energy-gas in the display unit of the sources (#26143)

This commit is contained in:
karwosts 2025-07-13 22:34:53 -07:00 committed by GitHub
parent 1a316d251e
commit 80b86a89f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 31 deletions

View File

@ -281,6 +281,7 @@ export interface EnergyData {
fossilEnergyConsumption?: FossilEnergyConsumption; fossilEnergyConsumption?: FossilEnergyConsumption;
fossilEnergyConsumptionCompare?: FossilEnergyConsumption; fossilEnergyConsumptionCompare?: FossilEnergyConsumption;
waterUnit: string; waterUnit: string;
gasUnit: string;
} }
export const getReferencedStatisticIds = ( export const getReferencedStatisticIds = (
@ -403,8 +404,6 @@ const getEnergyData = async (
? "day" ? "day"
: "hour"; : "hour";
const lengthUnit = hass.config.unit_system.length || "";
const statsMetadata: Record<string, StatisticsMetaData> = {}; const statsMetadata: Record<string, StatisticsMetaData> = {};
const statsMetadataArray = allStatIDs.length const statsMetadataArray = allStatIDs.length
? await getStatisticMetadata(hass, allStatIDs) ? await getStatisticMetadata(hass, allStatIDs)
@ -416,9 +415,14 @@ const getEnergyData = async (
}); });
} }
const gasUnit = getEnergyGasUnit(hass, prefs, statsMetadata);
const gasIsVolume = VOLUME_UNITS.includes(gasUnit as any);
const energyUnits: StatisticsUnitConfiguration = { const energyUnits: StatisticsUnitConfiguration = {
energy: "kWh", energy: "kWh",
volume: lengthUnit === "km" ? "m³" : "ft³", volume: gasIsVolume
? (gasUnit as (typeof VOLUME_UNITS)[number])
: undefined,
}; };
const waterUnit = getEnergyWaterUnit(hass, prefs, statsMetadata); const waterUnit = getEnergyWaterUnit(hass, prefs, statsMetadata);
const waterUnits: StatisticsUnitConfiguration = { const waterUnits: StatisticsUnitConfiguration = {
@ -564,6 +568,7 @@ const getEnergyData = async (
fossilEnergyConsumption, fossilEnergyConsumption,
fossilEnergyConsumptionCompare, fossilEnergyConsumptionCompare,
waterUnit, waterUnit,
gasUnit,
}; };
return data; return data;
@ -772,7 +777,7 @@ export const getEnergyGasUnitClass = (
return undefined; return undefined;
}; };
export const getEnergyGasUnit = ( const getEnergyGasUnit = (
hass: HomeAssistant, hass: HomeAssistant,
prefs: EnergyPreferences, prefs: EnergyPreferences,
statisticsMetaData: Record<string, StatisticsMetaData> = {} statisticsMetaData: Record<string, StatisticsMetaData> = {}
@ -782,6 +787,25 @@ export const getEnergyGasUnit = (
return "kWh"; return "kWh";
} }
const units = prefs.energy_sources
.filter((s) => s.type === "gas")
.map((s) =>
getDisplayUnit(
hass,
s.stat_energy_from,
statisticsMetaData[s.stat_energy_from]
)
);
if (units.length) {
const first = units[0];
if (
VOLUME_UNITS.includes(first as any) &&
units.every((u) => u === first)
) {
return first as (typeof VOLUME_UNITS)[number];
}
}
return hass.config.unit_system.length === "km" ? "m³" : "ft³"; return hass.config.unit_system.length === "km" ? "m³" : "ft³";
}; };

View File

@ -24,7 +24,6 @@ import type {
import { import {
computeConsumptionData, computeConsumptionData,
getEnergyDataCollection, getEnergyDataCollection,
getEnergyGasUnit,
getSummedData, getSummedData,
} from "../../data/energy"; } from "../../data/energy";
import { fileDownload } from "../../util/file_download"; import { fileDownload } from "../../util/file_download";
@ -152,11 +151,7 @@ class PanelEnergy extends LitElement {
return; return;
} }
const gasUnit = getEnergyGasUnit( const gasUnit = energyData.state.gasUnit;
this.hass,
energyData.prefs,
energyData.state.statsMetadata
);
const electricUnit = "kWh"; const electricUnit = "kWh";
const energy_sources = energyData.prefs.energy_sources; const energy_sources = energyData.prefs.energy_sources;

View File

@ -23,7 +23,6 @@ import type { EnergyData } from "../../../../data/energy";
import { import {
energySourcesByType, energySourcesByType,
getEnergyDataCollection, getEnergyDataCollection,
getEnergyGasUnit,
formatConsumptionShort, formatConsumptionShort,
getSummedData, getSummedData,
computeConsumptionData, computeConsumptionData,
@ -334,11 +333,7 @@ class HuiEnergyDistrubutionCard
${formatConsumptionShort( ${formatConsumptionShort(
this.hass, this.hass,
gasUsage, gasUsage,
getEnergyGasUnit( this._data.gasUnit
this.hass,
prefs,
this._data.statsMetadata
)
)} )}
</div> </div>
<svg width="80" height="30"> <svg width="80" height="30">

View File

@ -14,10 +14,7 @@ import type {
EnergyData, EnergyData,
GasSourceTypeEnergyPreference, GasSourceTypeEnergyPreference,
} from "../../../../data/energy"; } from "../../../../data/energy";
import { import { getEnergyDataCollection } from "../../../../data/energy";
getEnergyDataCollection,
getEnergyGasUnit,
} from "../../../../data/energy";
import type { Statistics, StatisticsMetaData } from "../../../../data/recorder"; import type { Statistics, StatisticsMetaData } from "../../../../data/recorder";
import { getStatisticLabel } from "../../../../data/recorder"; import { getStatisticLabel } from "../../../../data/recorder";
import type { FrontendLocaleData } from "../../../../data/translation"; import type { FrontendLocaleData } from "../../../../data/translation";
@ -163,11 +160,7 @@ export class HuiEnergyGasGraphCard
(source) => source.type === "gas" (source) => source.type === "gas"
) as GasSourceTypeEnergyPreference[]; ) as GasSourceTypeEnergyPreference[];
this._unit = getEnergyGasUnit( this._unit = energyData.gasUnit;
this.hass,
energyData.prefs,
energyData.statsMetadata
);
const datasets: BarSeriesOption[] = []; const datasets: BarSeriesOption[] = [];

View File

@ -13,7 +13,6 @@ import type { EnergyData } from "../../../../data/energy";
import { import {
energySourcesByType, energySourcesByType,
getEnergyDataCollection, getEnergyDataCollection,
getEnergyGasUnit,
} from "../../../../data/energy"; } from "../../../../data/energy";
import { import {
calculateStatisticSumGrowth, calculateStatisticSumGrowth,
@ -133,11 +132,7 @@ export class HuiEnergySourcesTableCard
flow.stat_cost || flow.entity_energy_price || flow.number_energy_price flow.stat_cost || flow.entity_energy_price || flow.number_energy_price
); );
const gasUnit = getEnergyGasUnit( const gasUnit = this._data.gasUnit;
this.hass,
this._data.prefs,
this._data.statsMetadata
);
const waterUnit = this._data.waterUnit; const waterUnit = this._data.waterUnit;