mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-24 17:56:46 +00:00
Don't fetch unneeded statistics from core (#14423)
This commit is contained in:
parent
1a68a2f4d7
commit
0bfb2b4a56
@ -13,7 +13,6 @@ import {
|
|||||||
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
|
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
|
||||||
|
|
||||||
const generateMeanStatistics = (
|
const generateMeanStatistics = (
|
||||||
id: string,
|
|
||||||
start: Date,
|
start: Date,
|
||||||
end: Date,
|
end: Date,
|
||||||
period: "5minute" | "hour" | "day" | "month" = "hour",
|
period: "5minute" | "hour" | "day" | "month" = "hour",
|
||||||
@ -29,13 +28,12 @@ const generateMeanStatistics = (
|
|||||||
const delta = Math.random() * maxDiff;
|
const delta = Math.random() * maxDiff;
|
||||||
const mean = lastVal + delta;
|
const mean = lastVal + delta;
|
||||||
statistics.push({
|
statistics.push({
|
||||||
statistic_id: id,
|
start: currentDate.getTime(),
|
||||||
start: currentDate.toISOString(),
|
end: currentDate.getTime(),
|
||||||
end: currentDate.toISOString(),
|
|
||||||
mean,
|
mean,
|
||||||
min: mean - Math.random() * maxDiff,
|
min: mean - Math.random() * maxDiff,
|
||||||
max: mean + Math.random() * maxDiff,
|
max: mean + Math.random() * maxDiff,
|
||||||
last_reset: "1970-01-01T00:00:00+00:00",
|
last_reset: 0,
|
||||||
state: mean,
|
state: mean,
|
||||||
sum: null,
|
sum: null,
|
||||||
});
|
});
|
||||||
@ -51,7 +49,6 @@ const generateMeanStatistics = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const generateSumStatistics = (
|
const generateSumStatistics = (
|
||||||
id: string,
|
|
||||||
start: Date,
|
start: Date,
|
||||||
end: Date,
|
end: Date,
|
||||||
period: "5minute" | "hour" | "day" | "month" = "hour",
|
period: "5minute" | "hour" | "day" | "month" = "hour",
|
||||||
@ -67,13 +64,12 @@ const generateSumStatistics = (
|
|||||||
const add = Math.random() * maxDiff;
|
const add = Math.random() * maxDiff;
|
||||||
sum += add;
|
sum += add;
|
||||||
statistics.push({
|
statistics.push({
|
||||||
statistic_id: id,
|
start: currentDate.getTime(),
|
||||||
start: currentDate.toISOString(),
|
end: currentDate.getTime(),
|
||||||
end: currentDate.toISOString(),
|
|
||||||
mean: null,
|
mean: null,
|
||||||
min: null,
|
min: null,
|
||||||
max: null,
|
max: null,
|
||||||
last_reset: "1970-01-01T00:00:00+00:00",
|
last_reset: 0,
|
||||||
state: initValue + sum,
|
state: initValue + sum,
|
||||||
sum,
|
sum,
|
||||||
});
|
});
|
||||||
@ -88,7 +84,6 @@ const generateSumStatistics = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const generateCurvedStatistics = (
|
const generateCurvedStatistics = (
|
||||||
id: string,
|
|
||||||
start: Date,
|
start: Date,
|
||||||
end: Date,
|
end: Date,
|
||||||
_period: "5minute" | "hour" | "day" | "month" = "hour",
|
_period: "5minute" | "hour" | "day" | "month" = "hour",
|
||||||
@ -108,13 +103,12 @@ const generateCurvedStatistics = (
|
|||||||
const add = Math.random() * maxDiff;
|
const add = Math.random() * maxDiff;
|
||||||
sum += i * add;
|
sum += i * add;
|
||||||
statistics.push({
|
statistics.push({
|
||||||
statistic_id: id,
|
start: currentDate.getTime(),
|
||||||
start: currentDate.toISOString(),
|
end: currentDate.getTime(),
|
||||||
end: currentDate.toISOString(),
|
|
||||||
mean: null,
|
mean: null,
|
||||||
min: null,
|
min: null,
|
||||||
max: null,
|
max: null,
|
||||||
last_reset: "1970-01-01T00:00:00+00:00",
|
last_reset: 0,
|
||||||
state: initValue + sum,
|
state: initValue + sum,
|
||||||
sum: metered ? sum : null,
|
sum: metered ? sum : null,
|
||||||
});
|
});
|
||||||
@ -137,14 +131,13 @@ const statisticsFunctions: Record<
|
|||||||
) => StatisticValue[]
|
) => StatisticValue[]
|
||||||
> = {
|
> = {
|
||||||
"sensor.energy_consumption_tarif_1": (
|
"sensor.energy_consumption_tarif_1": (
|
||||||
id: string,
|
_id: string,
|
||||||
start: Date,
|
start: Date,
|
||||||
end: Date,
|
end: Date,
|
||||||
period = "hour"
|
period = "hour"
|
||||||
) => {
|
) => {
|
||||||
if (period !== "hour") {
|
if (period !== "hour") {
|
||||||
return generateSumStatistics(
|
return generateSumStatistics(
|
||||||
id,
|
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -153,20 +146,12 @@ const statisticsFunctions: Record<
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
const morningEnd = new Date(start.getTime() + 10 * 60 * 60 * 1000);
|
const morningEnd = new Date(start.getTime() + 10 * 60 * 60 * 1000);
|
||||||
const morningLow = generateSumStatistics(
|
const morningLow = generateSumStatistics(start, morningEnd, period, 0, 0.7);
|
||||||
id,
|
|
||||||
start,
|
|
||||||
morningEnd,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0.7
|
|
||||||
);
|
|
||||||
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
||||||
const morningFinalVal = morningLow.length
|
const morningFinalVal = morningLow.length
|
||||||
? morningLow[morningLow.length - 1].sum!
|
? morningLow[morningLow.length - 1].sum!
|
||||||
: 0;
|
: 0;
|
||||||
const empty = generateSumStatistics(
|
const empty = generateSumStatistics(
|
||||||
id,
|
|
||||||
morningEnd,
|
morningEnd,
|
||||||
eveningStart,
|
eveningStart,
|
||||||
period,
|
period,
|
||||||
@ -174,7 +159,6 @@ const statisticsFunctions: Record<
|
|||||||
0
|
0
|
||||||
);
|
);
|
||||||
const eveningLow = generateSumStatistics(
|
const eveningLow = generateSumStatistics(
|
||||||
id,
|
|
||||||
eveningStart,
|
eveningStart,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -184,14 +168,13 @@ const statisticsFunctions: Record<
|
|||||||
return [...morningLow, ...empty, ...eveningLow];
|
return [...morningLow, ...empty, ...eveningLow];
|
||||||
},
|
},
|
||||||
"sensor.energy_consumption_tarif_2": (
|
"sensor.energy_consumption_tarif_2": (
|
||||||
id: string,
|
_id: string,
|
||||||
start: Date,
|
start: Date,
|
||||||
end: Date,
|
end: Date,
|
||||||
period = "hour"
|
period = "hour"
|
||||||
) => {
|
) => {
|
||||||
if (period !== "hour") {
|
if (period !== "hour") {
|
||||||
return generateSumStatistics(
|
return generateSumStatistics(
|
||||||
id,
|
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -202,7 +185,6 @@ const statisticsFunctions: Record<
|
|||||||
const morningEnd = new Date(start.getTime() + 9 * 60 * 60 * 1000);
|
const morningEnd = new Date(start.getTime() + 9 * 60 * 60 * 1000);
|
||||||
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
||||||
const highTarif = generateSumStatistics(
|
const highTarif = generateSumStatistics(
|
||||||
id,
|
|
||||||
morningEnd,
|
morningEnd,
|
||||||
eveningStart,
|
eveningStart,
|
||||||
period,
|
period,
|
||||||
@ -212,9 +194,8 @@ const statisticsFunctions: Record<
|
|||||||
const highTarifFinalVal = highTarif.length
|
const highTarifFinalVal = highTarif.length
|
||||||
? highTarif[highTarif.length - 1].sum!
|
? highTarif[highTarif.length - 1].sum!
|
||||||
: 0;
|
: 0;
|
||||||
const morning = generateSumStatistics(id, start, morningEnd, period, 0, 0);
|
const morning = generateSumStatistics(start, morningEnd, period, 0, 0);
|
||||||
const evening = generateSumStatistics(
|
const evening = generateSumStatistics(
|
||||||
id,
|
|
||||||
eveningStart,
|
eveningStart,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -223,18 +204,17 @@ const statisticsFunctions: Record<
|
|||||||
);
|
);
|
||||||
return [...morning, ...highTarif, ...evening];
|
return [...morning, ...highTarif, ...evening];
|
||||||
},
|
},
|
||||||
"sensor.energy_production_tarif_1": (id, start, end, period = "hour") =>
|
"sensor.energy_production_tarif_1": (_id, start, end, period = "hour") =>
|
||||||
generateSumStatistics(id, start, end, period, 0, 0),
|
generateSumStatistics(start, end, period, 0, 0),
|
||||||
"sensor.energy_production_tarif_1_compensation": (
|
"sensor.energy_production_tarif_1_compensation": (
|
||||||
id,
|
_id,
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
period = "hour"
|
period = "hour"
|
||||||
) => generateSumStatistics(id, start, end, period, 0, 0),
|
) => generateSumStatistics(start, end, period, 0, 0),
|
||||||
"sensor.energy_production_tarif_2": (id, start, end, period = "hour") => {
|
"sensor.energy_production_tarif_2": (_id, start, end, period = "hour") => {
|
||||||
if (period !== "hour") {
|
if (period !== "hour") {
|
||||||
return generateSumStatistics(
|
return generateSumStatistics(
|
||||||
id,
|
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -246,7 +226,6 @@ const statisticsFunctions: Record<
|
|||||||
const productionEnd = new Date(start.getTime() + 21 * 60 * 60 * 1000);
|
const productionEnd = new Date(start.getTime() + 21 * 60 * 60 * 1000);
|
||||||
const dayEnd = new Date(endOfDay(productionEnd));
|
const dayEnd = new Date(endOfDay(productionEnd));
|
||||||
const production = generateCurvedStatistics(
|
const production = generateCurvedStatistics(
|
||||||
id,
|
|
||||||
productionStart,
|
productionStart,
|
||||||
productionEnd,
|
productionEnd,
|
||||||
period,
|
period,
|
||||||
@ -257,16 +236,8 @@ const statisticsFunctions: Record<
|
|||||||
const productionFinalVal = production.length
|
const productionFinalVal = production.length
|
||||||
? production[production.length - 1].sum!
|
? production[production.length - 1].sum!
|
||||||
: 0;
|
: 0;
|
||||||
const morning = generateSumStatistics(
|
const morning = generateSumStatistics(start, productionStart, period, 0, 0);
|
||||||
id,
|
|
||||||
start,
|
|
||||||
productionStart,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
const evening = generateSumStatistics(
|
const evening = generateSumStatistics(
|
||||||
id,
|
|
||||||
productionEnd,
|
productionEnd,
|
||||||
dayEnd,
|
dayEnd,
|
||||||
period,
|
period,
|
||||||
@ -274,7 +245,6 @@ const statisticsFunctions: Record<
|
|||||||
0
|
0
|
||||||
);
|
);
|
||||||
const rest = generateSumStatistics(
|
const rest = generateSumStatistics(
|
||||||
id,
|
|
||||||
dayEnd,
|
dayEnd,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -283,10 +253,9 @@ const statisticsFunctions: Record<
|
|||||||
);
|
);
|
||||||
return [...morning, ...production, ...evening, ...rest];
|
return [...morning, ...production, ...evening, ...rest];
|
||||||
},
|
},
|
||||||
"sensor.solar_production": (id, start, end, period = "hour") => {
|
"sensor.solar_production": (_id, start, end, period = "hour") => {
|
||||||
if (period !== "hour") {
|
if (period !== "hour") {
|
||||||
return generateSumStatistics(
|
return generateSumStatistics(
|
||||||
id,
|
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -298,7 +267,6 @@ const statisticsFunctions: Record<
|
|||||||
const productionEnd = new Date(start.getTime() + 23 * 60 * 60 * 1000);
|
const productionEnd = new Date(start.getTime() + 23 * 60 * 60 * 1000);
|
||||||
const dayEnd = new Date(endOfDay(productionEnd));
|
const dayEnd = new Date(endOfDay(productionEnd));
|
||||||
const production = generateCurvedStatistics(
|
const production = generateCurvedStatistics(
|
||||||
id,
|
|
||||||
productionStart,
|
productionStart,
|
||||||
productionEnd,
|
productionEnd,
|
||||||
period,
|
period,
|
||||||
@ -309,16 +277,8 @@ const statisticsFunctions: Record<
|
|||||||
const productionFinalVal = production.length
|
const productionFinalVal = production.length
|
||||||
? production[production.length - 1].sum!
|
? production[production.length - 1].sum!
|
||||||
: 0;
|
: 0;
|
||||||
const morning = generateSumStatistics(
|
const morning = generateSumStatistics(start, productionStart, period, 0, 0);
|
||||||
id,
|
|
||||||
start,
|
|
||||||
productionStart,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
const evening = generateSumStatistics(
|
const evening = generateSumStatistics(
|
||||||
id,
|
|
||||||
productionEnd,
|
productionEnd,
|
||||||
dayEnd,
|
dayEnd,
|
||||||
period,
|
period,
|
||||||
@ -326,7 +286,6 @@ const statisticsFunctions: Record<
|
|||||||
0
|
0
|
||||||
);
|
);
|
||||||
const rest = generateSumStatistics(
|
const rest = generateSumStatistics(
|
||||||
id,
|
|
||||||
dayEnd,
|
dayEnd,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -362,7 +321,6 @@ export const mockRecorder = (mockHass: MockHomeAssistant) => {
|
|||||||
statistics[id] =
|
statistics[id] =
|
||||||
entityState && "last_reset" in entityState.attributes
|
entityState && "last_reset" in entityState.attributes
|
||||||
? generateSumStatistics(
|
? generateSumStatistics(
|
||||||
id,
|
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
@ -370,7 +328,6 @@ export const mockRecorder = (mockHass: MockHomeAssistant) => {
|
|||||||
state * (state > 80 ? 0.01 : 0.05)
|
state * (state > 80 ? 0.01 : 0.05)
|
||||||
)
|
)
|
||||||
: generateMeanStatistics(
|
: generateMeanStatistics(
|
||||||
id,
|
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
period,
|
period,
|
||||||
|
@ -233,7 +233,7 @@ class StatisticsChart extends LitElement {
|
|||||||
(await this._getStatisticsMetaData(Object.keys(this.statisticsData)));
|
(await this._getStatisticsMetaData(Object.keys(this.statisticsData)));
|
||||||
|
|
||||||
let colorIndex = 0;
|
let colorIndex = 0;
|
||||||
const statisticsData = Object.values(this.statisticsData);
|
const statisticsData = Object.entries(this.statisticsData);
|
||||||
const totalDataSets: ChartDataset<"line">[] = [];
|
const totalDataSets: ChartDataset<"line">[] = [];
|
||||||
let endTime: Date;
|
let endTime: Date;
|
||||||
|
|
||||||
@ -246,7 +246,7 @@ class StatisticsChart extends LitElement {
|
|||||||
// Get the highest date from the last date of each statistic
|
// Get the highest date from the last date of each statistic
|
||||||
new Date(
|
new Date(
|
||||||
Math.max(
|
Math.max(
|
||||||
...statisticsData.map((stats) =>
|
...statisticsData.map(([_, stats]) =>
|
||||||
new Date(stats[stats.length - 1].start).getTime()
|
new Date(stats[stats.length - 1].start).getTime()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -259,20 +259,19 @@ class StatisticsChart extends LitElement {
|
|||||||
let unit: string | undefined | null;
|
let unit: string | undefined | null;
|
||||||
|
|
||||||
const names = this.names || {};
|
const names = this.names || {};
|
||||||
statisticsData.forEach((stats) => {
|
statisticsData.forEach(([statistic_id, stats]) => {
|
||||||
const firstStat = stats[0];
|
const meta = statisticsMetaData?.[statistic_id];
|
||||||
const meta = statisticsMetaData?.[firstStat.statistic_id];
|
let name = names[statistic_id];
|
||||||
let name = names[firstStat.statistic_id];
|
|
||||||
if (name === undefined) {
|
if (name === undefined) {
|
||||||
name = getStatisticLabel(this.hass, firstStat.statistic_id, meta);
|
name = getStatisticLabel(this.hass, statistic_id, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.unit) {
|
if (!this.unit) {
|
||||||
if (unit === undefined) {
|
if (unit === undefined) {
|
||||||
unit = getDisplayUnit(this.hass, firstStat.statistic_id, meta);
|
unit = getDisplayUnit(this.hass, statistic_id, meta);
|
||||||
} else if (
|
} else if (
|
||||||
unit !== null &&
|
unit !== null &&
|
||||||
unit !== getDisplayUnit(this.hass, firstStat.statistic_id, meta)
|
unit !== getDisplayUnit(this.hass, statistic_id, meta)
|
||||||
) {
|
) {
|
||||||
// Clear unit if not all statistics have same unit
|
// Clear unit if not all statistics have same unit
|
||||||
unit = null;
|
unit = null;
|
||||||
@ -363,8 +362,8 @@ class StatisticsChart extends LitElement {
|
|||||||
|
|
||||||
let prevDate: Date | null = null;
|
let prevDate: Date | null = null;
|
||||||
// Process chart data.
|
// Process chart data.
|
||||||
let firstSum: number | null = null;
|
let firstSum: number | null | undefined = null;
|
||||||
let prevSum: number | null = null;
|
let prevSum: number | null | undefined = null;
|
||||||
stats.forEach((stat) => {
|
stats.forEach((stat) => {
|
||||||
const date = new Date(stat.start);
|
const date = new Date(stat.start);
|
||||||
if (prevDate === date) {
|
if (prevDate === date) {
|
||||||
@ -373,16 +372,16 @@ class StatisticsChart extends LitElement {
|
|||||||
prevDate = date;
|
prevDate = date;
|
||||||
const dataValues: Array<number | null> = [];
|
const dataValues: Array<number | null> = [];
|
||||||
statTypes.forEach((type) => {
|
statTypes.forEach((type) => {
|
||||||
let val: number | null;
|
let val: number | null | undefined;
|
||||||
if (type === "sum") {
|
if (type === "sum") {
|
||||||
if (firstSum === null) {
|
if (firstSum === null || firstSum === undefined) {
|
||||||
val = 0;
|
val = 0;
|
||||||
firstSum = stat.sum;
|
firstSum = stat.sum;
|
||||||
} else {
|
} else {
|
||||||
val = (stat.sum || 0) - firstSum;
|
val = (stat.sum || 0) - firstSum;
|
||||||
}
|
}
|
||||||
} else if (type === "change") {
|
} else if (type === "change") {
|
||||||
if (prevSum === null) {
|
if (prevSum === null || prevSum === undefined) {
|
||||||
prevSum = stat.sum;
|
prevSum = stat.sum;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -391,7 +390,11 @@ class StatisticsChart extends LitElement {
|
|||||||
} else {
|
} else {
|
||||||
val = stat[type];
|
val = stat[type];
|
||||||
}
|
}
|
||||||
dataValues.push(val !== null ? Math.round(val * 100) / 100 : null);
|
dataValues.push(
|
||||||
|
val !== null && val !== undefined
|
||||||
|
? Math.round(val * 100) / 100
|
||||||
|
: null
|
||||||
|
);
|
||||||
});
|
});
|
||||||
pushData(date, dataValues);
|
pushData(date, dataValues);
|
||||||
});
|
});
|
||||||
|
@ -410,7 +410,8 @@ const getEnergyData = async (
|
|||||||
end,
|
end,
|
||||||
energyStatIds,
|
energyStatIds,
|
||||||
period,
|
period,
|
||||||
energyUnits
|
energyUnits,
|
||||||
|
["sum"]
|
||||||
)),
|
)),
|
||||||
...(await fetchStatistics(
|
...(await fetchStatistics(
|
||||||
hass!,
|
hass!,
|
||||||
@ -418,7 +419,8 @@ const getEnergyData = async (
|
|||||||
end,
|
end,
|
||||||
waterStatIds,
|
waterStatIds,
|
||||||
period,
|
period,
|
||||||
waterUnits
|
waterUnits,
|
||||||
|
["sum"]
|
||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -443,7 +445,8 @@ const getEnergyData = async (
|
|||||||
endCompare,
|
endCompare,
|
||||||
energyStatIds,
|
energyStatIds,
|
||||||
period,
|
period,
|
||||||
energyUnits
|
energyUnits,
|
||||||
|
["sum"]
|
||||||
)),
|
)),
|
||||||
...(await fetchStatistics(
|
...(await fetchStatistics(
|
||||||
hass!,
|
hass!,
|
||||||
@ -451,7 +454,8 @@ const getEnergyData = async (
|
|||||||
end,
|
end,
|
||||||
waterStatIds,
|
waterStatIds,
|
||||||
period,
|
period,
|
||||||
waterUnits
|
waterUnits,
|
||||||
|
["sum"]
|
||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -485,8 +489,8 @@ const getEnergyData = async (
|
|||||||
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
||||||
stat.unshift({
|
stat.unshift({
|
||||||
...stat[0],
|
...stat[0],
|
||||||
start: startMinHour.toISOString(),
|
start: startMinHour.getTime(),
|
||||||
end: startMinHour.toISOString(),
|
end: startMinHour.getTime(),
|
||||||
sum: 0,
|
sum: 0,
|
||||||
state: 0,
|
state: 0,
|
||||||
});
|
});
|
||||||
|
@ -9,15 +9,14 @@ export interface Statistics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface StatisticValue {
|
export interface StatisticValue {
|
||||||
statistic_id: string;
|
start: number;
|
||||||
start: string;
|
end: number;
|
||||||
end: string;
|
last_reset?: number | null;
|
||||||
last_reset: string | null;
|
max?: number | null;
|
||||||
max: number | null;
|
mean?: number | null;
|
||||||
mean: number | null;
|
min?: number | null;
|
||||||
min: number | null;
|
sum?: number | null;
|
||||||
sum: number | null;
|
state?: number | null;
|
||||||
state: number | null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Statistic {
|
export interface Statistic {
|
||||||
@ -91,6 +90,16 @@ export interface StatisticsUnitConfiguration {
|
|||||||
volume?: "L" | "gal" | "ft³" | "m³";
|
volume?: "L" | "gal" | "ft³" | "m³";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const statisticTypes = [
|
||||||
|
"last_reset",
|
||||||
|
"max",
|
||||||
|
"mean",
|
||||||
|
"min",
|
||||||
|
"state",
|
||||||
|
"sum",
|
||||||
|
] as const;
|
||||||
|
export type StatisticsTypes = typeof statisticTypes[number][];
|
||||||
|
|
||||||
export interface StatisticsValidationResults {
|
export interface StatisticsValidationResults {
|
||||||
[statisticId: string]: StatisticsValidationResult[];
|
[statisticId: string]: StatisticsValidationResult[];
|
||||||
}
|
}
|
||||||
@ -119,7 +128,8 @@ export const fetchStatistics = (
|
|||||||
endTime?: Date,
|
endTime?: Date,
|
||||||
statistic_ids?: string[],
|
statistic_ids?: string[],
|
||||||
period: "5minute" | "hour" | "day" | "week" | "month" = "hour",
|
period: "5minute" | "hour" | "day" | "week" | "month" = "hour",
|
||||||
units?: StatisticsUnitConfiguration
|
units?: StatisticsUnitConfiguration,
|
||||||
|
types?: StatisticsTypes
|
||||||
) =>
|
) =>
|
||||||
hass.callWS<Statistics>({
|
hass.callWS<Statistics>({
|
||||||
type: "recorder/statistics_during_period",
|
type: "recorder/statistics_during_period",
|
||||||
@ -128,6 +138,7 @@ export const fetchStatistics = (
|
|||||||
statistic_ids,
|
statistic_ids,
|
||||||
period,
|
period,
|
||||||
units,
|
units,
|
||||||
|
types,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const fetchStatistic = (
|
export const fetchStatistic = (
|
||||||
@ -189,11 +200,11 @@ export const calculateStatisticSumGrowth = (
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const endSum = values[values.length - 1].sum;
|
const endSum = values[values.length - 1].sum;
|
||||||
if (endSum === null) {
|
if (endSum === null || endSum === undefined) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const startSum = values[0].sum;
|
const startSum = values[0].sum;
|
||||||
if (startSum === null) {
|
if (startSum === null || startSum === undefined) {
|
||||||
return endSum;
|
return endSum;
|
||||||
}
|
}
|
||||||
return endSum - startSum;
|
return endSum - startSum;
|
||||||
@ -248,17 +259,19 @@ export const statisticsMetaHasType = (
|
|||||||
export const adjustStatisticsSum = (
|
export const adjustStatisticsSum = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
statistic_id: string,
|
statistic_id: string,
|
||||||
start_time: string,
|
start_time: number,
|
||||||
adjustment: number,
|
adjustment: number,
|
||||||
adjustment_unit_of_measurement: string | null
|
adjustment_unit_of_measurement: string | null
|
||||||
): Promise<void> =>
|
): Promise<void> => {
|
||||||
hass.callWS({
|
const start_time_iso = new Date(start_time).toISOString();
|
||||||
|
return hass.callWS({
|
||||||
type: "recorder/adjust_sum_statistics",
|
type: "recorder/adjust_sum_statistics",
|
||||||
statistic_id,
|
statistic_id,
|
||||||
start_time,
|
start_time_iso,
|
||||||
adjustment,
|
adjustment,
|
||||||
adjustment_unit_of_measurement,
|
adjustment_unit_of_measurement,
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const getStatisticLabel = (
|
export const getStatisticLabel = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -11,6 +11,7 @@ import {
|
|||||||
fetchStatistics,
|
fetchStatistics,
|
||||||
getStatisticMetadata,
|
getStatisticMetadata,
|
||||||
Statistics,
|
Statistics,
|
||||||
|
StatisticsTypes,
|
||||||
} from "../../data/recorder";
|
} from "../../data/recorder";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
import "../../components/chart/statistics-chart";
|
import "../../components/chart/statistics-chart";
|
||||||
@ -22,7 +23,7 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const statTypes = ["state", "min", "mean", "max"];
|
const statTypes: StatisticsTypes = ["state", "min", "mean", "max"];
|
||||||
|
|
||||||
@customElement("ha-more-info-history")
|
@customElement("ha-more-info-history")
|
||||||
export class MoreInfoHistory extends LitElement {
|
export class MoreInfoHistory extends LitElement {
|
||||||
@ -124,7 +125,9 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
subHours(new Date(), 24),
|
subHours(new Date(), 24),
|
||||||
undefined,
|
undefined,
|
||||||
[this.entityId],
|
[this.entityId],
|
||||||
"5minute"
|
"5minute",
|
||||||
|
undefined,
|
||||||
|
statTypes
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -202,8 +202,8 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
||||||
stat.unshift({
|
stat.unshift({
|
||||||
...stat[0],
|
...stat[0],
|
||||||
start: startMinHour.toISOString(),
|
start: startMinHour.getTime(),
|
||||||
end: startMinHour.toISOString(),
|
end: startMinHour.getTime(),
|
||||||
sum: 0,
|
sum: 0,
|
||||||
state: 0,
|
state: 0,
|
||||||
});
|
});
|
||||||
@ -228,8 +228,8 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
if (stat.length && new Date(stat[0].start) > startMinHour) {
|
||||||
stat.unshift({
|
stat.unshift({
|
||||||
...stat[0],
|
...stat[0],
|
||||||
start: startCompareMinHour.toISOString(),
|
start: startCompareMinHour.getTime(),
|
||||||
end: startCompareMinHour.toISOString(),
|
end: startCompareMinHour.getTime(),
|
||||||
sum: 0,
|
sum: 0,
|
||||||
state: 0,
|
state: 0,
|
||||||
});
|
});
|
||||||
|
@ -348,7 +348,7 @@ export class HuiEnergyGasGraphCard
|
|||||||
: gasColor;
|
: gasColor;
|
||||||
|
|
||||||
let prevValue: number | null = null;
|
let prevValue: number | null = null;
|
||||||
let prevStart: string | null = null;
|
let prevStart: number | null = null;
|
||||||
|
|
||||||
const gasConsumptionData: ScatterDataPoint[] = [];
|
const gasConsumptionData: ScatterDataPoint[] = [];
|
||||||
|
|
||||||
@ -357,10 +357,10 @@ export class HuiEnergyGasGraphCard
|
|||||||
const stats = statistics[source.stat_energy_from];
|
const stats = statistics[source.stat_energy_from];
|
||||||
|
|
||||||
for (const point of stats) {
|
for (const point of stats) {
|
||||||
if (point.sum === null) {
|
if (point.sum === null || point.sum === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (prevValue === null) {
|
if (prevValue === null || prevValue === undefined) {
|
||||||
prevValue = point.sum;
|
prevValue = point.sum;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -368,7 +368,7 @@ export class HuiEnergySolarGraphCard
|
|||||||
: solarColor;
|
: solarColor;
|
||||||
|
|
||||||
let prevValue: number | null = null;
|
let prevValue: number | null = null;
|
||||||
let prevStart: string | null = null;
|
let prevStart: number | null = null;
|
||||||
|
|
||||||
const solarProductionData: ScatterDataPoint[] = [];
|
const solarProductionData: ScatterDataPoint[] = [];
|
||||||
|
|
||||||
@ -377,10 +377,10 @@ export class HuiEnergySolarGraphCard
|
|||||||
const stats = statistics[source.stat_energy_from];
|
const stats = statistics[source.stat_energy_from];
|
||||||
|
|
||||||
for (const point of stats) {
|
for (const point of stats) {
|
||||||
if (point.sum === null) {
|
if (point.sum === null || point.sum === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (prevValue === null) {
|
if (prevValue === null || prevValue === undefined) {
|
||||||
prevValue = point.sum;
|
prevValue = point.sum;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -484,7 +484,7 @@ export class HuiEnergyUsageGraphCard
|
|||||||
const set = {};
|
const set = {};
|
||||||
let prevValue: number;
|
let prevValue: number;
|
||||||
stats.forEach((stat) => {
|
stats.forEach((stat) => {
|
||||||
if (stat.sum === null) {
|
if (stat.sum === null || stat.sum === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (prevValue === undefined) {
|
if (prevValue === undefined) {
|
||||||
|
@ -346,7 +346,7 @@ export class HuiEnergyWaterGraphCard
|
|||||||
: waterColor;
|
: waterColor;
|
||||||
|
|
||||||
let prevValue: number | null = null;
|
let prevValue: number | null = null;
|
||||||
let prevStart: string | null = null;
|
let prevStart: number | null = null;
|
||||||
|
|
||||||
const waterConsumptionData: ScatterDataPoint[] = [];
|
const waterConsumptionData: ScatterDataPoint[] = [];
|
||||||
|
|
||||||
@ -355,10 +355,10 @@ export class HuiEnergyWaterGraphCard
|
|||||||
const stats = statistics[source.stat_energy_from];
|
const stats = statistics[source.stat_energy_from];
|
||||||
|
|
||||||
for (const point of stats) {
|
for (const point of stats) {
|
||||||
if (point.sum === null) {
|
if (point.sum === null || point.sum === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (prevValue === null) {
|
if (prevValue === null || prevValue === undefined) {
|
||||||
prevValue = point.sum;
|
prevValue = point.sum;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ import {
|
|||||||
getStatisticMetadata,
|
getStatisticMetadata,
|
||||||
Statistics,
|
Statistics,
|
||||||
StatisticsMetaData,
|
StatisticsMetaData,
|
||||||
|
StatisticsTypes,
|
||||||
} from "../../../data/recorder";
|
} from "../../../data/recorder";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { findEntities } from "../common/find-entities";
|
import { findEntities } from "../common/find-entities";
|
||||||
@ -69,6 +70,8 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
|
|
||||||
private _interval?: number;
|
private _interval?: number;
|
||||||
|
|
||||||
|
private _statTypes?: StatisticsTypes;
|
||||||
|
|
||||||
public disconnectedCallback() {
|
public disconnectedCallback() {
|
||||||
super.disconnectedCallback();
|
super.disconnectedCallback();
|
||||||
if (this._interval) {
|
if (this._interval) {
|
||||||
@ -117,15 +120,13 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (typeof config.stat_types === "string") {
|
if (typeof config.stat_types === "string") {
|
||||||
this._config = { ...config, stat_types: [config.stat_types] };
|
this._statTypes = [config.stat_types];
|
||||||
} else if (!config.stat_types) {
|
} else if (!config.stat_types) {
|
||||||
this._config = {
|
this._statTypes = ["state", "sum", "min", "max", "mean"];
|
||||||
...config,
|
|
||||||
stat_types: ["state", "sum", "min", "max", "mean"],
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
this._config = config;
|
this._statTypes = config.stat_types;
|
||||||
}
|
}
|
||||||
|
this._config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected shouldUpdate(changedProps: PropertyValues): boolean {
|
protected shouldUpdate(changedProps: PropertyValues): boolean {
|
||||||
@ -191,7 +192,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
.statisticsData=${this._statistics}
|
.statisticsData=${this._statistics}
|
||||||
.metadata=${this._metadata}
|
.metadata=${this._metadata}
|
||||||
.chartType=${this._config.chart_type || "line"}
|
.chartType=${this._config.chart_type || "line"}
|
||||||
.statTypes=${this._config.stat_types!}
|
.statTypes=${this._statTypes!}
|
||||||
.names=${this._names}
|
.names=${this._names}
|
||||||
.unit=${this._unit}
|
.unit=${this._unit}
|
||||||
></statistics-chart>
|
></statistics-chart>
|
||||||
@ -250,7 +251,8 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
undefined,
|
undefined,
|
||||||
this._entities,
|
this._entities,
|
||||||
this._config!.period,
|
this._config!.period,
|
||||||
unitconfig
|
unitconfig,
|
||||||
|
this._statTypes
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._statistics = undefined;
|
this._statistics = undefined;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user