Refactor getStatisticLabel (#13486)

This commit is contained in:
Erik Montnemery 2022-08-25 14:53:16 +02:00 committed by GitHub
parent a8833a5ec1
commit 5d1536030a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 26 additions and 27 deletions

View File

@ -238,11 +238,7 @@ class StatisticsChart extends LitElement {
); );
let name = names[firstStat.statistic_id]; let name = names[firstStat.statistic_id];
if (!name) { if (!name) {
const tmp: Record<string, StatisticsMetaData> = {}; name = getStatisticLabel(this.hass, firstStat.statistic_id, meta);
if (meta) {
tmp[firstStat.statistic_id] = meta;
}
name = getStatisticLabel(this.hass, firstStat.statistic_id, tmp);
} }
if (!this.unit) { if (!this.unit) {

View File

@ -569,12 +569,11 @@ export const adjustStatisticsSum = (
export const getStatisticLabel = ( export const getStatisticLabel = (
hass: HomeAssistant, hass: HomeAssistant,
statisticsId: string, statisticsId: string,
statisticsMetaData: Record<string, StatisticsMetaData> | undefined statisticsMetaData: StatisticsMetaData | undefined
): string => { ): string => {
const entity = hass.states[statisticsId]; const entity = hass.states[statisticsId];
if (entity) { if (entity) {
return computeStateName(entity); return computeStateName(entity);
} }
const statisticMetaData = statisticsMetaData?.[statisticsId]; return statisticsMetaData?.name || statisticsId;
return statisticMetaData?.name || statisticsId;
}; };

View File

@ -104,14 +104,14 @@ export class EnergyBatterySettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this.statsMetadata this.statsMetadata[source.stat_energy_from]
)}</span )}</span
> >
<span <span
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_to, source.stat_energy_to,
this.statsMetadata this.statsMetadata[source.stat_energy_to]
)}</span )}</span
> >
</div> </div>

View File

@ -90,7 +90,7 @@ export class EnergyDeviceSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
device.stat_consumption, device.stat_consumption,
this.statsMetadata this.statsMetadata[device.stat_consumption]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button

View File

@ -98,7 +98,7 @@ export class EnergyGasSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this.statsMetadata this.statsMetadata[source.stat_energy_from]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button

View File

@ -136,7 +136,7 @@ export class EnergyGridSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
flow.stat_energy_from, flow.stat_energy_from,
this.statsMetadata this.statsMetadata[flow.stat_energy_from]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button
@ -183,7 +183,7 @@ export class EnergyGridSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
flow.stat_energy_to, flow.stat_energy_to,
this.statsMetadata this.statsMetadata[flow.stat_energy_to]
)}</span )}</span
> >
<ha-icon-button <ha-icon-button

View File

@ -106,7 +106,7 @@ export class EnergySolarSettings extends LitElement {
>${getStatisticLabel( >${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this.statsMetadata this.statsMetadata[source.stat_energy_from]
)}</span )}</span
> >
${this.info ${this.info

View File

@ -116,7 +116,7 @@ export class HuiEnergyDevicesGraphCard
return getStatisticLabel( return getStatisticLabel(
this.hass, this.hass,
statisticId as any, statisticId as any,
this._data?.statsMetadata this._data?.statsMetadata[statisticId]
); );
}, },
}, },
@ -138,7 +138,7 @@ export class HuiEnergyDevicesGraphCard
return getStatisticLabel( return getStatisticLabel(
this.hass, this.hass,
statisticId, statisticId,
this._data?.statsMetadata this._data?.statsMetadata[statisticId]
); );
}, },
label: (context) => label: (context) =>

View File

@ -382,7 +382,7 @@ export class HuiEnergyGasGraphCard
label: getStatisticLabel( label: getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
statisticsMetaData statisticsMetaData[source.stat_energy_from]
), ),
borderColor: compare ? borderColor + "7F" : borderColor, borderColor: compare ? borderColor + "7F" : borderColor,
backgroundColor: compare ? borderColor + "32" : borderColor + "7F", backgroundColor: compare ? borderColor + "32" : borderColor + "7F",

View File

@ -405,7 +405,7 @@ export class HuiEnergySolarGraphCard
name: getStatisticLabel( name: getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
statisticsMetaData statisticsMetaData[source.stat_energy_from]
), ),
} }
), ),
@ -483,7 +483,7 @@ export class HuiEnergySolarGraphCard
name: getStatisticLabel( name: getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
statisticsMetaData statisticsMetaData[source.stat_energy_from]
), ),
} }
), ),

View File

@ -239,7 +239,7 @@ export class HuiEnergySourcesTableCard
${getStatisticLabel( ${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this._data?.statsMetadata this._data?.statsMetadata[source.stat_energy_from]
)} )}
</th> </th>
${compare ${compare
@ -347,7 +347,7 @@ export class HuiEnergySourcesTableCard
${getStatisticLabel( ${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this._data?.statsMetadata this._data?.statsMetadata[source.stat_energy_from]
)} )}
</th> </th>
${compare ${compare
@ -384,7 +384,7 @@ export class HuiEnergySourcesTableCard
${getStatisticLabel( ${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_to, source.stat_energy_to,
this._data?.statsMetadata this._data?.statsMetadata[source.stat_energy_to]
)} )}
</th> </th>
${compare ${compare
@ -505,7 +505,7 @@ export class HuiEnergySourcesTableCard
${getStatisticLabel( ${getStatisticLabel(
this.hass, this.hass,
flow.stat_energy_from, flow.stat_energy_from,
this._data?.statsMetadata this._data?.statsMetadata[flow.stat_energy_from]
)} )}
</th> </th>
${compare ${compare
@ -610,7 +610,7 @@ export class HuiEnergySourcesTableCard
${getStatisticLabel( ${getStatisticLabel(
this.hass, this.hass,
flow.stat_energy_to, flow.stat_energy_to,
this._data?.statsMetadata this._data?.statsMetadata[flow.stat_energy_to]
)} )}
</th> </th>
${compare ${compare
@ -763,7 +763,7 @@ export class HuiEnergySourcesTableCard
${getStatisticLabel( ${getStatisticLabel(
this.hass, this.hass,
source.stat_energy_from, source.stat_energy_from,
this._data?.statsMetadata this._data?.statsMetadata[source.stat_energy_from]
)} )}
</th> </th>
${compare ${compare

View File

@ -620,7 +620,11 @@ export class HuiEnergyUsageGraphCard
label: label:
type in labels type in labels
? labels[type] ? labels[type]
: getStatisticLabel(this.hass, statId, statisticsMetaData), : getStatisticLabel(
this.hass,
statId,
statisticsMetaData[statId]
),
order: order:
type === "used_solar" type === "used_solar"
? 1 ? 1