mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-27 06:47:20 +00:00
Fix energy device graph when multiple entities have same name (#10092)
This commit is contained in:
parent
a89caccd32
commit
21e14bd644
@ -21,7 +21,11 @@ import {
|
|||||||
import "../../../../components/chart/ha-chart-base";
|
import "../../../../components/chart/ha-chart-base";
|
||||||
import type HaChartBase from "../../../../components/chart/ha-chart-base";
|
import type HaChartBase from "../../../../components/chart/ha-chart-base";
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
import { EnergyData, getEnergyDataCollection } from "../../../../data/energy";
|
import {
|
||||||
|
DeviceConsumptionEnergyPreference,
|
||||||
|
EnergyData,
|
||||||
|
getEnergyDataCollection,
|
||||||
|
} from "../../../../data/energy";
|
||||||
import {
|
import {
|
||||||
calculateStatisticSumGrowth,
|
calculateStatisticSumGrowth,
|
||||||
fetchStatistics,
|
fetchStatistics,
|
||||||
@ -48,6 +52,8 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
|
|
||||||
@query("ha-chart-base") private _chart?: HaChartBase;
|
@query("ha-chart-base") private _chart?: HaChartBase;
|
||||||
|
|
||||||
|
private _deviceConsumptionPrefs: DeviceConsumptionEnergyPreference[] = [];
|
||||||
|
|
||||||
public hassSubscribe(): UnsubscribeFunc[] {
|
public hassSubscribe(): UnsubscribeFunc[] {
|
||||||
return [
|
return [
|
||||||
getEnergyDataCollection(this.hass, {
|
getEnergyDataCollection(this.hass, {
|
||||||
@ -101,7 +107,16 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
scales: {
|
scales: {
|
||||||
y: {
|
y: {
|
||||||
type: "category",
|
type: "category",
|
||||||
ticks: { autoSkip: false },
|
ticks: {
|
||||||
|
autoSkip: false,
|
||||||
|
callback: (index) => {
|
||||||
|
const devicePref = this._deviceConsumptionPrefs[index];
|
||||||
|
const entity = this.hass.states[devicePref.stat_consumption];
|
||||||
|
return entity
|
||||||
|
? computeStateName(entity)
|
||||||
|
: devicePref.stat_consumption;
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
x: {
|
x: {
|
||||||
title: {
|
title: {
|
||||||
@ -134,13 +149,15 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
);
|
);
|
||||||
fireEvent(this, "hass-more-info", {
|
fireEvent(this, "hass-more-info", {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
entityId: this._chartData?.datasets[0]?.data[index]?.entity_id,
|
entityId: this._chartData?.datasets[0]?.data[index]?.label,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
private async _getStatistics(energyData: EnergyData): Promise<void> {
|
private async _getStatistics(energyData: EnergyData): Promise<void> {
|
||||||
|
this._deviceConsumptionPrefs = energyData.prefs.device_consumption;
|
||||||
|
|
||||||
this._data = await fetchStatistics(
|
this._data = await fetchStatistics(
|
||||||
this.hass,
|
this.hass,
|
||||||
addHours(energyData.start, -1),
|
addHours(energyData.start, -1),
|
||||||
@ -180,9 +197,6 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
];
|
];
|
||||||
|
|
||||||
energyData.prefs.device_consumption.forEach((device, idx) => {
|
energyData.prefs.device_consumption.forEach((device, idx) => {
|
||||||
const entity = this.hass.states[device.stat_consumption];
|
|
||||||
const label = entity ? computeStateName(entity) : device.stat_consumption;
|
|
||||||
|
|
||||||
const value =
|
const value =
|
||||||
device.stat_consumption in this._data!
|
device.stat_consumption in this._data!
|
||||||
? calculateStatisticSumGrowth(this._data![device.stat_consumption]) ||
|
? calculateStatisticSumGrowth(this._data![device.stat_consumption]) ||
|
||||||
@ -191,9 +205,8 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
|
|
||||||
data.push({
|
data.push({
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
y: label,
|
y: device.stat_consumption,
|
||||||
x: value,
|
x: value,
|
||||||
entity_id: device.stat_consumption,
|
|
||||||
idx,
|
idx,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user