Fix duplicate fetch of stats metadata in more info (#15590)

This commit is contained in:
J. Nick Koston 2023-02-27 03:27:26 -06:00 committed by GitHub
parent ba3b265b9a
commit 25cf879793
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 6 deletions

View File

@ -332,7 +332,10 @@ class StatisticsChart extends LitElement {
prevEndTime = end; prevEndTime = end;
}; };
const color = getGraphColorByIndex(colorIndex, this._computedStyle!); const color = getGraphColorByIndex(
colorIndex,
this._computedStyle || getComputedStyle(this)
);
colorIndex++; colorIndex++;
const statTypes: this["statTypes"] = []; const statTypes: this["statTypes"] = [];

View File

@ -14,6 +14,7 @@ import {
getStatisticMetadata, getStatisticMetadata,
Statistics, Statistics,
StatisticsTypes, StatisticsTypes,
StatisticsMetaData,
} from "../../data/recorder"; } from "../../data/recorder";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import "../../components/chart/statistics-chart"; import "../../components/chart/statistics-chart";
@ -47,6 +48,8 @@ export class MoreInfoHistory extends LitElement {
private _error?: string; private _error?: string;
private _metadata?: Record<string, StatisticsMetaData>;
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this.entityId) { if (!this.entityId) {
return html``; return html``;
@ -70,6 +73,7 @@ export class MoreInfoHistory extends LitElement {
.hass=${this.hass} .hass=${this.hass}
.isLoadingData=${!this._statistics} .isLoadingData=${!this._statistics}
.statisticsData=${this._statistics} .statisticsData=${this._statistics}
.metadata=${this._metadata}
.statTypes=${statTypes} .statTypes=${statTypes}
.names=${this._statNames} .names=${this._statNames}
hideLegend hideLegend
@ -136,15 +140,33 @@ export class MoreInfoHistory extends LitElement {
this._interval = window.setInterval(() => this._redrawGraph(), 1000 * 60); this._interval = window.setInterval(() => this._redrawGraph(), 1000 * 60);
} }
private async _getStatisticsMetaData(statisticIds: string[] | undefined) {
const statsMetadataArray = await getStatisticMetadata(
this.hass,
statisticIds
);
const statisticsMetaData = {};
statsMetadataArray.forEach((x) => {
statisticsMetaData[x.statistic_id] = x;
});
return statisticsMetaData;
}
private async _getStateHistory(): Promise<void> { private async _getStateHistory(): Promise<void> {
if ( if (
isComponentLoaded(this.hass, "recorder") && isComponentLoaded(this.hass, "recorder") &&
computeDomain(this.entityId) === "sensor" computeDomain(this.entityId) === "sensor"
) { ) {
const metadata = await getStatisticMetadata(this.hass, [this.entityId]); const stateObj = this.hass.states[this.entityId];
this._statNames = { [this.entityId]: "" }; // If there is no state class, the integration providing the entity
if (metadata.length) { // has not opted into statistics so there is no need to check as it
this._statistics = await fetchStatistics( // requires another round-trip to the server.
if (stateObj && stateObj.attributes.state_class) {
// Fire off the metadata and fetch at the same time
// to avoid waiting in sequence so the UI responds
// faster.
const _metadata = this._getStatisticsMetaData([this.entityId]);
const _statistics = fetchStatistics(
this.hass!, this.hass!,
subHours(new Date(), 24), subHours(new Date(), 24),
undefined, undefined,
@ -153,7 +175,16 @@ export class MoreInfoHistory extends LitElement {
undefined, undefined,
statTypes statTypes
); );
return; const [metadata, statistics] = await Promise.all([
_metadata,
_statistics,
]);
if (metadata && Object.keys(metadata).length) {
this._metadata = metadata;
this._statistics = statistics;
this._statNames = { [this.entityId]: "" };
return;
}
} }
} }
if (!isComponentLoaded(this.hass, "history") || this._subscribed) { if (!isComponentLoaded(this.hass, "history") || this._subscribed) {