Change water units to L/gal (#14223)

This commit is contained in:
Bram Kragten 2022-10-27 23:10:35 +02:00 committed by GitHub
parent 03d03f9903
commit 449c1f2469
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 22 deletions

View File

@ -272,11 +272,15 @@ export interface EnergyData {
export const getReferencedStatisticIds = ( export const getReferencedStatisticIds = (
prefs: EnergyPreferences, prefs: EnergyPreferences,
info: EnergyInfo info: EnergyInfo,
exclude?: string[]
): string[] => { ): string[] => {
const statIDs: string[] = []; const statIDs: string[] = [];
for (const source of prefs.energy_sources) { for (const source of prefs.energy_sources) {
if (exclude?.includes(source.type)) {
continue;
}
if (source.type === "solar") { if (source.type === "solar") {
statIDs.push(source.stat_energy_from); statIDs.push(source.stat_energy_from);
continue; continue;
@ -362,6 +366,7 @@ const getEnergyData = async (
} }
} }
const waterStatIds: string[] = [];
const consumptionStatIDs: string[] = []; const consumptionStatIDs: string[] = [];
for (const source of prefs.energy_sources) { for (const source of prefs.energy_sources) {
// grid source // grid source
@ -370,8 +375,13 @@ const getEnergyData = async (
consumptionStatIDs.push(flowFrom.stat_energy_from); consumptionStatIDs.push(flowFrom.stat_energy_from);
} }
} }
if (source.type === "water") {
waterStatIds.push(source.stat_energy_from);
} }
const statIDs = getReferencedStatisticIds(prefs, info); }
const energyStatIds = getReferencedStatisticIds(prefs, info, ["water"]);
const allStatIDs = [...energyStatIds, ...waterStatIds];
const dayDifference = differenceInDays(end || new Date(), start); const dayDifference = differenceInDays(end || new Date(), start);
const period = const period =
@ -381,19 +391,32 @@ const getEnergyData = async (
const startMinHour = addHours(start, -1); const startMinHour = addHours(start, -1);
const lengthUnit = hass.config.unit_system.length || ""; const lengthUnit = hass.config.unit_system.length || "";
const units: StatisticsUnitConfiguration = { const energyUnits: StatisticsUnitConfiguration = {
energy: "kWh", energy: "kWh",
volume: lengthUnit === "km" ? "m³" : "ft³", volume: lengthUnit === "km" ? "m³" : "ft³",
}; };
const waterUnits: StatisticsUnitConfiguration = {
volume: lengthUnit === "km" ? "L" : "gal",
};
const stats = await fetchStatistics( const stats = {
...(await fetchStatistics(
hass!, hass!,
startMinHour, startMinHour,
end, end,
statIDs, energyStatIds,
period, period,
units energyUnits
); )),
...(await fetchStatistics(
hass!,
startMinHour,
end,
waterStatIds,
period,
waterUnits
)),
};
let statsCompare; let statsCompare;
let startCompare; let startCompare;
@ -409,14 +432,24 @@ const getEnergyData = async (
const compareStartMinHour = addHours(startCompare, -1); const compareStartMinHour = addHours(startCompare, -1);
endCompare = addMilliseconds(start, -1); endCompare = addMilliseconds(start, -1);
statsCompare = await fetchStatistics( statsCompare = {
...(await fetchStatistics(
hass!, hass!,
compareStartMinHour, compareStartMinHour,
endCompare, endCompare,
statIDs, energyStatIds,
period, period,
units energyUnits
); )),
...(await fetchStatistics(
hass!,
startMinHour,
end,
waterStatIds,
period,
waterUnits
)),
};
} }
let fossilEnergyConsumption: FossilEnergyConsumption | undefined; let fossilEnergyConsumption: FossilEnergyConsumption | undefined;
@ -456,7 +489,7 @@ const getEnergyData = async (
} }
}); });
const statsMetadataArray = await getStatisticMetadata(hass, statIDs); const statsMetadataArray = await getStatisticMetadata(hass, allStatIDs);
const statsMetadata: Record<string, StatisticsMetaData> = {}; const statsMetadata: Record<string, StatisticsMetaData> = {};
statsMetadataArray.forEach((x) => { statsMetadataArray.forEach((x) => {
statsMetadata[x.statistic_id] = x; statsMetadata[x.statistic_id] = x;
@ -671,4 +704,4 @@ export const getEnergyGasUnit = (
}; };
export const getEnergyWaterUnit = (hass: HomeAssistant): string | undefined => export const getEnergyWaterUnit = (hass: HomeAssistant): string | undefined =>
hass.config.unit_system.length === "km" ? "m³" : "ft³"; hass.config.unit_system.length === "km" ? "L" : "gal";

View File

@ -88,7 +88,7 @@ export interface StatisticsUnitConfiguration {
| "psi" | "psi"
| "mmHg"; | "mmHg";
temperature?: "°C" | "°F" | "K"; temperature?: "°C" | "°F" | "K";
volume?: "ft³" | "m³"; volume?: "L" | "gal" | "ft³" | "m³";
} }
export interface StatisticsValidationResults { export interface StatisticsValidationResults {