From a325d32d091e98939b33f5fc78299a08b3d96b51 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 11 Feb 2023 16:43:20 -0600 Subject: [PATCH] Fix stats data being fetched for all entities when there are no energy/water stat ids (#15428) * Fix stats data being fetched for all entities when there are no energy/water stat ids I noticed the following query was being sent to the stats api since there were no water stat ids. Since it sent an empty list it enumerated everything ``` { type: "recorder/statistics_during_period", start_time: "2023-02-05T05:00:00.000Z", end_time: "2023-02-12T05:59:59.999Z" .... statistic_ids: [], type: "recorder/statistics_during_period" types: ["sum"] units: {volume: "gal"} } ``` * not python --- src/data/energy.ts | 80 +++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/src/data/energy.ts b/src/data/energy.ts index ba369bded0..1584d385c5 100644 --- a/src/data/energy.ts +++ b/src/data/energy.ts @@ -406,24 +406,28 @@ const getEnergyData = async ( }; const stats = { - ...(await fetchStatistics( - hass!, - startMinHour, - end, - energyStatIds, - period, - energyUnits, - ["sum"] - )), - ...(await fetchStatistics( - hass!, - startMinHour, - end, - waterStatIds, - period, - waterUnits, - ["sum"] - )), + ...(energyStatIds.length + ? await fetchStatistics( + hass!, + startMinHour, + end, + energyStatIds, + period, + energyUnits, + ["sum"] + ) + : {}), + ...(waterStatIds.length + ? await fetchStatistics( + hass!, + startMinHour, + end, + waterStatIds, + period, + waterUnits, + ["sum"] + ) + : {}), }; let statsCompare; @@ -441,24 +445,28 @@ const getEnergyData = async ( endCompare = addMilliseconds(start, -1); statsCompare = { - ...(await fetchStatistics( - hass!, - compareStartMinHour, - endCompare, - energyStatIds, - period, - energyUnits, - ["sum"] - )), - ...(await fetchStatistics( - hass!, - compareStartMinHour, - endCompare, - waterStatIds, - period, - waterUnits, - ["sum"] - )), + ...(energyStatIds.length + ? await fetchStatistics( + hass!, + compareStartMinHour, + endCompare, + energyStatIds, + period, + energyUnits, + ["sum"] + ) + : {}), + ...(waterStatIds.length + ? await fetchStatistics( + hass!, + compareStartMinHour, + endCompare, + waterStatIds, + period, + waterUnits, + ["sum"] + ) + : {}), }; }