mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-19 07:16:39 +00:00
Fix energy demo (#14002)
This commit is contained in:
parent
af93ec1b92
commit
ef9643ddaf
@ -20,6 +20,7 @@ import { mockHistory } from "./stubs/history";
|
|||||||
import { mockLovelace } from "./stubs/lovelace";
|
import { mockLovelace } from "./stubs/lovelace";
|
||||||
import { mockMediaPlayer } from "./stubs/media_player";
|
import { mockMediaPlayer } from "./stubs/media_player";
|
||||||
import { mockPersistentNotification } from "./stubs/persistent_notification";
|
import { mockPersistentNotification } from "./stubs/persistent_notification";
|
||||||
|
import { mockRecorder } from "./stubs/recorder";
|
||||||
import { mockShoppingList } from "./stubs/shopping_list";
|
import { mockShoppingList } from "./stubs/shopping_list";
|
||||||
import { mockSystemLog } from "./stubs/system_log";
|
import { mockSystemLog } from "./stubs/system_log";
|
||||||
import { mockTemplate } from "./stubs/template";
|
import { mockTemplate } from "./stubs/template";
|
||||||
@ -45,6 +46,7 @@ class HaDemo extends HomeAssistantAppEl {
|
|||||||
mockAuth(hass);
|
mockAuth(hass);
|
||||||
mockTranslations(hass);
|
mockTranslations(hass);
|
||||||
mockHistory(hass);
|
mockHistory(hass);
|
||||||
|
mockRecorder(hass);
|
||||||
mockShoppingList(hass);
|
mockShoppingList(hass);
|
||||||
mockSystemLog(hass);
|
mockSystemLog(hass);
|
||||||
mockTemplate(hass);
|
mockTemplate(hass);
|
||||||
@ -120,3 +122,9 @@ class HaDemo extends HomeAssistantAppEl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
customElements.define("ha-demo", HaDemo);
|
customElements.define("ha-demo", HaDemo);
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"ha-demo": HaDemo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,85 +1,101 @@
|
|||||||
import { format, startOfToday, startOfTomorrow } from "date-fns/esm";
|
import { format, startOfToday, startOfTomorrow } from "date-fns/esm";
|
||||||
import { EnergySolarForecasts } from "../../../src/data/energy";
|
import {
|
||||||
|
EnergyInfo,
|
||||||
|
EnergyPreferences,
|
||||||
|
EnergySolarForecasts,
|
||||||
|
FossilEnergyConsumption,
|
||||||
|
} from "../../../src/data/energy";
|
||||||
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
|
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
|
||||||
|
|
||||||
export const mockEnergy = (hass: MockHomeAssistant) => {
|
export const mockEnergy = (hass: MockHomeAssistant) => {
|
||||||
hass.mockWS("energy/get_prefs", () => ({
|
hass.mockWS(
|
||||||
energy_sources: [
|
"energy/get_prefs",
|
||||||
{
|
(): EnergyPreferences => ({
|
||||||
type: "grid",
|
energy_sources: [
|
||||||
flow_from: [
|
{
|
||||||
{
|
type: "grid",
|
||||||
stat_energy_from: "sensor.energy_consumption_tarif_1",
|
flow_from: [
|
||||||
stat_cost: "sensor.energy_consumption_tarif_1_cost",
|
{
|
||||||
entity_energy_price: null,
|
stat_energy_from: "sensor.energy_consumption_tarif_1",
|
||||||
number_energy_price: null,
|
stat_cost: "sensor.energy_consumption_tarif_1_cost",
|
||||||
},
|
entity_energy_price: null,
|
||||||
{
|
number_energy_price: null,
|
||||||
stat_energy_from: "sensor.energy_consumption_tarif_2",
|
},
|
||||||
stat_cost: "sensor.energy_consumption_tarif_2_cost",
|
{
|
||||||
entity_energy_price: null,
|
stat_energy_from: "sensor.energy_consumption_tarif_2",
|
||||||
number_energy_price: null,
|
stat_cost: "sensor.energy_consumption_tarif_2_cost",
|
||||||
},
|
entity_energy_price: null,
|
||||||
],
|
number_energy_price: null,
|
||||||
flow_to: [
|
},
|
||||||
{
|
],
|
||||||
stat_energy_to: "sensor.energy_production_tarif_1",
|
flow_to: [
|
||||||
stat_compensation: "sensor.energy_production_tarif_1_compensation",
|
{
|
||||||
entity_energy_price: null,
|
stat_energy_to: "sensor.energy_production_tarif_1",
|
||||||
number_energy_price: null,
|
stat_compensation:
|
||||||
},
|
"sensor.energy_production_tarif_1_compensation",
|
||||||
{
|
entity_energy_price: null,
|
||||||
stat_energy_to: "sensor.energy_production_tarif_2",
|
number_energy_price: null,
|
||||||
stat_compensation: "sensor.energy_production_tarif_2_compensation",
|
},
|
||||||
entity_energy_price: null,
|
{
|
||||||
number_energy_price: null,
|
stat_energy_to: "sensor.energy_production_tarif_2",
|
||||||
},
|
stat_compensation:
|
||||||
],
|
"sensor.energy_production_tarif_2_compensation",
|
||||||
cost_adjustment_day: 0,
|
entity_energy_price: null,
|
||||||
},
|
number_energy_price: null,
|
||||||
{
|
},
|
||||||
type: "solar",
|
],
|
||||||
stat_energy_from: "sensor.solar_production",
|
cost_adjustment_day: 0,
|
||||||
config_entry_solar_forecast: ["solar_forecast"],
|
},
|
||||||
},
|
{
|
||||||
/* {
|
type: "solar",
|
||||||
type: "battery",
|
stat_energy_from: "sensor.solar_production",
|
||||||
stat_energy_from: "sensor.battery_output",
|
config_entry_solar_forecast: ["solar_forecast"],
|
||||||
stat_energy_to: "sensor.battery_input",
|
},
|
||||||
}, */
|
/* {
|
||||||
{
|
type: "battery",
|
||||||
type: "gas",
|
stat_energy_from: "sensor.battery_output",
|
||||||
stat_energy_from: "sensor.energy_gas",
|
stat_energy_to: "sensor.battery_input",
|
||||||
stat_cost: "sensor.energy_gas_cost",
|
}, */
|
||||||
entity_energy_price: null,
|
{
|
||||||
number_energy_price: null,
|
type: "gas",
|
||||||
},
|
stat_energy_from: "sensor.energy_gas",
|
||||||
],
|
stat_cost: "sensor.energy_gas_cost",
|
||||||
device_consumption: [
|
entity_energy_price: null,
|
||||||
{
|
number_energy_price: null,
|
||||||
stat_consumption: "sensor.energy_car",
|
},
|
||||||
},
|
],
|
||||||
{
|
device_consumption: [
|
||||||
stat_consumption: "sensor.energy_ac",
|
{
|
||||||
},
|
stat_consumption: "sensor.energy_car",
|
||||||
{
|
},
|
||||||
stat_consumption: "sensor.energy_washing_machine",
|
{
|
||||||
},
|
stat_consumption: "sensor.energy_ac",
|
||||||
{
|
},
|
||||||
stat_consumption: "sensor.energy_dryer",
|
{
|
||||||
},
|
stat_consumption: "sensor.energy_washing_machine",
|
||||||
{
|
},
|
||||||
stat_consumption: "sensor.energy_heat_pump",
|
{
|
||||||
},
|
stat_consumption: "sensor.energy_dryer",
|
||||||
{
|
},
|
||||||
stat_consumption: "sensor.energy_boiler",
|
{
|
||||||
},
|
stat_consumption: "sensor.energy_heat_pump",
|
||||||
],
|
},
|
||||||
}));
|
{
|
||||||
hass.mockWS("energy/info", () => ({ cost_sensors: [] }));
|
stat_consumption: "sensor.energy_boiler",
|
||||||
hass.mockWS("energy/fossil_energy_consumption", ({ period }) => ({
|
},
|
||||||
start: period === "month" ? 250 : period === "day" ? 10 : 2,
|
],
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
|
hass.mockWS(
|
||||||
|
"energy/info",
|
||||||
|
(): EnergyInfo => ({ cost_sensors: {}, solar_forecast_domains: [] })
|
||||||
|
);
|
||||||
|
hass.mockWS(
|
||||||
|
"energy/fossil_energy_consumption",
|
||||||
|
({ period }): FossilEnergyConsumption => ({
|
||||||
|
start: period === "month" ? 250 : period === "day" ? 10 : 2,
|
||||||
|
})
|
||||||
|
);
|
||||||
const todayString = format(startOfToday(), "yyyy-MM-dd");
|
const todayString = format(startOfToday(), "yyyy-MM-dd");
|
||||||
const tomorrowString = format(startOfTomorrow(), "yyyy-MM-dd");
|
const tomorrowString = format(startOfTomorrow(), "yyyy-MM-dd");
|
||||||
hass.mockWS(
|
hass.mockWS(
|
||||||
|
@ -1,12 +1,4 @@
|
|||||||
import {
|
|
||||||
addDays,
|
|
||||||
addHours,
|
|
||||||
addMonths,
|
|
||||||
differenceInHours,
|
|
||||||
endOfDay,
|
|
||||||
} from "date-fns/esm";
|
|
||||||
import { HassEntity } from "home-assistant-js-websocket";
|
import { HassEntity } from "home-assistant-js-websocket";
|
||||||
import { StatisticValue } from "../../../src/data/recorder";
|
|
||||||
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
|
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
|
||||||
|
|
||||||
interface HistoryQueryParams {
|
interface HistoryQueryParams {
|
||||||
@ -72,331 +64,6 @@ const generateHistory = (state, deltas) => {
|
|||||||
|
|
||||||
const incrementalUnits = ["clients", "queries", "ads"];
|
const incrementalUnits = ["clients", "queries", "ads"];
|
||||||
|
|
||||||
const generateMeanStatistics = (
|
|
||||||
id: string,
|
|
||||||
start: Date,
|
|
||||||
end: Date,
|
|
||||||
period: "5minute" | "hour" | "day" | "month" = "hour",
|
|
||||||
initValue: number,
|
|
||||||
maxDiff: number
|
|
||||||
) => {
|
|
||||||
const statistics: StatisticValue[] = [];
|
|
||||||
let currentDate = new Date(start);
|
|
||||||
currentDate.setMinutes(0, 0, 0);
|
|
||||||
let lastVal = initValue;
|
|
||||||
const now = new Date();
|
|
||||||
while (end > currentDate && currentDate < now) {
|
|
||||||
const delta = Math.random() * maxDiff;
|
|
||||||
const mean = lastVal + delta;
|
|
||||||
statistics.push({
|
|
||||||
statistic_id: id,
|
|
||||||
start: currentDate.toISOString(),
|
|
||||||
end: currentDate.toISOString(),
|
|
||||||
mean,
|
|
||||||
min: mean - Math.random() * maxDiff,
|
|
||||||
max: mean + Math.random() * maxDiff,
|
|
||||||
last_reset: "1970-01-01T00:00:00+00:00",
|
|
||||||
state: mean,
|
|
||||||
sum: null,
|
|
||||||
});
|
|
||||||
lastVal = mean;
|
|
||||||
currentDate =
|
|
||||||
period === "day"
|
|
||||||
? addDays(currentDate, 1)
|
|
||||||
: period === "month"
|
|
||||||
? addMonths(currentDate, 1)
|
|
||||||
: addHours(currentDate, 1);
|
|
||||||
}
|
|
||||||
return statistics;
|
|
||||||
};
|
|
||||||
|
|
||||||
const generateSumStatistics = (
|
|
||||||
id: string,
|
|
||||||
start: Date,
|
|
||||||
end: Date,
|
|
||||||
period: "5minute" | "hour" | "day" | "month" = "hour",
|
|
||||||
initValue: number,
|
|
||||||
maxDiff: number
|
|
||||||
) => {
|
|
||||||
const statistics: StatisticValue[] = [];
|
|
||||||
let currentDate = new Date(start);
|
|
||||||
currentDate.setMinutes(0, 0, 0);
|
|
||||||
let sum = initValue;
|
|
||||||
const now = new Date();
|
|
||||||
while (end > currentDate && currentDate < now) {
|
|
||||||
const add = Math.random() * maxDiff;
|
|
||||||
sum += add;
|
|
||||||
statistics.push({
|
|
||||||
statistic_id: id,
|
|
||||||
start: currentDate.toISOString(),
|
|
||||||
end: currentDate.toISOString(),
|
|
||||||
mean: null,
|
|
||||||
min: null,
|
|
||||||
max: null,
|
|
||||||
last_reset: "1970-01-01T00:00:00+00:00",
|
|
||||||
state: initValue + sum,
|
|
||||||
sum,
|
|
||||||
});
|
|
||||||
currentDate =
|
|
||||||
period === "day"
|
|
||||||
? addDays(currentDate, 1)
|
|
||||||
: period === "month"
|
|
||||||
? addMonths(currentDate, 1)
|
|
||||||
: addHours(currentDate, 1);
|
|
||||||
}
|
|
||||||
return statistics;
|
|
||||||
};
|
|
||||||
|
|
||||||
const generateCurvedStatistics = (
|
|
||||||
id: string,
|
|
||||||
start: Date,
|
|
||||||
end: Date,
|
|
||||||
_period: "5minute" | "hour" | "day" | "month" = "hour",
|
|
||||||
initValue: number,
|
|
||||||
maxDiff: number,
|
|
||||||
metered: boolean
|
|
||||||
) => {
|
|
||||||
const statistics: StatisticValue[] = [];
|
|
||||||
let currentDate = new Date(start);
|
|
||||||
currentDate.setMinutes(0, 0, 0);
|
|
||||||
let sum = initValue;
|
|
||||||
const hours = differenceInHours(end, start) - 1;
|
|
||||||
let i = 0;
|
|
||||||
let half = false;
|
|
||||||
const now = new Date();
|
|
||||||
while (end > currentDate && currentDate < now) {
|
|
||||||
const add = Math.random() * maxDiff;
|
|
||||||
sum += i * add;
|
|
||||||
statistics.push({
|
|
||||||
statistic_id: id,
|
|
||||||
start: currentDate.toISOString(),
|
|
||||||
end: currentDate.toISOString(),
|
|
||||||
mean: null,
|
|
||||||
min: null,
|
|
||||||
max: null,
|
|
||||||
last_reset: "1970-01-01T00:00:00+00:00",
|
|
||||||
state: initValue + sum,
|
|
||||||
sum: metered ? sum : null,
|
|
||||||
});
|
|
||||||
currentDate = addHours(currentDate, 1);
|
|
||||||
if (!half && i > hours / 2) {
|
|
||||||
half = true;
|
|
||||||
}
|
|
||||||
i += half ? -1 : 1;
|
|
||||||
}
|
|
||||||
return statistics;
|
|
||||||
};
|
|
||||||
|
|
||||||
const statisticsFunctions: Record<
|
|
||||||
string,
|
|
||||||
(
|
|
||||||
id: string,
|
|
||||||
start: Date,
|
|
||||||
end: Date,
|
|
||||||
period: "5minute" | "hour" | "day" | "month"
|
|
||||||
) => StatisticValue[]
|
|
||||||
> = {
|
|
||||||
"sensor.energy_consumption_tarif_1": (
|
|
||||||
id: string,
|
|
||||||
start: Date,
|
|
||||||
end: Date,
|
|
||||||
period = "hour"
|
|
||||||
) => {
|
|
||||||
if (period !== "hour") {
|
|
||||||
return generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
period === "day" ? 17 : 504
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const morningEnd = new Date(start.getTime() + 10 * 60 * 60 * 1000);
|
|
||||||
const morningLow = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
morningEnd,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0.7
|
|
||||||
);
|
|
||||||
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
|
||||||
const morningFinalVal = morningLow.length
|
|
||||||
? morningLow[morningLow.length - 1].sum!
|
|
||||||
: 0;
|
|
||||||
const empty = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
morningEnd,
|
|
||||||
eveningStart,
|
|
||||||
period,
|
|
||||||
morningFinalVal,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
const eveningLow = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
eveningStart,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
morningFinalVal,
|
|
||||||
0.7
|
|
||||||
);
|
|
||||||
return [...morningLow, ...empty, ...eveningLow];
|
|
||||||
},
|
|
||||||
"sensor.energy_consumption_tarif_2": (
|
|
||||||
id: string,
|
|
||||||
start: Date,
|
|
||||||
end: Date,
|
|
||||||
period = "hour"
|
|
||||||
) => {
|
|
||||||
if (period !== "hour") {
|
|
||||||
return generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
period === "day" ? 17 : 504
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const morningEnd = new Date(start.getTime() + 9 * 60 * 60 * 1000);
|
|
||||||
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
|
||||||
const highTarif = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
morningEnd,
|
|
||||||
eveningStart,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0.3
|
|
||||||
);
|
|
||||||
const highTarifFinalVal = highTarif.length
|
|
||||||
? highTarif[highTarif.length - 1].sum!
|
|
||||||
: 0;
|
|
||||||
const morning = generateSumStatistics(id, start, morningEnd, period, 0, 0);
|
|
||||||
const evening = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
eveningStart,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
highTarifFinalVal,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
return [...morning, ...highTarif, ...evening];
|
|
||||||
},
|
|
||||||
"sensor.energy_production_tarif_1": (id, start, end, period = "hour") =>
|
|
||||||
generateSumStatistics(id, start, end, period, 0, 0),
|
|
||||||
"sensor.energy_production_tarif_1_compensation": (
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
period = "hour"
|
|
||||||
) => generateSumStatistics(id, start, end, period, 0, 0),
|
|
||||||
"sensor.energy_production_tarif_2": (id, start, end, period = "hour") => {
|
|
||||||
if (period !== "hour") {
|
|
||||||
return generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
period === "day" ? 17 : 504
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const productionStart = new Date(start.getTime() + 9 * 60 * 60 * 1000);
|
|
||||||
const productionEnd = new Date(start.getTime() + 21 * 60 * 60 * 1000);
|
|
||||||
const dayEnd = new Date(endOfDay(productionEnd));
|
|
||||||
const production = generateCurvedStatistics(
|
|
||||||
id,
|
|
||||||
productionStart,
|
|
||||||
productionEnd,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0.15,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
const productionFinalVal = production.length
|
|
||||||
? production[production.length - 1].sum!
|
|
||||||
: 0;
|
|
||||||
const morning = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
productionStart,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
const evening = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
productionEnd,
|
|
||||||
dayEnd,
|
|
||||||
period,
|
|
||||||
productionFinalVal,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
const rest = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
dayEnd,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
productionFinalVal,
|
|
||||||
1
|
|
||||||
);
|
|
||||||
return [...morning, ...production, ...evening, ...rest];
|
|
||||||
},
|
|
||||||
"sensor.solar_production": (id, start, end, period = "hour") => {
|
|
||||||
if (period !== "hour") {
|
|
||||||
return generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
period === "day" ? 17 : 504
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const productionStart = new Date(start.getTime() + 7 * 60 * 60 * 1000);
|
|
||||||
const productionEnd = new Date(start.getTime() + 23 * 60 * 60 * 1000);
|
|
||||||
const dayEnd = new Date(endOfDay(productionEnd));
|
|
||||||
const production = generateCurvedStatistics(
|
|
||||||
id,
|
|
||||||
productionStart,
|
|
||||||
productionEnd,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0.3,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
const productionFinalVal = production.length
|
|
||||||
? production[production.length - 1].sum!
|
|
||||||
: 0;
|
|
||||||
const morning = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
productionStart,
|
|
||||||
period,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
const evening = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
productionEnd,
|
|
||||||
dayEnd,
|
|
||||||
period,
|
|
||||||
productionFinalVal,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
const rest = generateSumStatistics(
|
|
||||||
id,
|
|
||||||
dayEnd,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
productionFinalVal,
|
|
||||||
2
|
|
||||||
);
|
|
||||||
return [...morning, ...production, ...evening, ...rest];
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export const mockHistory = (mockHass: MockHomeAssistant) => {
|
export const mockHistory = (mockHass: MockHomeAssistant) => {
|
||||||
mockHass.mockAPI(
|
mockHass.mockAPI(
|
||||||
new RegExp("history/period/.+"),
|
new RegExp("history/period/.+"),
|
||||||
@ -466,43 +133,4 @@ export const mockHistory = (mockHass: MockHomeAssistant) => {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
mockHass.mockWS("recorder/get_statistics_metadata", () => []);
|
|
||||||
mockHass.mockWS("history/list_statistic_ids", () => []);
|
|
||||||
mockHass.mockWS(
|
|
||||||
"history/statistics_during_period",
|
|
||||||
({ statistic_ids, start_time, end_time, period }, hass) => {
|
|
||||||
const start = new Date(start_time);
|
|
||||||
const end = end_time ? new Date(end_time) : new Date();
|
|
||||||
|
|
||||||
const statistics: Record<string, StatisticValue[]> = {};
|
|
||||||
|
|
||||||
statistic_ids.forEach((id: string) => {
|
|
||||||
if (id in statisticsFunctions) {
|
|
||||||
statistics[id] = statisticsFunctions[id](id, start, end, period);
|
|
||||||
} else {
|
|
||||||
const entityState = hass.states[id];
|
|
||||||
const state = entityState ? Number(entityState.state) : 1;
|
|
||||||
statistics[id] =
|
|
||||||
entityState && "last_reset" in entityState.attributes
|
|
||||||
? generateSumStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
state,
|
|
||||||
state * (state > 80 ? 0.01 : 0.05)
|
|
||||||
)
|
|
||||||
: generateMeanStatistics(
|
|
||||||
id,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
period,
|
|
||||||
state,
|
|
||||||
state * (state > 80 ? 0.05 : 0.1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return statistics;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
385
demo/src/stubs/recorder.ts
Normal file
385
demo/src/stubs/recorder.ts
Normal file
@ -0,0 +1,385 @@
|
|||||||
|
import {
|
||||||
|
addDays,
|
||||||
|
addHours,
|
||||||
|
addMonths,
|
||||||
|
differenceInHours,
|
||||||
|
endOfDay,
|
||||||
|
} from "date-fns";
|
||||||
|
import {
|
||||||
|
Statistics,
|
||||||
|
StatisticsMetaData,
|
||||||
|
StatisticValue,
|
||||||
|
} from "../../../src/data/recorder";
|
||||||
|
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
|
||||||
|
|
||||||
|
const generateMeanStatistics = (
|
||||||
|
id: string,
|
||||||
|
start: Date,
|
||||||
|
end: Date,
|
||||||
|
period: "5minute" | "hour" | "day" | "month" = "hour",
|
||||||
|
initValue: number,
|
||||||
|
maxDiff: number
|
||||||
|
): StatisticValue[] => {
|
||||||
|
const statistics: StatisticValue[] = [];
|
||||||
|
let currentDate = new Date(start);
|
||||||
|
currentDate.setMinutes(0, 0, 0);
|
||||||
|
let lastVal = initValue;
|
||||||
|
const now = new Date();
|
||||||
|
while (end > currentDate && currentDate < now) {
|
||||||
|
const delta = Math.random() * maxDiff;
|
||||||
|
const mean = lastVal + delta;
|
||||||
|
statistics.push({
|
||||||
|
statistic_id: id,
|
||||||
|
start: currentDate.toISOString(),
|
||||||
|
end: currentDate.toISOString(),
|
||||||
|
mean,
|
||||||
|
min: mean - Math.random() * maxDiff,
|
||||||
|
max: mean + Math.random() * maxDiff,
|
||||||
|
last_reset: "1970-01-01T00:00:00+00:00",
|
||||||
|
state: mean,
|
||||||
|
sum: null,
|
||||||
|
});
|
||||||
|
lastVal = mean;
|
||||||
|
currentDate =
|
||||||
|
period === "day"
|
||||||
|
? addDays(currentDate, 1)
|
||||||
|
: period === "month"
|
||||||
|
? addMonths(currentDate, 1)
|
||||||
|
: addHours(currentDate, 1);
|
||||||
|
}
|
||||||
|
return statistics;
|
||||||
|
};
|
||||||
|
|
||||||
|
const generateSumStatistics = (
|
||||||
|
id: string,
|
||||||
|
start: Date,
|
||||||
|
end: Date,
|
||||||
|
period: "5minute" | "hour" | "day" | "month" = "hour",
|
||||||
|
initValue: number,
|
||||||
|
maxDiff: number
|
||||||
|
): StatisticValue[] => {
|
||||||
|
const statistics: StatisticValue[] = [];
|
||||||
|
let currentDate = new Date(start);
|
||||||
|
currentDate.setMinutes(0, 0, 0);
|
||||||
|
let sum = initValue;
|
||||||
|
const now = new Date();
|
||||||
|
while (end > currentDate && currentDate < now) {
|
||||||
|
const add = Math.random() * maxDiff;
|
||||||
|
sum += add;
|
||||||
|
statistics.push({
|
||||||
|
statistic_id: id,
|
||||||
|
start: currentDate.toISOString(),
|
||||||
|
end: currentDate.toISOString(),
|
||||||
|
mean: null,
|
||||||
|
min: null,
|
||||||
|
max: null,
|
||||||
|
last_reset: "1970-01-01T00:00:00+00:00",
|
||||||
|
state: initValue + sum,
|
||||||
|
sum,
|
||||||
|
});
|
||||||
|
currentDate =
|
||||||
|
period === "day"
|
||||||
|
? addDays(currentDate, 1)
|
||||||
|
: period === "month"
|
||||||
|
? addMonths(currentDate, 1)
|
||||||
|
: addHours(currentDate, 1);
|
||||||
|
}
|
||||||
|
return statistics;
|
||||||
|
};
|
||||||
|
|
||||||
|
const generateCurvedStatistics = (
|
||||||
|
id: string,
|
||||||
|
start: Date,
|
||||||
|
end: Date,
|
||||||
|
_period: "5minute" | "hour" | "day" | "month" = "hour",
|
||||||
|
initValue: number,
|
||||||
|
maxDiff: number,
|
||||||
|
metered: boolean
|
||||||
|
): StatisticValue[] => {
|
||||||
|
const statistics: StatisticValue[] = [];
|
||||||
|
let currentDate = new Date(start);
|
||||||
|
currentDate.setMinutes(0, 0, 0);
|
||||||
|
let sum = initValue;
|
||||||
|
const hours = differenceInHours(end, start) - 1;
|
||||||
|
let i = 0;
|
||||||
|
let half = false;
|
||||||
|
const now = new Date();
|
||||||
|
while (end > currentDate && currentDate < now) {
|
||||||
|
const add = Math.random() * maxDiff;
|
||||||
|
sum += i * add;
|
||||||
|
statistics.push({
|
||||||
|
statistic_id: id,
|
||||||
|
start: currentDate.toISOString(),
|
||||||
|
end: currentDate.toISOString(),
|
||||||
|
mean: null,
|
||||||
|
min: null,
|
||||||
|
max: null,
|
||||||
|
last_reset: "1970-01-01T00:00:00+00:00",
|
||||||
|
state: initValue + sum,
|
||||||
|
sum: metered ? sum : null,
|
||||||
|
});
|
||||||
|
currentDate = addHours(currentDate, 1);
|
||||||
|
if (!half && i > hours / 2) {
|
||||||
|
half = true;
|
||||||
|
}
|
||||||
|
i += half ? -1 : 1;
|
||||||
|
}
|
||||||
|
return statistics;
|
||||||
|
};
|
||||||
|
|
||||||
|
const statisticsFunctions: Record<
|
||||||
|
string,
|
||||||
|
(
|
||||||
|
id: string,
|
||||||
|
start: Date,
|
||||||
|
end: Date,
|
||||||
|
period: "5minute" | "hour" | "day" | "month"
|
||||||
|
) => StatisticValue[]
|
||||||
|
> = {
|
||||||
|
"sensor.energy_consumption_tarif_1": (
|
||||||
|
id: string,
|
||||||
|
start: Date,
|
||||||
|
end: Date,
|
||||||
|
period = "hour"
|
||||||
|
) => {
|
||||||
|
if (period !== "hour") {
|
||||||
|
return generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
period === "day" ? 17 : 504
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const morningEnd = new Date(start.getTime() + 10 * 60 * 60 * 1000);
|
||||||
|
const morningLow = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
morningEnd,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
0.7
|
||||||
|
);
|
||||||
|
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
||||||
|
const morningFinalVal = morningLow.length
|
||||||
|
? morningLow[morningLow.length - 1].sum!
|
||||||
|
: 0;
|
||||||
|
const empty = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
morningEnd,
|
||||||
|
eveningStart,
|
||||||
|
period,
|
||||||
|
morningFinalVal,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
const eveningLow = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
eveningStart,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
morningFinalVal,
|
||||||
|
0.7
|
||||||
|
);
|
||||||
|
return [...morningLow, ...empty, ...eveningLow];
|
||||||
|
},
|
||||||
|
"sensor.energy_consumption_tarif_2": (
|
||||||
|
id: string,
|
||||||
|
start: Date,
|
||||||
|
end: Date,
|
||||||
|
period = "hour"
|
||||||
|
) => {
|
||||||
|
if (period !== "hour") {
|
||||||
|
return generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
period === "day" ? 17 : 504
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const morningEnd = new Date(start.getTime() + 9 * 60 * 60 * 1000);
|
||||||
|
const eveningStart = new Date(start.getTime() + 20 * 60 * 60 * 1000);
|
||||||
|
const highTarif = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
morningEnd,
|
||||||
|
eveningStart,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
0.3
|
||||||
|
);
|
||||||
|
const highTarifFinalVal = highTarif.length
|
||||||
|
? highTarif[highTarif.length - 1].sum!
|
||||||
|
: 0;
|
||||||
|
const morning = generateSumStatistics(id, start, morningEnd, period, 0, 0);
|
||||||
|
const evening = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
eveningStart,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
highTarifFinalVal,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
return [...morning, ...highTarif, ...evening];
|
||||||
|
},
|
||||||
|
"sensor.energy_production_tarif_1": (id, start, end, period = "hour") =>
|
||||||
|
generateSumStatistics(id, start, end, period, 0, 0),
|
||||||
|
"sensor.energy_production_tarif_1_compensation": (
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
period = "hour"
|
||||||
|
) => generateSumStatistics(id, start, end, period, 0, 0),
|
||||||
|
"sensor.energy_production_tarif_2": (id, start, end, period = "hour") => {
|
||||||
|
if (period !== "hour") {
|
||||||
|
return generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
period === "day" ? 17 : 504
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const productionStart = new Date(start.getTime() + 9 * 60 * 60 * 1000);
|
||||||
|
const productionEnd = new Date(start.getTime() + 21 * 60 * 60 * 1000);
|
||||||
|
const dayEnd = new Date(endOfDay(productionEnd));
|
||||||
|
const production = generateCurvedStatistics(
|
||||||
|
id,
|
||||||
|
productionStart,
|
||||||
|
productionEnd,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
0.15,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
const productionFinalVal = production.length
|
||||||
|
? production[production.length - 1].sum!
|
||||||
|
: 0;
|
||||||
|
const morning = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
productionStart,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
const evening = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
productionEnd,
|
||||||
|
dayEnd,
|
||||||
|
period,
|
||||||
|
productionFinalVal,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
const rest = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
dayEnd,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
productionFinalVal,
|
||||||
|
1
|
||||||
|
);
|
||||||
|
return [...morning, ...production, ...evening, ...rest];
|
||||||
|
},
|
||||||
|
"sensor.solar_production": (id, start, end, period = "hour") => {
|
||||||
|
if (period !== "hour") {
|
||||||
|
return generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
period === "day" ? 17 : 504
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const productionStart = new Date(start.getTime() + 7 * 60 * 60 * 1000);
|
||||||
|
const productionEnd = new Date(start.getTime() + 23 * 60 * 60 * 1000);
|
||||||
|
const dayEnd = new Date(endOfDay(productionEnd));
|
||||||
|
const production = generateCurvedStatistics(
|
||||||
|
id,
|
||||||
|
productionStart,
|
||||||
|
productionEnd,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
0.3,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
const productionFinalVal = production.length
|
||||||
|
? production[production.length - 1].sum!
|
||||||
|
: 0;
|
||||||
|
const morning = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
productionStart,
|
||||||
|
period,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
const evening = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
productionEnd,
|
||||||
|
dayEnd,
|
||||||
|
period,
|
||||||
|
productionFinalVal,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
const rest = generateSumStatistics(
|
||||||
|
id,
|
||||||
|
dayEnd,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
productionFinalVal,
|
||||||
|
2
|
||||||
|
);
|
||||||
|
return [...morning, ...production, ...evening, ...rest];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
export const mockRecorder = (mockHass: MockHomeAssistant) => {
|
||||||
|
mockHass.mockWS(
|
||||||
|
"recorder/get_statistics_metadata",
|
||||||
|
(): StatisticsMetaData[] => []
|
||||||
|
);
|
||||||
|
mockHass.mockWS(
|
||||||
|
"recorder/list_statistic_ids",
|
||||||
|
(): StatisticsMetaData[] => []
|
||||||
|
);
|
||||||
|
mockHass.mockWS(
|
||||||
|
"recorder/statistics_during_period",
|
||||||
|
({ statistic_ids, start_time, end_time, period }, hass): Statistics => {
|
||||||
|
const start = new Date(start_time);
|
||||||
|
const end = end_time ? new Date(end_time) : new Date();
|
||||||
|
|
||||||
|
const statistics: Record<string, StatisticValue[]> = {};
|
||||||
|
|
||||||
|
statistic_ids.forEach((id: string) => {
|
||||||
|
if (id in statisticsFunctions) {
|
||||||
|
statistics[id] = statisticsFunctions[id](id, start, end, period);
|
||||||
|
} else {
|
||||||
|
const entityState = hass.states[id];
|
||||||
|
const state = entityState ? Number(entityState.state) : 1;
|
||||||
|
statistics[id] =
|
||||||
|
entityState && "last_reset" in entityState.attributes
|
||||||
|
? generateSumStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
state,
|
||||||
|
state * (state > 80 ? 0.01 : 0.05)
|
||||||
|
)
|
||||||
|
: generateMeanStatistics(
|
||||||
|
id,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
period,
|
||||||
|
state,
|
||||||
|
state * (state > 80 ? 0.05 : 0.1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return statistics;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
@ -618,7 +618,7 @@ export const getEnergyGasUnitClass = (
|
|||||||
const statisticIdWithMeta = statisticsMetaData[source.stat_energy_from];
|
const statisticIdWithMeta = statisticsMetaData[source.stat_energy_from];
|
||||||
if (
|
if (
|
||||||
energyGasUnitClass.includes(
|
energyGasUnitClass.includes(
|
||||||
statisticIdWithMeta.unit_class as EnergyGasUnitClass
|
statisticIdWithMeta?.unit_class as EnergyGasUnitClass
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
return statisticIdWithMeta.unit_class as EnergyGasUnitClass;
|
return statisticIdWithMeta.unit_class as EnergyGasUnitClass;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user