Fix cleanup of logbook when switching entities (#17575)

This commit is contained in:
Bram Kragten 2023-08-14 18:10:22 +02:00 committed by GitHub
parent 255137992b
commit c98cdb91e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 47 deletions

View File

@ -62,12 +62,6 @@ const triggerPhrases = {
"Home Assistant starting": "triggered_by_homeassistant_starting", // start event "Home Assistant starting": "triggered_by_homeassistant_starting", // start event
}; };
const DATA_CACHE: {
[cacheKey: string]: {
[entityId: string]: Promise<LogbookEntry[]> | undefined;
};
} = {};
export const getLogbookDataForContext = async ( export const getLogbookDataForContext = async (
hass: HomeAssistant, hass: HomeAssistant,
startDate: string, startDate: string,
@ -144,10 +138,6 @@ export const subscribeLogbook = (
); );
}; };
export const clearLogbookCache = (startDate: string, endDate: string) => {
DATA_CACHE[`${startDate}${endDate}`] = {};
};
export const createHistoricState = ( export const createHistoricState = (
currentStateObj: HassEntity, currentStateObj: HassEntity,
state?: string state?: string

View File

@ -90,8 +90,8 @@ export class MoreInfoHistory extends LitElement {
: ""}`; : ""}`;
} }
protected updated(changedProps: PropertyValues): void { protected willUpdate(changedProps: PropertyValues): void {
super.updated(changedProps); super.willUpdate(changedProps);
if (changedProps.has("entityId")) { if (changedProps.has("entityId")) {
this._stateHistory = undefined; this._stateHistory = undefined;

View File

@ -1,12 +1,10 @@
import { css, html, LitElement, PropertyValues, nothing } from "lit"; import { css, html, LitElement, PropertyValues, nothing } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { ensureArray } from "../../common/array/ensure-array";
import { isComponentLoaded } from "../../common/config/is_component_loaded"; import { isComponentLoaded } from "../../common/config/is_component_loaded";
import { computeStateDomain } from "../../common/entity/compute_state_domain"; import { computeStateDomain } from "../../common/entity/compute_state_domain";
import { throttle } from "../../common/util/throttle"; import { throttle } from "../../common/util/throttle";
import "../../components/ha-circular-progress"; import "../../components/ha-circular-progress";
import { import {
clearLogbookCache,
LogbookEntry, LogbookEntry,
LogbookStreamMessage, LogbookStreamMessage,
subscribeLogbook, subscribeLogbook,
@ -34,7 +32,8 @@ const idsChanged = (oldIds?: string[], newIds?: string[]) => {
!oldIds || !oldIds ||
!newIds || !newIds ||
oldIds.length !== newIds.length || oldIds.length !== newIds.length ||
!oldIds.every((val) => newIds.includes(val)) oldIds.some((val) => !newIds.includes(val)) ||
newIds.some((val) => !oldIds.includes(val))
); );
}; };
@ -144,23 +143,16 @@ export class HaLogbook extends LitElement {
return; return;
} }
this._unsubscribeSetLoading();
this._throttleGetLogbookEntries.cancel(); this._throttleGetLogbookEntries.cancel();
this._updateTraceContexts.cancel(); this._updateTraceContexts.cancel();
this._updateUsers.cancel(); this._updateUsers.cancel();
await this._unsubscribeSetLoading();
if ("range" in this.time) { if (force) {
clearLogbookCache( this._getLogBookData();
this.time.range[0].toISOString(), } else {
this.time.range[1].toISOString() this._throttleGetLogbookEntries();
);
} }
this._throttleGetLogbookEntries();
}
protected firstUpdated(changedProps: PropertyValues) {
super.firstUpdated(changedProps);
} }
protected shouldUpdate(changedProps: PropertyValues): boolean { protected shouldUpdate(changedProps: PropertyValues): boolean {
@ -172,7 +164,7 @@ export class HaLogbook extends LitElement {
return !oldHass || oldHass.localize !== this.hass.localize; return !oldHass || oldHass.localize !== this.hass.localize;
} }
protected updated(changedProps: PropertyValues): void { protected willUpdate(changedProps: PropertyValues): void {
let changed = changedProps.has("time"); let changed = changedProps.has("time");
for (const key of ["entityIds", "deviceIds"]) { for (const key of ["entityIds", "deviceIds"]) {
@ -210,28 +202,29 @@ export class HaLogbook extends LitElement {
} }
private get _filterAlwaysEmptyResults(): boolean { private get _filterAlwaysEmptyResults(): boolean {
const entityIds = ensureArray(this.entityIds); const entityIds = this.entityIds;
const deviceIds = ensureArray(this.deviceIds); const deviceIds = this.deviceIds;
// If all specified filters are empty lists, we can return an empty list. // If all specified filters are empty lists, we can return an empty list.
return ( return (
(entityIds || deviceIds) && Boolean(entityIds || deviceIds) &&
(!entityIds || entityIds.length === 0) && (!entityIds || entityIds.length === 0) &&
(!deviceIds || deviceIds.length === 0) (!deviceIds || deviceIds.length === 0)
); );
} }
private _unsubscribe(): void { private async _unsubscribe(): Promise<void> {
if (this._subscribed) { if (this._subscribed) {
this._subscribed.then((unsub) => const unsub = await this._subscribed;
unsub if (unsub) {
? unsub().catch(() => { try {
// The backend will cancel the subscription if await unsub();
// we subscribe to entities that will all be } catch (e) {
// filtered away // The backend will cancel the subscription if
}) // we subscribe to entities that will all be
: undefined // filtered away
); }
}
this._subscribed = undefined; this._subscribed = undefined;
} }
} }
@ -253,18 +246,20 @@ export class HaLogbook extends LitElement {
* Setting this._logbookEntries to undefined * Setting this._logbookEntries to undefined
* will put the page in a loading state. * will put the page in a loading state.
*/ */
private _unsubscribeSetLoading() { private async _unsubscribeSetLoading() {
await this._unsubscribe();
this._logbookEntries = undefined; this._logbookEntries = undefined;
this._unsubscribe(); this._pendingStreamMessages = [];
} }
/** Unsubscribe because there are no results. /** Unsubscribe because there are no results.
* Setting this._logbookEntries to an empty * Setting this._logbookEntries to an empty
* list will show a no results message. * list will show a no results message.
*/ */
private _unsubscribeNoResults() { private async _unsubscribeNoResults() {
await this._unsubscribe();
this._logbookEntries = []; this._logbookEntries = [];
this._unsubscribe(); this._pendingStreamMessages = [];
} }
private _calculateLogbookPeriod() { private _calculateLogbookPeriod() {
@ -311,8 +306,8 @@ export class HaLogbook extends LitElement {
}, },
logbookPeriod.startTime.toISOString(), logbookPeriod.startTime.toISOString(),
logbookPeriod.endTime.toISOString(), logbookPeriod.endTime.toISOString(),
ensureArray(this.entityIds), this.entityIds,
ensureArray(this.deviceIds) this.deviceIds
).catch((err) => { ).catch((err) => {
this._subscribed = undefined; this._subscribed = undefined;
this._error = err; this._error = err;