mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 08:16:36 +00:00
Add devices to energy collection (#19849)
* Include individual devices in energy collection * async cleanup, fix includeTypes filter
This commit is contained in:
parent
267fc3743d
commit
47f7cf5419
@ -331,6 +331,9 @@ export const getReferencedStatisticIds = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!(includeTypes && !includeTypes.includes("device"))) {
|
||||||
|
statIDs.push(...prefs.device_consumption.map((d) => d.stat_consumption));
|
||||||
|
}
|
||||||
|
|
||||||
return statIDs;
|
return statIDs;
|
||||||
};
|
};
|
||||||
@ -383,6 +386,7 @@ const getEnergyData = async (
|
|||||||
"solar",
|
"solar",
|
||||||
"battery",
|
"battery",
|
||||||
"gas",
|
"gas",
|
||||||
|
"device",
|
||||||
]);
|
]);
|
||||||
const waterStatIds = getReferencedStatisticIds(prefs, info, ["water"]);
|
const waterStatIds = getReferencedStatisticIds(prefs, info, ["water"]);
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ import {
|
|||||||
ChartOptions,
|
ChartOptions,
|
||||||
ScatterDataPoint,
|
ScatterDataPoint,
|
||||||
} from "chart.js";
|
} from "chart.js";
|
||||||
import { differenceInDays, endOfToday, startOfToday } from "date-fns/esm";
|
import { endOfToday, startOfToday } from "date-fns/esm";
|
||||||
import { HassConfig, UnsubscribeFunc } from "home-assistant-js-websocket";
|
import { HassConfig, UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import {
|
import {
|
||||||
css,
|
css,
|
||||||
@ -27,11 +27,9 @@ import {
|
|||||||
} from "../../../../data/energy";
|
} from "../../../../data/energy";
|
||||||
import {
|
import {
|
||||||
calculateStatisticSumGrowth,
|
calculateStatisticSumGrowth,
|
||||||
fetchStatistics,
|
|
||||||
getStatisticLabel,
|
getStatisticLabel,
|
||||||
Statistics,
|
Statistics,
|
||||||
StatisticsMetaData,
|
StatisticsMetaData,
|
||||||
StatisticsUnitConfiguration,
|
|
||||||
} from "../../../../data/recorder";
|
} from "../../../../data/recorder";
|
||||||
import { FrontendLocaleData } from "../../../../data/translation";
|
import { FrontendLocaleData } from "../../../../data/translation";
|
||||||
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
||||||
@ -41,6 +39,8 @@ import { EnergyDevicesDetailGraphCardConfig } from "../types";
|
|||||||
import { hasConfigChanged } from "../../common/has-changed";
|
import { hasConfigChanged } from "../../common/has-changed";
|
||||||
import { getCommonOptions } from "./common/energy-chart-options";
|
import { getCommonOptions } from "./common/energy-chart-options";
|
||||||
|
|
||||||
|
const UNIT = "kWh";
|
||||||
|
|
||||||
@customElement("hui-energy-devices-detail-graph-card")
|
@customElement("hui-energy-devices-detail-graph-card")
|
||||||
export class HuiEnergyDevicesDetailGraphCard
|
export class HuiEnergyDevicesDetailGraphCard
|
||||||
extends SubscribeMixin(LitElement)
|
extends SubscribeMixin(LitElement)
|
||||||
@ -56,10 +56,6 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
|
|
||||||
@state() private _data?: EnergyData;
|
@state() private _data?: EnergyData;
|
||||||
|
|
||||||
@state() private _statistics?: Statistics;
|
|
||||||
|
|
||||||
@state() private _compareStatistics?: Statistics;
|
|
||||||
|
|
||||||
@state() private _start = startOfToday();
|
@state() private _start = startOfToday();
|
||||||
|
|
||||||
@state() private _end = endOfToday();
|
@state() private _end = endOfToday();
|
||||||
@ -68,8 +64,6 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
|
|
||||||
@state() private _compareEnd?: Date;
|
@state() private _compareEnd?: Date;
|
||||||
|
|
||||||
@state() private _unit?: string;
|
|
||||||
|
|
||||||
@state() private _hiddenStats = new Set<string>();
|
@state() private _hiddenStats = new Set<string>();
|
||||||
|
|
||||||
protected hassSubscribeRequiredHostProps = ["_config"];
|
protected hassSubscribeRequiredHostProps = ["_config"];
|
||||||
@ -78,9 +72,8 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
return [
|
return [
|
||||||
getEnergyDataCollection(this.hass, {
|
getEnergyDataCollection(this.hass, {
|
||||||
key: this._config?.collection_key,
|
key: this._config?.collection_key,
|
||||||
}).subscribe(async (data) => {
|
}).subscribe((data) => {
|
||||||
this._data = data;
|
this._data = data;
|
||||||
await this._getStatistics(this._data);
|
|
||||||
this._processStatistics();
|
this._processStatistics();
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
@ -103,7 +96,7 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected willUpdate(changedProps: PropertyValues) {
|
protected willUpdate(changedProps: PropertyValues) {
|
||||||
if (changedProps.has("_hiddenStats") && this._statistics) {
|
if (changedProps.has("_hiddenStats") && this._data) {
|
||||||
this._processStatistics();
|
this._processStatistics();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,7 +126,7 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
this._end,
|
this._end,
|
||||||
this.hass.locale,
|
this.hass.locale,
|
||||||
this.hass.config,
|
this.hass.config,
|
||||||
this._unit,
|
UNIT,
|
||||||
this._compareStart,
|
this._compareStart,
|
||||||
this._compareEnd
|
this._compareEnd
|
||||||
)}
|
)}
|
||||||
@ -201,57 +194,10 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
private async _getStatistics(energyData: EnergyData): Promise<void> {
|
private _processStatistics() {
|
||||||
const dayDifference = differenceInDays(
|
|
||||||
energyData.end || new Date(),
|
|
||||||
energyData.start
|
|
||||||
);
|
|
||||||
|
|
||||||
const devices = energyData.prefs.device_consumption.map(
|
|
||||||
(device) => device.stat_consumption
|
|
||||||
);
|
|
||||||
|
|
||||||
const period =
|
|
||||||
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour";
|
|
||||||
|
|
||||||
const lengthUnit = this.hass.config.unit_system.length || "";
|
|
||||||
const units: StatisticsUnitConfiguration = {
|
|
||||||
energy: "kWh",
|
|
||||||
volume: lengthUnit === "km" ? "m³" : "ft³",
|
|
||||||
};
|
|
||||||
this._unit = "kWh";
|
|
||||||
|
|
||||||
const statistics = await fetchStatistics(
|
|
||||||
this.hass,
|
|
||||||
energyData.start,
|
|
||||||
energyData.end,
|
|
||||||
devices,
|
|
||||||
period,
|
|
||||||
units,
|
|
||||||
["change"]
|
|
||||||
);
|
|
||||||
|
|
||||||
let compareStatistics: Statistics | undefined;
|
|
||||||
|
|
||||||
if (energyData.startCompare && energyData.endCompare) {
|
|
||||||
compareStatistics = await fetchStatistics(
|
|
||||||
this.hass,
|
|
||||||
energyData.startCompare,
|
|
||||||
energyData.endCompare,
|
|
||||||
devices,
|
|
||||||
period,
|
|
||||||
units,
|
|
||||||
["change"]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
this._statistics = statistics;
|
|
||||||
this._compareStatistics = compareStatistics;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async _processStatistics() {
|
|
||||||
const energyData = this._data!;
|
const energyData = this._data!;
|
||||||
const data = this._statistics!;
|
const data = energyData.stats;
|
||||||
const compareData = this._compareStatistics;
|
const compareData = energyData.statsCompare;
|
||||||
|
|
||||||
const growthValues = {};
|
const growthValues = {};
|
||||||
energyData.prefs.device_consumption.forEach((device) => {
|
energyData.prefs.device_consumption.forEach((device) => {
|
||||||
|
@ -6,7 +6,6 @@ import {
|
|||||||
ScatterDataPoint,
|
ScatterDataPoint,
|
||||||
} from "chart.js";
|
} from "chart.js";
|
||||||
import { getRelativePosition } from "chart.js/helpers";
|
import { getRelativePosition } from "chart.js/helpers";
|
||||||
import { differenceInDays } from "date-fns/esm";
|
|
||||||
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import {
|
import {
|
||||||
css,
|
css,
|
||||||
@ -31,10 +30,7 @@ import "../../../../components/ha-card";
|
|||||||
import { EnergyData, getEnergyDataCollection } from "../../../../data/energy";
|
import { EnergyData, getEnergyDataCollection } from "../../../../data/energy";
|
||||||
import {
|
import {
|
||||||
calculateStatisticSumGrowth,
|
calculateStatisticSumGrowth,
|
||||||
fetchStatistics,
|
|
||||||
getStatisticLabel,
|
getStatisticLabel,
|
||||||
Statistics,
|
|
||||||
StatisticsUnitConfiguration,
|
|
||||||
} from "../../../../data/recorder";
|
} from "../../../../data/recorder";
|
||||||
import { FrontendLocaleData } from "../../../../data/translation";
|
import { FrontendLocaleData } from "../../../../data/translation";
|
||||||
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
||||||
@ -186,47 +182,8 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
);
|
);
|
||||||
|
|
||||||
private async _getStatistics(energyData: EnergyData): Promise<void> {
|
private async _getStatistics(energyData: EnergyData): Promise<void> {
|
||||||
const dayDifference = differenceInDays(
|
const data = energyData.stats;
|
||||||
energyData.end || new Date(),
|
const compareData = energyData.statsCompare;
|
||||||
energyData.start
|
|
||||||
);
|
|
||||||
|
|
||||||
const devices = energyData.prefs.device_consumption.map(
|
|
||||||
(device) => device.stat_consumption
|
|
||||||
);
|
|
||||||
|
|
||||||
const period =
|
|
||||||
dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour";
|
|
||||||
|
|
||||||
const lengthUnit = this.hass.config.unit_system.length || "";
|
|
||||||
const units: StatisticsUnitConfiguration = {
|
|
||||||
energy: "kWh",
|
|
||||||
volume: lengthUnit === "km" ? "m³" : "ft³",
|
|
||||||
};
|
|
||||||
|
|
||||||
const data = await fetchStatistics(
|
|
||||||
this.hass,
|
|
||||||
energyData.start,
|
|
||||||
energyData.end,
|
|
||||||
devices,
|
|
||||||
period,
|
|
||||||
units,
|
|
||||||
["change"]
|
|
||||||
);
|
|
||||||
|
|
||||||
let compareData: Statistics | undefined;
|
|
||||||
|
|
||||||
if (energyData.startCompare && energyData.endCompare) {
|
|
||||||
compareData = await fetchStatistics(
|
|
||||||
this.hass,
|
|
||||||
energyData.startCompare,
|
|
||||||
energyData.endCompare,
|
|
||||||
devices,
|
|
||||||
period,
|
|
||||||
units,
|
|
||||||
["change"]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const chartData: Array<ChartDataset<"bar", ParsedDataType<"bar">>["data"]> =
|
const chartData: Array<ChartDataset<"bar", ParsedDataType<"bar">>["data"]> =
|
||||||
[];
|
[];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user