Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/data/logbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export const subscribeLogbook = (
hass: HomeAssistant,
callbackFunction: (message: LogbookEntry[]) => void,
startDate: string,
endDate: string,
entityIds?: string[],
deviceIds?: string[]
): Promise<UnsubscribeFunc> => {
Expand All @@ -187,6 +188,7 @@ export const subscribeLogbook = (
const params: any = {
type: "logbook/event_stream",
start_time: startDate,
end_time: endDate,
};
if (entityIds?.length) {
params.entity_ids = entityIds;
Expand Down
55 changes: 10 additions & 45 deletions src/panels/logbook/ha-logbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { throttle } from "../../common/util/throttle";
import "../../components/ha-circular-progress";
import {
clearLogbookCache,
getLogbookData,
LogbookEntry,
subscribeLogbook,
} from "../../data/logbook";
Expand Down Expand Up @@ -79,9 +78,7 @@ export class HaLogbook extends LitElement {

@state() private _error?: string;

private _renderId = 1;

private _subscribed?: Promise<UnsubscribeFunc>;
private _subscribed?: Promise<UnsubscribeFunc | void>;

private _throttleGetLogbookEntries = throttle(
() => this._getLogBookData(),
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -236,17 +233,15 @@ export class HaLogbook extends LitElement {
return <LogbookTimePeriod>{
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),
};
}
throw new Error("Unexpected time specified");
}

private _subscribeLogbookPeriod(logbookPeriod: LogbookTimePeriod) {
if (logbookPeriod.endTime < logbookPeriod.now) {
return false;
}
if (this._subscribed) {
return true;
}
Expand All @@ -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) {
Expand All @@ -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) =>
Expand Down