Skip to content

Commit 49bed5e

Browse files
authored
Standardize all energy period calculations (#28827)
1 parent b84a512 commit 49bed5e

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

src/data/energy.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,16 +449,9 @@ const getEnergyData = async (
449449
const allStatIDs = [...energyStatIds, ...waterStatIds, ...powerStatIds];
450450

451451
const dayDifference = differenceInDays(end || new Date(), start);
452-
const period =
453-
isFirstDayOfMonth(start) &&
454-
(!end || isLastDayOfMonth(end)) &&
455-
dayDifference > 35
456-
? "month"
457-
: dayDifference > 2
458-
? "day"
459-
: "hour";
460-
const finePeriod =
461-
dayDifference > 64 ? "day" : dayDifference > 8 ? "hour" : "5minute";
452+
453+
const period = getSuggestedPeriod(start, end);
454+
const finePeriod = getSuggestedPeriod(start, end, true);
462455

463456
const statsMetadata: Record<string, StatisticsMetaData> = {};
464457
const statsMetadataArray = allStatIDs.length
@@ -589,7 +582,7 @@ const getEnergyData = async (
589582
consumptionStatIDs,
590583
co2SignalEntity,
591584
end,
592-
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
585+
period
593586
);
594587
if (compare) {
595588
_fossilEnergyConsumptionCompare = getFossilEnergyConsumption(
@@ -598,7 +591,7 @@ const getEnergyData = async (
598591
consumptionStatIDs,
599592
co2SignalEntity,
600593
endCompare,
601-
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
594+
period
602595
);
603596
}
604597
}
@@ -1427,3 +1420,22 @@ export const formatPowerShort = (
14271420
units[unitIndex]
14281421
);
14291422
};
1423+
1424+
export function getSuggestedPeriod(
1425+
start: Date,
1426+
end?: Date,
1427+
fine = false
1428+
): "5minute" | "hour" | "day" | "month" {
1429+
const dayDifference = differenceInDays(end || new Date(), start);
1430+
1431+
if (fine) {
1432+
return dayDifference > 64 ? "day" : dayDifference > 8 ? "hour" : "5minute";
1433+
}
1434+
return isFirstDayOfMonth(start) &&
1435+
(!end || isLastDayOfMonth(end)) &&
1436+
dayDifference > 35
1437+
? "month"
1438+
: dayDifference > 2
1439+
? "day"
1440+
: "hour";
1441+
}

src/panels/lovelace/cards/energy/common/energy-chart-options.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { formatTime } from "../../../../../common/datetime/format_time";
3131
import type { ECOption } from "../../../../../resources/echarts/echarts";
3232
import { filterXSS } from "../../../../../common/util/xss";
3333
import type { StatisticPeriod } from "../../../../../data/recorder";
34+
import { getSuggestedPeriod } from "../../../../../data/energy";
3435

3536
export function getSuggestedMax(period: StatisticPeriod, end: Date): number {
3637
let suggestedMax = new Date(end);
@@ -56,10 +57,6 @@ export function getSuggestedMax(period: StatisticPeriod, end: Date): number {
5657
return suggestedMax.getTime();
5758
}
5859

59-
export function getSuggestedPeriod(dayDifference: number): StatisticPeriod {
60-
return dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour";
61-
}
62-
6360
function createYAxisLabelFormatter(locale: FrontendLocaleData) {
6461
let previousValue: number | undefined;
6562

@@ -95,7 +92,7 @@ export function getCommonOptions(
9592
type: "time",
9693
min: start,
9794
max: getSuggestedMax(
98-
detailedDailyData ? "5minute" : getSuggestedPeriod(dayDifference),
95+
getSuggestedPeriod(start, end, detailedDailyData),
9996
end
10097
),
10198
},

src/panels/lovelace/cards/energy/hui-energy-solar-graph-card.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { differenceInDays, endOfToday, isToday, startOfToday } from "date-fns";
1+
import { endOfToday, isToday, startOfToday } from "date-fns";
22
import type { HassConfig, UnsubscribeFunc } from "home-assistant-js-websocket";
33
import type { PropertyValues } from "lit";
44
import { css, html, LitElement, nothing } from "lit";
@@ -18,6 +18,7 @@ import type {
1818
import {
1919
getEnergyDataCollection,
2020
getEnergySolarForecasts,
21+
getSuggestedPeriod,
2122
} from "../../../../data/energy";
2223
import type { Statistics, StatisticsMetaData } from "../../../../data/recorder";
2324
import { getStatisticLabel } from "../../../../data/recorder";
@@ -354,7 +355,7 @@ export class HuiEnergySolarGraphCard
354355
) {
355356
const data: LineSeriesOption[] = [];
356357

357-
const dayDifference = differenceInDays(end || new Date(), start);
358+
const period = getSuggestedPeriod(start, end);
358359

359360
// Process solar forecast data.
360361
solarSources.forEach((source) => {
@@ -370,10 +371,10 @@ export class HuiEnergySolarGraphCard
370371
if (dateObj < start || (end && dateObj > end)) {
371372
return;
372373
}
373-
if (dayDifference > 35) {
374+
if (period === "month") {
374375
dateObj.setDate(1);
375376
}
376-
if (dayDifference > 2) {
377+
if (period === "month" || period === "day") {
377378
dateObj.setHours(0, 0, 0, 0);
378379
} else {
379380
dateObj.setMinutes(0, 0, 0);

src/panels/lovelace/cards/hui-statistics-graph-card.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { createSearchParam } from "../../../common/url/search-params";
88
import "../../../components/ha-card";
99
import "../../../components/ha-icon-next";
1010
import "../../../components/ha-tooltip";
11-
import { getEnergyDataCollection } from "../../../data/energy";
11+
import {
12+
getEnergyDataCollection,
13+
getSuggestedPeriod,
14+
} from "../../../data/energy";
1215
import type {
1316
Statistics,
1417
StatisticsMetaData,
@@ -26,10 +29,7 @@ import { hasConfigOrEntitiesChanged } from "../common/has-changed";
2629
import { processConfigEntities } from "../common/process-config-entities";
2730
import type { EntityConfig } from "../entity-rows/types";
2831
import type { LovelaceCard, LovelaceGridOptions } from "../types";
29-
import {
30-
getSuggestedMax,
31-
getSuggestedPeriod,
32-
} from "./energy/common/energy-chart-options";
32+
import { getSuggestedMax } from "./energy/common/energy-chart-options";
3333
import type { StatisticsGraphCardConfig } from "./types";
3434

3535
export const DEFAULT_DAYS_TO_SHOW = 30;
@@ -268,9 +268,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
268268
return (
269269
this._config?.period ??
270270
(this._energyStart && this._energyEnd
271-
? getSuggestedPeriod(
272-
differenceInDays(this._energyEnd, this._energyStart)
273-
)
271+
? getSuggestedPeriod(this._energyStart, this._energyEnd)
274272
: undefined)
275273
);
276274
}

0 commit comments

Comments
 (0)