From ccf1fb573a3c2c395f50e5ffcab0528dbb6011c5 Mon Sep 17 00:00:00 2001 From: Pawel Date: Tue, 22 Mar 2022 05:15:28 +0100 Subject: [PATCH] Fix gas energy graph units if stats added by external source (#11892) --- src/data/energy.ts | 34 ++++++++++++++++++- src/data/history.ts | 9 +++++ .../components/ha-energy-gas-settings.ts | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/data/energy.ts b/src/data/energy.ts index d86419e9b2..7fd941b325 100644 --- a/src/data/energy.ts +++ b/src/data/energy.ts @@ -12,7 +12,12 @@ import { subscribeOne } from "../common/util/subscribe-one"; import { HomeAssistant } from "../types"; import { ConfigEntry, getConfigEntries } from "./config_entries"; import { subscribeEntityRegistry } from "./entity_registry"; -import { fetchStatistics, Statistics } from "./history"; +import { + fetchStatistics, + Statistics, + StatisticsMetaData, + getStatisticMetadata, +} from "./history"; const energyCollectionKeys: (string | undefined)[] = []; @@ -136,6 +141,7 @@ export interface GasSourceTypeEnergyPreference { entity_energy_from: string | null; entity_energy_price: string | null; number_energy_price: number | null; + unit_of_measurement?: string | null; } type EnergySource = @@ -271,6 +277,15 @@ const getEnergyData = async ( const consumptionStatIDs: string[] = []; const statIDs: string[] = []; + const gasSources: GasSourceTypeEnergyPreference[] = + prefs.energy_sources.filter( + (source) => source.type === "gas" + ) as GasSourceTypeEnergyPreference[]; + const gasStatisticIdsWithMeta: StatisticsMetaData[] = + await getStatisticMetadata( + hass, + gasSources.map((source) => source.stat_energy_from) + ); for (const source of prefs.energy_sources) { if (source.type === "solar") { @@ -280,6 +295,20 @@ const getEnergyData = async ( if (source.type === "gas") { statIDs.push(source.stat_energy_from); + const entity = hass.states[source.stat_energy_from]; + if (!entity) { + for (const statisticIdWithMeta of gasStatisticIdsWithMeta) { + if ( + statisticIdWithMeta?.statistic_id === source.stat_energy_from && + statisticIdWithMeta?.unit_of_measurement + ) { + source.unit_of_measurement = + statisticIdWithMeta?.unit_of_measurement === "Wh" + ? "kWh" + : statisticIdWithMeta?.unit_of_measurement; + } + } + } if (source.stat_cost) { statIDs.push(source.stat_cost); } @@ -559,6 +588,9 @@ export const getEnergyGasUnit = ( ? "kWh" : entity.attributes.unit_of_measurement; } + if (source.unit_of_measurement) { + return source.unit_of_measurement; + } } return undefined; }; diff --git a/src/data/history.ts b/src/data/history.ts index c81c592637..eae1b3c9b3 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -373,6 +373,15 @@ export const getStatisticIds = ( statistic_type, }); +export const getStatisticMetadata = ( + hass: HomeAssistant, + statistic_ids?: string[] +) => + hass.callWS({ + type: "recorder/get_statistics_metadata", + statistic_ids, + }); + export const fetchStatistics = ( hass: HomeAssistant, startTime: Date, diff --git a/src/panels/config/energy/components/ha-energy-gas-settings.ts b/src/panels/config/energy/components/ha-energy-gas-settings.ts index 3f92d1a023..4e65150f2b 100644 --- a/src/panels/config/energy/components/ha-energy-gas-settings.ts +++ b/src/panels/config/energy/components/ha-energy-gas-settings.ts @@ -121,6 +121,7 @@ export class EnergyGasSettings extends LitElement { showEnergySettingsGasDialog(this, { unit: getEnergyGasUnitCategory(this.hass, this.preferences), saveCallback: async (source) => { + delete source.unit_of_measurement; await this._savePreferences({ ...this.preferences, energy_sources: this.preferences.energy_sources.concat(source),