mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-22 00:36:34 +00:00
Use new logbook streaming websocket api for cases where we need end_time (#12753)
This commit is contained in:
parent
a02b817d7f
commit
067c2fdfa8
@ -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;
|
||||
|
@ -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) =>
|
||||
|
Loading…
x
Reference in New Issue
Block a user