mirror of
https://github.com/home-assistant/frontend.git
synced 2026-02-04 01:10:33 -06:00
Standardize all energy period calculations (#28827)
This commit is contained in:
parent
b84a51235d
commit
49bed5e6a6
@ -449,16 +449,9 @@ const getEnergyData = async (
|
||||
const allStatIDs = [...energyStatIds, ...waterStatIds, ...powerStatIds];
|
||||
|
||||
const dayDifference = differenceInDays(end || new Date(), start);
|
||||
const period =
|
||||
isFirstDayOfMonth(start) &&
|
||||
(!end || isLastDayOfMonth(end)) &&
|
||||
dayDifference > 35
|
||||
? "month"
|
||||
: dayDifference > 2
|
||||
? "day"
|
||||
: "hour";
|
||||
const finePeriod =
|
||||
dayDifference > 64 ? "day" : dayDifference > 8 ? "hour" : "5minute";
|
||||
|
||||
const period = getSuggestedPeriod(start, end);
|
||||
const finePeriod = getSuggestedPeriod(start, end, true);
|
||||
|
||||
const statsMetadata: Record<string, StatisticsMetaData> = {};
|
||||
const statsMetadataArray = allStatIDs.length
|
||||
@ -589,7 +582,7 @@ const getEnergyData = async (
|
||||
consumptionStatIDs,
|
||||
co2SignalEntity,
|
||||
end,
|
||||
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
|
||||
period
|
||||
);
|
||||
if (compare) {
|
||||
_fossilEnergyConsumptionCompare = getFossilEnergyConsumption(
|
||||
@ -598,7 +591,7 @@ const getEnergyData = async (
|
||||
consumptionStatIDs,
|
||||
co2SignalEntity,
|
||||
endCompare,
|
||||
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"
|
||||
period
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1427,3 +1420,22 @@ export const formatPowerShort = (
|
||||
units[unitIndex]
|
||||
);
|
||||
};
|
||||
|
||||
export function getSuggestedPeriod(
|
||||
start: Date,
|
||||
end?: Date,
|
||||
fine = false
|
||||
): "5minute" | "hour" | "day" | "month" {
|
||||
const dayDifference = differenceInDays(end || new Date(), start);
|
||||
|
||||
if (fine) {
|
||||
return dayDifference > 64 ? "day" : dayDifference > 8 ? "hour" : "5minute";
|
||||
}
|
||||
return isFirstDayOfMonth(start) &&
|
||||
(!end || isLastDayOfMonth(end)) &&
|
||||
dayDifference > 35
|
||||
? "month"
|
||||
: dayDifference > 2
|
||||
? "day"
|
||||
: "hour";
|
||||
}
|
||||
|
||||
@ -31,6 +31,7 @@ import { formatTime } from "../../../../../common/datetime/format_time";
|
||||
import type { ECOption } from "../../../../../resources/echarts/echarts";
|
||||
import { filterXSS } from "../../../../../common/util/xss";
|
||||
import type { StatisticPeriod } from "../../../../../data/recorder";
|
||||
import { getSuggestedPeriod } from "../../../../../data/energy";
|
||||
|
||||
export function getSuggestedMax(period: StatisticPeriod, end: Date): number {
|
||||
let suggestedMax = new Date(end);
|
||||
@ -56,10 +57,6 @@ export function getSuggestedMax(period: StatisticPeriod, end: Date): number {
|
||||
return suggestedMax.getTime();
|
||||
}
|
||||
|
||||
export function getSuggestedPeriod(dayDifference: number): StatisticPeriod {
|
||||
return dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour";
|
||||
}
|
||||
|
||||
function createYAxisLabelFormatter(locale: FrontendLocaleData) {
|
||||
let previousValue: number | undefined;
|
||||
|
||||
@ -95,7 +92,7 @@ export function getCommonOptions(
|
||||
type: "time",
|
||||
min: start,
|
||||
max: getSuggestedMax(
|
||||
detailedDailyData ? "5minute" : getSuggestedPeriod(dayDifference),
|
||||
getSuggestedPeriod(start, end, detailedDailyData),
|
||||
end
|
||||
),
|
||||
},
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { differenceInDays, endOfToday, isToday, startOfToday } from "date-fns";
|
||||
import { endOfToday, isToday, startOfToday } from "date-fns";
|
||||
import type { HassConfig, UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||
import type { PropertyValues } from "lit";
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
@ -18,6 +18,7 @@ import type {
|
||||
import {
|
||||
getEnergyDataCollection,
|
||||
getEnergySolarForecasts,
|
||||
getSuggestedPeriod,
|
||||
} from "../../../../data/energy";
|
||||
import type { Statistics, StatisticsMetaData } from "../../../../data/recorder";
|
||||
import { getStatisticLabel } from "../../../../data/recorder";
|
||||
@ -354,7 +355,7 @@ export class HuiEnergySolarGraphCard
|
||||
) {
|
||||
const data: LineSeriesOption[] = [];
|
||||
|
||||
const dayDifference = differenceInDays(end || new Date(), start);
|
||||
const period = getSuggestedPeriod(start, end);
|
||||
|
||||
// Process solar forecast data.
|
||||
solarSources.forEach((source) => {
|
||||
@ -370,10 +371,10 @@ export class HuiEnergySolarGraphCard
|
||||
if (dateObj < start || (end && dateObj > end)) {
|
||||
return;
|
||||
}
|
||||
if (dayDifference > 35) {
|
||||
if (period === "month") {
|
||||
dateObj.setDate(1);
|
||||
}
|
||||
if (dayDifference > 2) {
|
||||
if (period === "month" || period === "day") {
|
||||
dateObj.setHours(0, 0, 0, 0);
|
||||
} else {
|
||||
dateObj.setMinutes(0, 0, 0);
|
||||
|
||||
@ -8,7 +8,10 @@ import { createSearchParam } from "../../../common/url/search-params";
|
||||
import "../../../components/ha-card";
|
||||
import "../../../components/ha-icon-next";
|
||||
import "../../../components/ha-tooltip";
|
||||
import { getEnergyDataCollection } from "../../../data/energy";
|
||||
import {
|
||||
getEnergyDataCollection,
|
||||
getSuggestedPeriod,
|
||||
} from "../../../data/energy";
|
||||
import type {
|
||||
Statistics,
|
||||
StatisticsMetaData,
|
||||
@ -26,10 +29,7 @@ import { hasConfigOrEntitiesChanged } from "../common/has-changed";
|
||||
import { processConfigEntities } from "../common/process-config-entities";
|
||||
import type { EntityConfig } from "../entity-rows/types";
|
||||
import type { LovelaceCard, LovelaceGridOptions } from "../types";
|
||||
import {
|
||||
getSuggestedMax,
|
||||
getSuggestedPeriod,
|
||||
} from "./energy/common/energy-chart-options";
|
||||
import { getSuggestedMax } from "./energy/common/energy-chart-options";
|
||||
import type { StatisticsGraphCardConfig } from "./types";
|
||||
|
||||
export const DEFAULT_DAYS_TO_SHOW = 30;
|
||||
@ -268,9 +268,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
||||
return (
|
||||
this._config?.period ??
|
||||
(this._energyStart && this._energyEnd
|
||||
? getSuggestedPeriod(
|
||||
differenceInDays(this._energyEnd, this._energyStart)
|
||||
)
|
||||
? getSuggestedPeriod(this._energyStart, this._energyEnd)
|
||||
: undefined)
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user