mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-16 13:56:35 +00:00
Use name from statistics metadata in energy dashboard (#13469)
This commit is contained in:
parent
d64c81a123
commit
6446534e0b
@ -569,12 +569,12 @@ export const adjustStatisticsSum = (
|
|||||||
export const getStatisticLabel = (
|
export const getStatisticLabel = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
statisticsId: string,
|
statisticsId: string,
|
||||||
statisticsMetaData: Record<string, StatisticsMetaData>
|
statisticsMetaData: Record<string, 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];
|
const statisticMetaData = statisticsMetaData?.[statisticsId];
|
||||||
return statisticMetaData?.name || statisticsId;
|
return statisticMetaData?.name || statisticsId;
|
||||||
};
|
};
|
||||||
|
@ -14,7 +14,6 @@ import { classMap } from "lit/directives/class-map";
|
|||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { getColorByIndex } from "../../../../common/color/colors";
|
import { getColorByIndex } from "../../../../common/color/colors";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { computeStateName } from "../../../../common/entity/compute_state_name";
|
|
||||||
import {
|
import {
|
||||||
formatNumber,
|
formatNumber,
|
||||||
numberFormatToLocale,
|
numberFormatToLocale,
|
||||||
@ -26,6 +25,7 @@ import { EnergyData, getEnergyDataCollection } from "../../../../data/energy";
|
|||||||
import {
|
import {
|
||||||
calculateStatisticSumGrowth,
|
calculateStatisticSumGrowth,
|
||||||
fetchStatistics,
|
fetchStatistics,
|
||||||
|
getStatisticLabel,
|
||||||
Statistics,
|
Statistics,
|
||||||
} from "../../../../data/history";
|
} from "../../../../data/history";
|
||||||
import { FrontendLocaleData } from "../../../../data/translation";
|
import { FrontendLocaleData } from "../../../../data/translation";
|
||||||
@ -45,6 +45,8 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
|
|
||||||
@state() private _chartData: ChartData = { datasets: [] };
|
@state() private _chartData: ChartData = { datasets: [] };
|
||||||
|
|
||||||
|
@state() private _data?: EnergyData;
|
||||||
|
|
||||||
@query("ha-chart-base") private _chart?: HaChartBase;
|
@query("ha-chart-base") private _chart?: HaChartBase;
|
||||||
|
|
||||||
protected hassSubscribeRequiredHostProps = ["_config"];
|
protected hassSubscribeRequiredHostProps = ["_config"];
|
||||||
@ -53,7 +55,10 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
return [
|
return [
|
||||||
getEnergyDataCollection(this.hass, {
|
getEnergyDataCollection(this.hass, {
|
||||||
key: this._config?.collection_key,
|
key: this._config?.collection_key,
|
||||||
}).subscribe((data) => this._getStatistics(data)),
|
}).subscribe((data) => {
|
||||||
|
this._data = data;
|
||||||
|
this._getStatistics(data);
|
||||||
|
}),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,11 +110,14 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
ticks: {
|
ticks: {
|
||||||
autoSkip: false,
|
autoSkip: false,
|
||||||
callback: (index) => {
|
callback: (index) => {
|
||||||
const entityId = (
|
const statisticId = (
|
||||||
this._chartData.datasets[0].data[index] as ScatterDataPoint
|
this._chartData.datasets[0].data[index] as ScatterDataPoint
|
||||||
).y;
|
).y;
|
||||||
const entity = this.hass.states[entityId];
|
return getStatisticLabel(
|
||||||
return entity ? computeStateName(entity) : entityId;
|
this.hass,
|
||||||
|
statisticId as any,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -126,8 +134,12 @@ export class HuiEnergyDevicesGraphCard
|
|||||||
mode: "nearest",
|
mode: "nearest",
|
||||||
callbacks: {
|
callbacks: {
|
||||||
title: (item) => {
|
title: (item) => {
|
||||||
const entity = this.hass.states[item[0].label];
|
const statisticId = item[0].label;
|
||||||
return entity ? computeStateName(entity) : item[0].label;
|
return getStatisticLabel(
|
||||||
|
this.hass,
|
||||||
|
statisticId,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
);
|
||||||
},
|
},
|
||||||
label: (context) =>
|
label: (context) =>
|
||||||
`${context.dataset.label}: ${formatNumber(
|
`${context.dataset.label}: ${formatNumber(
|
||||||
|
@ -26,7 +26,6 @@ import {
|
|||||||
import { labBrighten, labDarken } from "../../../../common/color/lab";
|
import { labBrighten, labDarken } from "../../../../common/color/lab";
|
||||||
import { formatDateShort } from "../../../../common/datetime/format_date";
|
import { formatDateShort } from "../../../../common/datetime/format_date";
|
||||||
import { formatTime } from "../../../../common/datetime/format_time";
|
import { formatTime } from "../../../../common/datetime/format_time";
|
||||||
import { computeStateName } from "../../../../common/entity/compute_state_name";
|
|
||||||
import {
|
import {
|
||||||
formatNumber,
|
formatNumber,
|
||||||
numberFormatToLocale,
|
numberFormatToLocale,
|
||||||
@ -327,6 +326,7 @@ export class HuiEnergySolarGraphCard
|
|||||||
if (forecasts) {
|
if (forecasts) {
|
||||||
datasets.push(
|
datasets.push(
|
||||||
...this._processForecast(
|
...this._processForecast(
|
||||||
|
energyData.statsMetadata,
|
||||||
forecasts,
|
forecasts,
|
||||||
solarSources,
|
solarSources,
|
||||||
computedStyles.getPropertyValue("--primary-text-color"),
|
computedStyles.getPropertyValue("--primary-text-color"),
|
||||||
@ -422,6 +422,7 @@ export class HuiEnergySolarGraphCard
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _processForecast(
|
private _processForecast(
|
||||||
|
statisticsMetaData: Record<string, StatisticsMetaData>,
|
||||||
forecasts: EnergySolarForecasts,
|
forecasts: EnergySolarForecasts,
|
||||||
solarSources: SolarSourceTypeEnergyPreference[],
|
solarSources: SolarSourceTypeEnergyPreference[],
|
||||||
borderColor: string,
|
borderColor: string,
|
||||||
@ -435,8 +436,6 @@ export class HuiEnergySolarGraphCard
|
|||||||
// Process solar forecast data.
|
// Process solar forecast data.
|
||||||
solarSources.forEach((source) => {
|
solarSources.forEach((source) => {
|
||||||
if (source.config_entry_solar_forecast) {
|
if (source.config_entry_solar_forecast) {
|
||||||
const entity = this.hass.states[source.stat_energy_from];
|
|
||||||
|
|
||||||
const forecastsData: Record<string, number> | undefined = {};
|
const forecastsData: Record<string, number> | undefined = {};
|
||||||
source.config_entry_solar_forecast.forEach((configEntryId) => {
|
source.config_entry_solar_forecast.forEach((configEntryId) => {
|
||||||
if (!forecasts![configEntryId]) {
|
if (!forecasts![configEntryId]) {
|
||||||
@ -481,9 +480,11 @@ export class HuiEnergySolarGraphCard
|
|||||||
label: this.hass.localize(
|
label: this.hass.localize(
|
||||||
"ui.panel.lovelace.cards.energy.energy_solar_graph.forecast",
|
"ui.panel.lovelace.cards.energy.energy_solar_graph.forecast",
|
||||||
{
|
{
|
||||||
name: entity
|
name: getStatisticLabel(
|
||||||
? computeStateName(entity)
|
this.hass,
|
||||||
: source.stat_energy_from,
|
source.stat_energy_from,
|
||||||
|
statisticsMetaData
|
||||||
|
),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
fill: false,
|
fill: false,
|
||||||
|
@ -18,7 +18,6 @@ import {
|
|||||||
hex2rgb,
|
hex2rgb,
|
||||||
} from "../../../../common/color/convert-color";
|
} from "../../../../common/color/convert-color";
|
||||||
import { labBrighten, labDarken } from "../../../../common/color/lab";
|
import { labBrighten, labDarken } from "../../../../common/color/lab";
|
||||||
import { computeStateName } from "../../../../common/entity/compute_state_name";
|
|
||||||
import { formatNumber } from "../../../../common/number/format_number";
|
import { formatNumber } from "../../../../common/number/format_number";
|
||||||
import "../../../../components/chart/statistics-chart";
|
import "../../../../components/chart/statistics-chart";
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
@ -28,7 +27,10 @@ import {
|
|||||||
getEnergyDataCollection,
|
getEnergyDataCollection,
|
||||||
getEnergyGasUnit,
|
getEnergyGasUnit,
|
||||||
} from "../../../../data/energy";
|
} from "../../../../data/energy";
|
||||||
import { calculateStatisticSumGrowth } from "../../../../data/history";
|
import {
|
||||||
|
calculateStatisticSumGrowth,
|
||||||
|
getStatisticLabel,
|
||||||
|
} from "../../../../data/history";
|
||||||
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
|
||||||
import { HomeAssistant } from "../../../../types";
|
import { HomeAssistant } from "../../../../types";
|
||||||
import { LovelaceCard } from "../../types";
|
import { LovelaceCard } from "../../types";
|
||||||
@ -199,7 +201,6 @@ export class HuiEnergySourcesTableCard
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody class="mdc-data-table__content">
|
<tbody class="mdc-data-table__content">
|
||||||
${types.solar?.map((source, idx) => {
|
${types.solar?.map((source, idx) => {
|
||||||
const entity = this.hass.states[source.stat_energy_from];
|
|
||||||
const energy =
|
const energy =
|
||||||
calculateStatisticSumGrowth(
|
calculateStatisticSumGrowth(
|
||||||
this._data!.stats[source.stat_energy_from]
|
this._data!.stats[source.stat_energy_from]
|
||||||
@ -235,9 +236,11 @@ export class HuiEnergySourcesTableCard
|
|||||||
></div>
|
></div>
|
||||||
</td>
|
</td>
|
||||||
<th class="mdc-data-table__cell" scope="row">
|
<th class="mdc-data-table__cell" scope="row">
|
||||||
${entity
|
${getStatisticLabel(
|
||||||
? computeStateName(entity)
|
this.hass,
|
||||||
: source.stat_energy_from}
|
source.stat_energy_from,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
)}
|
||||||
</th>
|
</th>
|
||||||
${compare
|
${compare
|
||||||
? html`<td
|
? html`<td
|
||||||
@ -287,8 +290,6 @@ export class HuiEnergySourcesTableCard
|
|||||||
</tr>`
|
</tr>`
|
||||||
: ""}
|
: ""}
|
||||||
${types.battery?.map((source, idx) => {
|
${types.battery?.map((source, idx) => {
|
||||||
const entityFrom = this.hass.states[source.stat_energy_from];
|
|
||||||
const entityTo = this.hass.states[source.stat_energy_to];
|
|
||||||
const energyFrom =
|
const energyFrom =
|
||||||
calculateStatisticSumGrowth(
|
calculateStatisticSumGrowth(
|
||||||
this._data!.stats[source.stat_energy_from]
|
this._data!.stats[source.stat_energy_from]
|
||||||
@ -343,9 +344,11 @@ export class HuiEnergySourcesTableCard
|
|||||||
></div>
|
></div>
|
||||||
</td>
|
</td>
|
||||||
<th class="mdc-data-table__cell" scope="row">
|
<th class="mdc-data-table__cell" scope="row">
|
||||||
${entityFrom
|
${getStatisticLabel(
|
||||||
? computeStateName(entityFrom)
|
this.hass,
|
||||||
: source.stat_energy_from}
|
source.stat_energy_from,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
)}
|
||||||
</th>
|
</th>
|
||||||
${compare
|
${compare
|
||||||
? html`<td
|
? html`<td
|
||||||
@ -378,9 +381,11 @@ export class HuiEnergySourcesTableCard
|
|||||||
></div>
|
></div>
|
||||||
</td>
|
</td>
|
||||||
<th class="mdc-data-table__cell" scope="row">
|
<th class="mdc-data-table__cell" scope="row">
|
||||||
${entityTo
|
${getStatisticLabel(
|
||||||
? computeStateName(entityTo)
|
this.hass,
|
||||||
: source.stat_energy_from}
|
source.stat_energy_to,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
)}
|
||||||
</th>
|
</th>
|
||||||
${compare
|
${compare
|
||||||
? html`<td
|
? html`<td
|
||||||
@ -440,7 +445,6 @@ export class HuiEnergySourcesTableCard
|
|||||||
: ""}
|
: ""}
|
||||||
${types.grid?.map(
|
${types.grid?.map(
|
||||||
(source) => html`${source.flow_from.map((flow, idx) => {
|
(source) => html`${source.flow_from.map((flow, idx) => {
|
||||||
const entity = this.hass.states[flow.stat_energy_from];
|
|
||||||
const energy =
|
const energy =
|
||||||
calculateStatisticSumGrowth(
|
calculateStatisticSumGrowth(
|
||||||
this._data!.stats[flow.stat_energy_from]
|
this._data!.stats[flow.stat_energy_from]
|
||||||
@ -498,9 +502,11 @@ export class HuiEnergySourcesTableCard
|
|||||||
></div>
|
></div>
|
||||||
</td>
|
</td>
|
||||||
<th class="mdc-data-table__cell" scope="row">
|
<th class="mdc-data-table__cell" scope="row">
|
||||||
${entity
|
${getStatisticLabel(
|
||||||
? computeStateName(entity)
|
this.hass,
|
||||||
: flow.stat_energy_from}
|
flow.stat_energy_from,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
)}
|
||||||
</th>
|
</th>
|
||||||
${compare
|
${compare
|
||||||
? html`<td
|
? html`<td
|
||||||
@ -545,7 +551,6 @@ export class HuiEnergySourcesTableCard
|
|||||||
</tr>`;
|
</tr>`;
|
||||||
})}
|
})}
|
||||||
${source.flow_to.map((flow, idx) => {
|
${source.flow_to.map((flow, idx) => {
|
||||||
const entity = this.hass.states[flow.stat_energy_to];
|
|
||||||
const energy =
|
const energy =
|
||||||
(calculateStatisticSumGrowth(
|
(calculateStatisticSumGrowth(
|
||||||
this._data!.stats[flow.stat_energy_to]
|
this._data!.stats[flow.stat_energy_to]
|
||||||
@ -602,7 +607,11 @@ export class HuiEnergySourcesTableCard
|
|||||||
></div>
|
></div>
|
||||||
</td>
|
</td>
|
||||||
<th class="mdc-data-table__cell" scope="row">
|
<th class="mdc-data-table__cell" scope="row">
|
||||||
${entity ? computeStateName(entity) : flow.stat_energy_to}
|
${getStatisticLabel(
|
||||||
|
this.hass,
|
||||||
|
flow.stat_energy_to,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
)}
|
||||||
</th>
|
</th>
|
||||||
${compare
|
${compare
|
||||||
? html`<td
|
? html`<td
|
||||||
@ -695,7 +704,6 @@ export class HuiEnergySourcesTableCard
|
|||||||
</tr>`
|
</tr>`
|
||||||
: ""}
|
: ""}
|
||||||
${types.gas?.map((source, idx) => {
|
${types.gas?.map((source, idx) => {
|
||||||
const entity = this.hass.states[source.stat_energy_from];
|
|
||||||
const energy =
|
const energy =
|
||||||
calculateStatisticSumGrowth(
|
calculateStatisticSumGrowth(
|
||||||
this._data!.stats[source.stat_energy_from]
|
this._data!.stats[source.stat_energy_from]
|
||||||
@ -752,9 +760,11 @@ export class HuiEnergySourcesTableCard
|
|||||||
></div>
|
></div>
|
||||||
</td>
|
</td>
|
||||||
<th class="mdc-data-table__cell" scope="row">
|
<th class="mdc-data-table__cell" scope="row">
|
||||||
${entity
|
${getStatisticLabel(
|
||||||
? computeStateName(entity)
|
this.hass,
|
||||||
: source.stat_energy_from}
|
source.stat_energy_from,
|
||||||
|
this._data?.statsMetadata
|
||||||
|
)}
|
||||||
</th>
|
</th>
|
||||||
${compare
|
${compare
|
||||||
? html` <td
|
? html` <td
|
||||||
|
Loading…
x
Reference in New Issue
Block a user