diff --git a/src/data/logbook.ts b/src/data/logbook.ts index d6b8ed08c2..11cf4cfc8d 100644 --- a/src/data/logbook.ts +++ b/src/data/logbook.ts @@ -165,6 +165,7 @@ export const subscribeLogbook = ( hass: HomeAssistant, callbackFunction: (message: LogbookEntry[]) => void, startDate: string, + endDate: string, entityIds?: string[], deviceIds?: string[] ): Promise => { @@ -179,6 +180,7 @@ export const subscribeLogbook = ( const params: any = { type: "logbook/event_stream", start_time: startDate, + end_time: endDate, }; if (entityIds?.length) { params.entity_ids = entityIds; diff --git a/src/panels/logbook/ha-logbook.ts b/src/panels/logbook/ha-logbook.ts index ede038d3c8..287d73b800 100644 --- a/src/panels/logbook/ha-logbook.ts +++ b/src/panels/logbook/ha-logbook.ts @@ -8,7 +8,6 @@ import { throttle } from "../../common/util/throttle"; import "../../components/ha-circular-progress"; import { clearLogbookCache, - getLogbookData, LogbookEntry, subscribeLogbook, } from "../../data/logbook"; @@ -79,9 +78,7 @@ export class HaLogbook extends LitElement { @state() private _error?: string; - private _renderId = 1; - - private _subscribed?: Promise; + private _subscribed?: Promise; private _throttleGetLogbookEntries = throttle( () => this._getLogBookData(), @@ -196,7 +193,7 @@ export class HaLogbook extends LitElement { private _unsubscribe(): void { if (this._subscribed) { - this._subscribed.then((unsub) => unsub()); + this._subscribed.then((unsub) => (unsub ? unsub() : undefined)); this._subscribed = undefined; } } @@ -236,7 +233,8 @@ export class HaLogbook extends LitElement { return { now: now, startTime: new Date(purgeBeforePythonTime * 1000), - endTime: now, + // end streaming one year from now + endTime: new Date(now.getTime() + 86400 * 365 * 1000), purgeBeforePythonTime: findStartOfRecentTime(now, this.time.recent), }; } @@ -244,9 +242,6 @@ export class HaLogbook extends LitElement { } private _subscribeLogbookPeriod(logbookPeriod: LogbookTimePeriod) { - if (logbookPeriod.endTime < logbookPeriod.now) { - return false; - } if (this._subscribed) { return true; } @@ -265,15 +260,17 @@ export class HaLogbook extends LitElement { } }, logbookPeriod.startTime.toISOString(), + logbookPeriod.endTime.toISOString(), ensureArray(this.entityIds), ensureArray(this.deviceIds) - ); + ).catch((err) => { + this._error = err.message; + this._subscribed = undefined; + }); return true; } private async _getLogBookData() { - this._renderId += 1; - const renderId = this._renderId; this._error = undefined; if (this._filterAlwaysEmptyResults) { @@ -294,39 +291,7 @@ export class HaLogbook extends LitElement { this._updateTraceContexts(); } - if (this._subscribeLogbookPeriod(logbookPeriod)) { - // We can go live - return; - } - - // We are only fetching in the past - // with a time window that does not - // extend into the future - this._unsubscribe(); - - let newEntries: LogbookEntry[]; - - try { - newEntries = await getLogbookData( - this.hass, - logbookPeriod.startTime.toISOString(), - logbookPeriod.endTime.toISOString(), - ensureArray(this.entityIds), - ensureArray(this.deviceIds) - ); - } catch (err: any) { - if (renderId === this._renderId) { - this._error = err.message; - } - return; - } - - // New render happening. - if (renderId !== this._renderId) { - return; - } - - this._logbookEntries = [...newEntries].reverse(); + this._subscribeLogbookPeriod(logbookPeriod); } private _nonExpiredRecords = (purgeBeforePythonTime: number | undefined) =>