prevent fetching stats twice

This commit is contained in:
Bram Kragten 2022-09-22 16:09:33 +02:00
parent e31d1aace0
commit 95bbf6e0d2
No known key found for this signature in database
GPG Key ID: FBE2DFDB363EF55B
2 changed files with 10 additions and 15 deletions

View File

@ -38,8 +38,6 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
private _names: Record<string, string> = {};
private _fetching = false;
private _interval?: number;
public disconnectedCallback() {
@ -156,15 +154,11 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
}
private async _getStatistics(): Promise<void> {
if (this._fetching) {
return;
}
const startDate = new Date();
startDate.setTime(
startDate.getTime() -
1000 * 60 * 60 * (24 * (this._config!.days_to_show || 30) + 1)
);
this._fetching = true;
try {
this._statistics = await fetchStatistics(
this.hass!,
@ -173,8 +167,8 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
this._entities,
this._config!.period
);
} finally {
this._fetching = false;
} catch (err) {
this._statistics = undefined;
}
}

View File

@ -94,12 +94,6 @@ export class HuiStatisticsGraphCardEditor
this.hass!,
statisticIds || []
);
if (statisticIds?.some((statistic_id) => statistic_id.includes(":"))) {
const { period, ...config } = this._config!;
fireEvent(this, "config-changed", {
config: config,
});
}
};
public willUpdate(changedProps: PropertyValues) {
@ -239,8 +233,15 @@ export class HuiStatisticsGraphCardEditor
}
private _entitiesChanged(ev: CustomEvent): void {
const config = { ...this._config!, entities: ev.detail.value };
if (
config.entities?.some((statistic_id) => statistic_id.includes(":")) &&
config.period === "5minute"
) {
delete config.period;
}
fireEvent(this, "config-changed", {
config: { ...this._config!, entities: ev.detail.value },
config,
});
}