Use new logbook streaming websocket api for cases where we need end_time (#12753)

This commit is contained in:
J. Nick Koston 2022-05-23 17:40:05 -05:00 committed by GitHub
parent a02b817d7f
commit 067c2fdfa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 45 deletions

View File

@ -165,6 +165,7 @@ export const subscribeLogbook = (
hass: HomeAssistant,
callbackFunction: (message: LogbookEntry[]) => void,
startDate: string,
endDate: string,
entityIds?: string[],
deviceIds?: string[]
): Promise<UnsubscribeFunc> => {
@ -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;

View File

@ -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<UnsubscribeFunc>;
private _subscribed?: Promise<UnsubscribeFunc | void>;
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 <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),
};
}
@ -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) =>