diff --git a/src/data/energy.ts b/src/data/energy.ts index b4fb94845e..68d18a8e43 100644 --- a/src/data/energy.ts +++ b/src/data/energy.ts @@ -248,6 +248,62 @@ export interface EnergyData { fossilEnergyConsumptionCompare?: FossilEnergyConsumption; } +export const getReferencedStatisticIds = ( + prefs: EnergyPreferences, + info: EnergyInfo +): string[] => { + const statIDs: string[] = []; + + for (const source of prefs.energy_sources) { + if (source.type === "solar") { + statIDs.push(source.stat_energy_from); + continue; + } + + if (source.type === "gas") { + statIDs.push(source.stat_energy_from); + if (source.stat_cost) { + statIDs.push(source.stat_cost); + } + const costStatId = info.cost_sensors[source.stat_energy_from]; + if (costStatId) { + statIDs.push(costStatId); + } + continue; + } + + if (source.type === "battery") { + statIDs.push(source.stat_energy_from); + statIDs.push(source.stat_energy_to); + continue; + } + + // grid source + for (const flowFrom of source.flow_from) { + statIDs.push(flowFrom.stat_energy_from); + if (flowFrom.stat_cost) { + statIDs.push(flowFrom.stat_cost); + } + const costStatId = info.cost_sensors[flowFrom.stat_energy_from]; + if (costStatId) { + statIDs.push(costStatId); + } + } + for (const flowTo of source.flow_to) { + statIDs.push(flowTo.stat_energy_to); + if (flowTo.stat_compensation) { + statIDs.push(flowTo.stat_compensation); + } + const costStatId = info.cost_sensors[flowTo.stat_energy_to]; + if (costStatId) { + statIDs.push(costStatId); + } + } + } + + return statIDs; +}; + const getEnergyData = async ( hass: HomeAssistant, prefs: EnergyPreferences, @@ -285,55 +341,15 @@ const getEnergyData = async ( } const consumptionStatIDs: string[] = []; - const statIDs: string[] = []; - for (const source of prefs.energy_sources) { - if (source.type === "solar") { - statIDs.push(source.stat_energy_from); - continue; - } - - if (source.type === "gas") { - statIDs.push(source.stat_energy_from); - if (source.stat_cost) { - statIDs.push(source.stat_cost); - } - const costStatId = info.cost_sensors[source.stat_energy_from]; - if (costStatId) { - statIDs.push(costStatId); - } - continue; - } - - if (source.type === "battery") { - statIDs.push(source.stat_energy_from); - statIDs.push(source.stat_energy_to); - continue; - } - // grid source - for (const flowFrom of source.flow_from) { - consumptionStatIDs.push(flowFrom.stat_energy_from); - statIDs.push(flowFrom.stat_energy_from); - if (flowFrom.stat_cost) { - statIDs.push(flowFrom.stat_cost); - } - const costStatId = info.cost_sensors[flowFrom.stat_energy_from]; - if (costStatId) { - statIDs.push(costStatId); - } - } - for (const flowTo of source.flow_to) { - statIDs.push(flowTo.stat_energy_to); - if (flowTo.stat_compensation) { - statIDs.push(flowTo.stat_compensation); - } - const costStatId = info.cost_sensors[flowTo.stat_energy_to]; - if (costStatId) { - statIDs.push(costStatId); + if (source.type === "grid") { + for (const flowFrom of source.flow_from) { + consumptionStatIDs.push(flowFrom.stat_energy_from); } } } + const statIDs = getReferencedStatisticIds(prefs, info); const dayDifference = differenceInDays(end || new Date(), start); const period = diff --git a/src/panels/config/energy/components/ha-energy-battery-settings.ts b/src/panels/config/energy/components/ha-energy-battery-settings.ts index 6879a34c96..e674658b43 100644 --- a/src/panels/config/energy/components/ha-energy-battery-settings.ts +++ b/src/panels/config/energy/components/ha-energy-battery-settings.ts @@ -3,7 +3,6 @@ import { mdiBatteryHigh, mdiDelete, mdiPencil } from "@mdi/js"; import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { customElement, property } from "lit/decorators"; import { fireEvent } from "../../../../common/dom/fire_event"; -import { computeStateName } from "../../../../common/entity/compute_state_name"; import "../../../../components/ha-card"; import "../../../../components/ha-icon-button"; import "../../../../components/ha-settings-row"; @@ -14,6 +13,10 @@ import { EnergyValidationIssue, saveEnergyPreferences, } from "../../../../data/energy"; +import { + StatisticsMetaData, + getStatisticLabel, +} from "../../../../data/history"; import { showAlertDialog, showConfirmationDialog, @@ -32,6 +35,9 @@ export class EnergyBatterySettings extends LitElement { @property({ attribute: false }) public preferences!: EnergyPreferences; + @property({ attribute: false }) + public statsMetadata!: Record; + @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -85,7 +91,6 @@ export class EnergyBatterySettings extends LitElement { )} ${batterySources.map((source) => { - const fromEntityState = this.hass.states[source.stat_energy_from]; const toEntityState = this.hass.states[source.stat_energy_to]; return html`
@@ -96,14 +101,18 @@ export class EnergyBatterySettings extends LitElement { : html``}
${toEntityState - ? computeStateName(toEntityState) - : source.stat_energy_from}${getStatisticLabel( + this.hass, + source.stat_energy_from, + this.statsMetadata + )} ${fromEntityState - ? computeStateName(fromEntityState) - : source.stat_energy_to}${getStatisticLabel( + this.hass, + source.stat_energy_to, + this.statsMetadata + )}
; + @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -81,9 +87,11 @@ export class EnergyDeviceSettings extends LitElement {
${entityState - ? computeStateName(entityState) - : device.stat_consumption}${getStatisticLabel( + this.hass, + device.stat_consumption, + this.statsMetadata + )} ; + @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -89,9 +95,11 @@ export class EnergyGasSettings extends LitElement { >` : html``} ${entityState - ? computeStateName(entityState) - : source.stat_energy_from}${getStatisticLabel( + this.hass, + source.stat_energy_from, + this.statsMetadata + )} ; + @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -127,9 +133,11 @@ export class EnergyGridSettings extends LitElement { .path=${mdiHomeImportOutline} >`} ${entityState - ? computeStateName(entityState) - : flow.stat_energy_from}${getStatisticLabel( + this.hass, + flow.stat_energy_from, + this.statsMetadata + )} `} ${entityState - ? computeStateName(entityState) - : flow.stat_energy_to}${getStatisticLabel( + this.hass, + flow.stat_energy_to, + this.statsMetadata + )} ; + @property({ attribute: false }) public validationResult?: EnergyPreferencesValidation; @@ -97,9 +103,11 @@ export class EnergySolarSettings extends LitElement { >` : html``} ${entityState - ? computeStateName(entityState) - : source.stat_energy_from}${getStatisticLabel( + this.hass, + source.stat_energy_from, + this.statsMetadata + )} ${this.info ? html` diff --git a/src/panels/config/energy/ha-config-energy.ts b/src/panels/config/energy/ha-config-energy.ts index 44c0f14d66..c6a2d5b150 100644 --- a/src/panels/config/energy/ha-config-energy.ts +++ b/src/panels/config/energy/ha-config-energy.ts @@ -8,7 +8,12 @@ import { EnergyPreferences, getEnergyInfo, getEnergyPreferences, + getReferencedStatisticIds, } from "../../../data/energy"; +import { + getStatisticMetadata, + StatisticsMetaData, +} from "../../../data/history"; import "../../../layouts/hass-loading-screen"; import "../../../layouts/hass-subpage"; import { haStyle } from "../../../resources/styles"; @@ -47,6 +52,8 @@ class HaConfigEnergy extends LitElement { @state() private _error?: string; + @state() private _statsMetadata?: Record; + protected firstUpdated() { this._fetchConfig(); } @@ -83,12 +90,14 @@ class HaConfigEnergy extends LitElement { @@ -136,6 +148,7 @@ class HaConfigEnergy extends LitElement { this._error = err.message; } this._info = await energyInfoPromise; + await this._fetchMetaData(); } private async _prefsChanged(ev: CustomEvent) { @@ -147,6 +160,20 @@ class HaConfigEnergy extends LitElement { this._error = err.message; } this._info = await getEnergyInfo(this.hass); + await this._fetchMetaData(); + } + + private async _fetchMetaData() { + if (!this._preferences || !this._info) { + return; + } + const statIDs = getReferencedStatisticIds(this._preferences, this._info); + const statsMetadataArray = await getStatisticMetadata(this.hass, statIDs); + const statsMetadata: Record = {}; + statsMetadataArray.forEach((x) => { + statsMetadata[x.statistic_id] = x; + }); + this._statsMetadata = statsMetadata; } static get styles(): CSSResultGroup {