mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-17 06:16:33 +00:00
Avoid fetching all stats metadata when there are no entities on the energy panel (#15591)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
6af57fa2cd
commit
20f2f5b317
@ -11,10 +11,8 @@ import {
|
|||||||
} from "date-fns/esm";
|
} from "date-fns/esm";
|
||||||
import { Collection, getCollection } from "home-assistant-js-websocket";
|
import { Collection, getCollection } from "home-assistant-js-websocket";
|
||||||
import { groupBy } from "../common/util/group-by";
|
import { groupBy } from "../common/util/group-by";
|
||||||
import { subscribeOne } from "../common/util/subscribe-one";
|
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
import { ConfigEntry, getConfigEntries } from "./config_entries";
|
import { ConfigEntry, getConfigEntries } from "./config_entries";
|
||||||
import { subscribeEntityRegistry } from "./entity_registry";
|
|
||||||
import {
|
import {
|
||||||
fetchStatistics,
|
fetchStatistics,
|
||||||
getStatisticMetadata,
|
getStatisticMetadata,
|
||||||
@ -341,9 +339,8 @@ const getEnergyData = async (
|
|||||||
end?: Date,
|
end?: Date,
|
||||||
compare?: boolean
|
compare?: boolean
|
||||||
): Promise<EnergyData> => {
|
): Promise<EnergyData> => {
|
||||||
const [configEntries, entityRegistryEntries, info] = await Promise.all([
|
const [configEntries, info] = await Promise.all([
|
||||||
getConfigEntries(hass, { domain: "co2signal" }),
|
getConfigEntries(hass, { domain: "co2signal" }),
|
||||||
subscribeOne(hass.connection, subscribeEntityRegistry),
|
|
||||||
getEnergyInfo(hass),
|
getEnergyInfo(hass),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -352,15 +349,14 @@ const getEnergyData = async (
|
|||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
let co2SignalEntity: string | undefined;
|
let co2SignalEntity: string | undefined;
|
||||||
|
|
||||||
if (co2SignalConfigEntry) {
|
if (co2SignalConfigEntry) {
|
||||||
for (const entry of entityRegistryEntries) {
|
for (const entity of Object.values(hass.entities)) {
|
||||||
if (entry.config_entry_id !== co2SignalConfigEntry.entry_id) {
|
if (entity.platform !== "co2signal") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The integration offers 2 entities. We want the % one.
|
// The integration offers 2 entities. We want the % one.
|
||||||
const co2State = hass.states[entry.entity_id];
|
const co2State = hass.states[entity.entity_id];
|
||||||
if (!co2State || co2State.attributes.unit_of_measurement !== "%") {
|
if (!co2State || co2State.attributes.unit_of_measurement !== "%") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -405,34 +401,35 @@ const getEnergyData = async (
|
|||||||
volume: lengthUnit === "km" ? "L" : "gal",
|
volume: lengthUnit === "km" ? "L" : "gal",
|
||||||
};
|
};
|
||||||
|
|
||||||
const stats = {
|
const _energyStats: Statistics | Promise<Statistics> = energyStatIds.length
|
||||||
...(energyStatIds.length
|
? fetchStatistics(
|
||||||
? await fetchStatistics(
|
hass!,
|
||||||
hass!,
|
startMinHour,
|
||||||
startMinHour,
|
end,
|
||||||
end,
|
energyStatIds,
|
||||||
energyStatIds,
|
period,
|
||||||
period,
|
energyUnits,
|
||||||
energyUnits,
|
["sum"]
|
||||||
["sum"]
|
)
|
||||||
)
|
: {};
|
||||||
: {}),
|
const _waterStats: Statistics | Promise<Statistics> = waterStatIds.length
|
||||||
...(waterStatIds.length
|
? fetchStatistics(
|
||||||
? await fetchStatistics(
|
hass!,
|
||||||
hass!,
|
startMinHour,
|
||||||
startMinHour,
|
end,
|
||||||
end,
|
waterStatIds,
|
||||||
waterStatIds,
|
period,
|
||||||
period,
|
waterUnits,
|
||||||
waterUnits,
|
["sum"]
|
||||||
["sum"]
|
)
|
||||||
)
|
: {};
|
||||||
: {}),
|
|
||||||
};
|
|
||||||
|
|
||||||
let statsCompare;
|
let statsCompare;
|
||||||
let startCompare;
|
let startCompare;
|
||||||
let endCompare;
|
let endCompare;
|
||||||
|
let _energyStatsCompare: Statistics | Promise<Statistics> = {};
|
||||||
|
let _waterStatsCompare: Statistics | Promise<Statistics> = {};
|
||||||
|
|
||||||
if (compare) {
|
if (compare) {
|
||||||
if (dayDifference > 27 && dayDifference < 32) {
|
if (dayDifference > 27 && dayDifference < 32) {
|
||||||
// When comparing a month, we want to start at the begining of the month
|
// When comparing a month, we want to start at the begining of the month
|
||||||
@ -443,38 +440,36 @@ const getEnergyData = async (
|
|||||||
|
|
||||||
const compareStartMinHour = addHours(startCompare, -1);
|
const compareStartMinHour = addHours(startCompare, -1);
|
||||||
endCompare = addMilliseconds(start, -1);
|
endCompare = addMilliseconds(start, -1);
|
||||||
|
if (energyStatIds.length) {
|
||||||
statsCompare = {
|
_energyStatsCompare = fetchStatistics(
|
||||||
...(energyStatIds.length
|
hass!,
|
||||||
? await fetchStatistics(
|
compareStartMinHour,
|
||||||
hass!,
|
endCompare,
|
||||||
compareStartMinHour,
|
energyStatIds,
|
||||||
endCompare,
|
period,
|
||||||
energyStatIds,
|
energyUnits,
|
||||||
period,
|
["sum"]
|
||||||
energyUnits,
|
);
|
||||||
["sum"]
|
}
|
||||||
)
|
if (waterStatIds.length) {
|
||||||
: {}),
|
_waterStatsCompare = fetchStatistics(
|
||||||
...(waterStatIds.length
|
hass!,
|
||||||
? await fetchStatistics(
|
compareStartMinHour,
|
||||||
hass!,
|
endCompare,
|
||||||
compareStartMinHour,
|
waterStatIds,
|
||||||
endCompare,
|
period,
|
||||||
waterStatIds,
|
waterUnits,
|
||||||
period,
|
["sum"]
|
||||||
waterUnits,
|
);
|
||||||
["sum"]
|
}
|
||||||
)
|
|
||||||
: {}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let fossilEnergyConsumption: FossilEnergyConsumption | undefined;
|
let _fossilEnergyConsumption: undefined | Promise<FossilEnergyConsumption>;
|
||||||
let fossilEnergyConsumptionCompare: FossilEnergyConsumption | undefined;
|
let _fossilEnergyConsumptionCompare:
|
||||||
|
| undefined
|
||||||
|
| Promise<FossilEnergyConsumption>;
|
||||||
if (co2SignalEntity !== undefined) {
|
if (co2SignalEntity !== undefined) {
|
||||||
fossilEnergyConsumption = await getFossilEnergyConsumption(
|
_fossilEnergyConsumption = getFossilEnergyConsumption(
|
||||||
hass!,
|
hass!,
|
||||||
start,
|
start,
|
||||||
consumptionStatIDs,
|
consumptionStatIDs,
|
||||||
@ -483,7 +478,7 @@ const getEnergyData = async (
|
|||||||
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
|
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
|
||||||
);
|
);
|
||||||
if (compare) {
|
if (compare) {
|
||||||
fossilEnergyConsumptionCompare = await getFossilEnergyConsumption(
|
_fossilEnergyConsumptionCompare = getFossilEnergyConsumption(
|
||||||
hass!,
|
hass!,
|
||||||
startCompare,
|
startCompare,
|
||||||
consumptionStatIDs,
|
consumptionStatIDs,
|
||||||
@ -494,6 +489,39 @@ const getEnergyData = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const statsMetadata: Record<string, StatisticsMetaData> = {};
|
||||||
|
const _getStatisticMetadata:
|
||||||
|
| Promise<StatisticsMetaData[]>
|
||||||
|
| StatisticsMetaData[] = allStatIDs.length
|
||||||
|
? getStatisticMetadata(hass, allStatIDs)
|
||||||
|
: [];
|
||||||
|
const [
|
||||||
|
energyStats,
|
||||||
|
waterStats,
|
||||||
|
energyStatsCompare,
|
||||||
|
waterStatsCompare,
|
||||||
|
statsMetadataArray,
|
||||||
|
fossilEnergyConsumption,
|
||||||
|
fossilEnergyConsumptionCompare,
|
||||||
|
] = await Promise.all([
|
||||||
|
_energyStats,
|
||||||
|
_waterStats,
|
||||||
|
_energyStatsCompare,
|
||||||
|
_waterStatsCompare,
|
||||||
|
_getStatisticMetadata,
|
||||||
|
_fossilEnergyConsumption,
|
||||||
|
_fossilEnergyConsumptionCompare,
|
||||||
|
]);
|
||||||
|
const stats = { ...energyStats, ...waterStats };
|
||||||
|
if (compare) {
|
||||||
|
statsCompare = { ...energyStatsCompare, ...waterStatsCompare };
|
||||||
|
}
|
||||||
|
if (allStatIDs.length) {
|
||||||
|
statsMetadataArray.forEach((x) => {
|
||||||
|
statsMetadata[x.statistic_id] = x;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Object.values(stats).forEach((stat) => {
|
Object.values(stats).forEach((stat) => {
|
||||||
// if the start of the first value is after the requested period, we have the first data point, and should add a zero point
|
// if the start of the first value is after the requested period, we have the first data point, and should add a zero point
|
||||||
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
||||||
@ -507,12 +535,6 @@ const getEnergyData = async (
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const statsMetadataArray = await getStatisticMetadata(hass, allStatIDs);
|
|
||||||
const statsMetadata: Record<string, StatisticsMetaData> = {};
|
|
||||||
statsMetadataArray.forEach((x) => {
|
|
||||||
statsMetadata[x.statistic_id] = x;
|
|
||||||
});
|
|
||||||
|
|
||||||
const data: EnergyData = {
|
const data: EnergyData = {
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user