1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-19 18:28:42 +00:00

Fix datetime handling in energy charts (#28345)

* Fix datetime handling in energy charts

* PR comment

* Add detailedDailyData parameter to getSuggestedMax and update getCommonOptions

* refactor

---------

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Petar Petrov
2025-12-19 19:47:18 +02:00
committed by GitHub
parent cdcbd00a92
commit 39f550cf9f
2 changed files with 26 additions and 14 deletions

View File

@@ -31,7 +31,11 @@ import { formatTime } from "../../../../../common/datetime/format_time";
import type { ECOption } from "../../../../../resources/echarts/echarts";
import { filterXSS } from "../../../../../common/util/xss";
export function getSuggestedMax(dayDifference: number, end: Date): number {
export function getSuggestedMax(
dayDifference: number,
end: Date,
detailedDailyData = false
): number {
let suggestedMax = new Date(end);
// Sometimes around DST we get a time of 0:59 instead of 23:59 as expected.
@@ -40,7 +44,9 @@ export function getSuggestedMax(dayDifference: number, end: Date): number {
suggestedMax = subHours(suggestedMax, 1);
}
suggestedMax.setMinutes(0, 0, 0);
if (!detailedDailyData) {
suggestedMax.setMinutes(0, 0, 0);
}
if (dayDifference > 35) {
suggestedMax.setDate(1);
}
@@ -77,7 +83,8 @@ export function getCommonOptions(
unit?: string,
compareStart?: Date,
compareEnd?: Date,
formatTotal?: (total: number) => string
formatTotal?: (total: number) => string,
detailedDailyData = false
): ECOption {
const dayDifference = differenceInDays(end, start);
@@ -89,7 +96,7 @@ export function getCommonOptions(
xAxis: {
type: "time",
min: start,
max: getSuggestedMax(dayDifference, end),
max: getSuggestedMax(dayDifference, end, detailedDailyData),
},
yAxis: {
type: "value",

View File

@@ -1,4 +1,4 @@
import { endOfToday, isToday, startOfToday } from "date-fns";
import { endOfToday, isSameDay, 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";
@@ -132,7 +132,9 @@ export class HuiPowerSourcesGraphCard
config,
"kW",
compareStart,
compareEnd
compareEnd,
undefined,
true
),
legend: {
show: this._config?.show_legend !== false,
@@ -210,14 +212,17 @@ export class HuiPowerSourcesGraphCard
const { positive, negative } = this._processData(
statIds[key].stats.map((id: string) => {
const stats = energyData.stats[id] ?? [];
const currentStateWatts = getPowerFromState(this.hass.states[id]);
if (currentStateWatts !== undefined) {
// getPowerFromState returns power in W; convert to kW for this graph
stats.push({
start: now,
end: now,
mean: currentStateWatts / 1000,
});
if (isSameDay(now, this._start) && isSameDay(now, this._end)) {
// Append current state if we are showing today
const currentStateWatts = getPowerFromState(this.hass.states[id]);
if (currentStateWatts !== undefined) {
// getPowerFromState returns power in W; convert to kW for this graph
stats.push({
start: now,
end: now,
mean: currentStateWatts / 1000,
});
}
}
return stats;
})