From dc8d483e8b9c41221e863d9d4a9e0cd6da54f2e1 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 23 Jan 2025 22:31:30 -0800 Subject: [PATCH] Logbook card loading fix (#23853) * the call to unsubscribe will never do anything * await the subscription when subscribing to logbook * removed unneeded if wrapped call to subscribeLogbook in try/catch remove return values from _subscribeLogbookPeriod --- src/panels/logbook/ha-logbook.ts | 59 +++++++++++++++----------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/src/panels/logbook/ha-logbook.ts b/src/panels/logbook/ha-logbook.ts index 1703e00c64..a22cc4111a 100644 --- a/src/panels/logbook/ha-logbook.ts +++ b/src/panels/logbook/ha-logbook.ts @@ -76,7 +76,7 @@ export class HaLogbook extends LitElement { @state() private _error?: string; - private _subscribed?: Promise<(() => Promise) | undefined>; + private _subscribed?: (() => Promise) | undefined; private _liveUpdatesEnabled = true; @@ -211,12 +211,9 @@ export class HaLogbook extends LitElement { private async _unsubscribe(): Promise { if (this._subscribed) { try { - const unsub = await this._subscribed; - if (unsub) { - await unsub(); - this._subscribed = undefined; - this._pendingStreamMessages = []; - } + await this._subscribed(); + this._subscribed = undefined; + this._pendingStreamMessages = []; } catch (err: any) { // eslint-disable-next-line console.error("Error unsubscribing:", err); @@ -284,34 +281,34 @@ export class HaLogbook extends LitElement { throw new Error("Unexpected time specified"); } - private async _subscribeLogbookPeriod(logbookPeriod: LogbookTimePeriod) { + private async _subscribeLogbookPeriod( + logbookPeriod: LogbookTimePeriod + ): Promise { if (this._subscribed) { - return true; + return; } - // Ensure any previous subscription is cleaned up - await this._unsubscribe(); - this._subscribed = subscribeLogbook( - this.hass, - (streamMessage) => { - // "recent" means start time is a sliding window - // so we need to calculate an expireTime to - // purge old events - if (!this._subscribed) { - // Message came in before we had a chance to unload - return; - } - this._processOrQueueStreamMessage(streamMessage); - }, - logbookPeriod.startTime.toISOString(), - logbookPeriod.endTime.toISOString(), - this.entityIds, - this.deviceIds - ).catch((err) => { + try { + this._subscribed = await subscribeLogbook( + this.hass, + (streamMessage) => { + // "recent" means start time is a sliding window + // so we need to calculate an expireTime to + // purge old events + if (!this._subscribed) { + // Message came in before we had a chance to unload + return; + } + this._processOrQueueStreamMessage(streamMessage); + }, + logbookPeriod.startTime.toISOString(), + logbookPeriod.endTime.toISOString(), + this.entityIds, + this.deviceIds + ); + } catch (err: any) { this._subscribed = undefined; this._error = err; - return undefined; - }); - return true; + } } private async _getLogBookData() {