mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-20 02:38:53 +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:
@@ -31,7 +31,11 @@ import { formatTime } from "../../../../../common/datetime/format_time";
|
|||||||
import type { ECOption } from "../../../../../resources/echarts/echarts";
|
import type { ECOption } from "../../../../../resources/echarts/echarts";
|
||||||
import { filterXSS } from "../../../../../common/util/xss";
|
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);
|
let suggestedMax = new Date(end);
|
||||||
|
|
||||||
// Sometimes around DST we get a time of 0:59 instead of 23:59 as expected.
|
// 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 = subHours(suggestedMax, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
suggestedMax.setMinutes(0, 0, 0);
|
if (!detailedDailyData) {
|
||||||
|
suggestedMax.setMinutes(0, 0, 0);
|
||||||
|
}
|
||||||
if (dayDifference > 35) {
|
if (dayDifference > 35) {
|
||||||
suggestedMax.setDate(1);
|
suggestedMax.setDate(1);
|
||||||
}
|
}
|
||||||
@@ -77,7 +83,8 @@ export function getCommonOptions(
|
|||||||
unit?: string,
|
unit?: string,
|
||||||
compareStart?: Date,
|
compareStart?: Date,
|
||||||
compareEnd?: Date,
|
compareEnd?: Date,
|
||||||
formatTotal?: (total: number) => string
|
formatTotal?: (total: number) => string,
|
||||||
|
detailedDailyData = false
|
||||||
): ECOption {
|
): ECOption {
|
||||||
const dayDifference = differenceInDays(end, start);
|
const dayDifference = differenceInDays(end, start);
|
||||||
|
|
||||||
@@ -89,7 +96,7 @@ export function getCommonOptions(
|
|||||||
xAxis: {
|
xAxis: {
|
||||||
type: "time",
|
type: "time",
|
||||||
min: start,
|
min: start,
|
||||||
max: getSuggestedMax(dayDifference, end),
|
max: getSuggestedMax(dayDifference, end, detailedDailyData),
|
||||||
},
|
},
|
||||||
yAxis: {
|
yAxis: {
|
||||||
type: "value",
|
type: "value",
|
||||||
|
|||||||
@@ -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 { HassConfig, UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import type { PropertyValues } from "lit";
|
import type { PropertyValues } from "lit";
|
||||||
import { css, html, LitElement, nothing } from "lit";
|
import { css, html, LitElement, nothing } from "lit";
|
||||||
@@ -132,7 +132,9 @@ export class HuiPowerSourcesGraphCard
|
|||||||
config,
|
config,
|
||||||
"kW",
|
"kW",
|
||||||
compareStart,
|
compareStart,
|
||||||
compareEnd
|
compareEnd,
|
||||||
|
undefined,
|
||||||
|
true
|
||||||
),
|
),
|
||||||
legend: {
|
legend: {
|
||||||
show: this._config?.show_legend !== false,
|
show: this._config?.show_legend !== false,
|
||||||
@@ -210,14 +212,17 @@ export class HuiPowerSourcesGraphCard
|
|||||||
const { positive, negative } = this._processData(
|
const { positive, negative } = this._processData(
|
||||||
statIds[key].stats.map((id: string) => {
|
statIds[key].stats.map((id: string) => {
|
||||||
const stats = energyData.stats[id] ?? [];
|
const stats = energyData.stats[id] ?? [];
|
||||||
const currentStateWatts = getPowerFromState(this.hass.states[id]);
|
if (isSameDay(now, this._start) && isSameDay(now, this._end)) {
|
||||||
if (currentStateWatts !== undefined) {
|
// Append current state if we are showing today
|
||||||
// getPowerFromState returns power in W; convert to kW for this graph
|
const currentStateWatts = getPowerFromState(this.hass.states[id]);
|
||||||
stats.push({
|
if (currentStateWatts !== undefined) {
|
||||||
start: now,
|
// getPowerFromState returns power in W; convert to kW for this graph
|
||||||
end: now,
|
stats.push({
|
||||||
mean: currentStateWatts / 1000,
|
start: now,
|
||||||
});
|
end: now,
|
||||||
|
mean: currentStateWatts / 1000,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return stats;
|
return stats;
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user