Switch logbook calls to use the new websocket (#12665)

This commit is contained in:
J. Nick Koston 2022-05-11 22:28:18 -05:00 committed by GitHub
parent c9c3be71cc
commit 4c982b3323
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 50 deletions

View File

@ -298,11 +298,11 @@ export const basicTrace: DemoTrace = {
source: "state of input_boolean.toggle_1",
entity_id: "automation.toggle_toggles",
context_id: "6cfcae368e7b3686fad6c59e83ae76c9",
when: "2021-03-25T04:36:51.240832+00:00",
when: 1616647011.240832,
domain: "automation",
},
{
when: "2021-03-25T04:36:51.249828+00:00",
when: 1616647011.249828,
name: "Toggle 4",
state: "on",
entity_id: "input_boolean.toggle_4",
@ -313,7 +313,7 @@ export const basicTrace: DemoTrace = {
context_name: "Ensure Party mode",
},
{
when: "2021-03-25T04:36:51.258947+00:00",
when: 1616647011.258947,
name: "Toggle 2",
state: "on",
entity_id: "input_boolean.toggle_2",
@ -324,7 +324,7 @@ export const basicTrace: DemoTrace = {
context_name: "Ensure Party mode",
},
{
when: "2021-03-25T04:36:51.261806+00:00",
when: 1616647011.261806,
name: "Toggle 3",
state: "off",
entity_id: "input_boolean.toggle_3",
@ -335,7 +335,7 @@ export const basicTrace: DemoTrace = {
context_name: "Ensure Party mode",
},
{
when: "2021-03-25T04:36:51.265246+00:00",
when: 1616647011.265246,
name: "Toggle 4",
state: "off",
entity_id: "input_boolean.toggle_4",

View File

@ -185,11 +185,11 @@ export const motionLightTrace: DemoTrace = {
"has been triggered by state of binary_sensor.pauluss_macbook_pro_camera_in_use",
source: "state of binary_sensor.pauluss_macbook_pro_camera_in_use",
entity_id: "automation.auto_elgato",
when: "2021-03-14T06:07:01.768492+00:00",
when: 1615702021.768492,
domain: "automation",
},
{
when: "2021-03-14T06:07:01.872187+00:00",
when: 1615702021.872187,
name: "Elgato Key Light Air",
state: "on",
entity_id: "light.elgato_key_light_air",
@ -200,7 +200,7 @@ export const motionLightTrace: DemoTrace = {
context_name: "Auto Elgato",
},
{
when: "2021-03-14T06:07:53.284505+00:00",
when: 1615702073.284505,
name: "Elgato Key Light Air",
state: "off",
entity_id: "light.elgato_key_light_air",

View File

@ -10,7 +10,7 @@ const LOGBOOK_LOCALIZE_PATH = "ui.components.logbook.messages";
export const CONTINUOUS_DOMAINS = ["proximity", "sensor"];
export interface LogbookEntry {
when: string;
when: number;
name: string;
message?: string;
entity_id?: string;
@ -46,7 +46,6 @@ export const getLogbookDataForContext = async (
startDate,
undefined,
undefined,
undefined,
contextId
)
);
@ -56,20 +55,13 @@ export const getLogbookData = async (
hass: HomeAssistant,
startDate: string,
endDate: string,
entityId?: string,
entity_matches_only?: boolean
entityId?: string
): Promise<LogbookEntry[]> => {
const localize = await hass.loadBackendTranslation("device_class");
return addLogbookMessage(
hass,
localize,
await getLogbookDataCache(
hass,
startDate,
endDate,
entityId,
entity_matches_only
)
await getLogbookDataCache(hass, startDate, endDate, entityId)
);
};
@ -97,8 +89,7 @@ export const getLogbookDataCache = async (
hass: HomeAssistant,
startDate: string,
endDate: string,
entityId?: string,
entity_matches_only?: boolean
entityId?: string
) => {
const ALL_ENTITIES = "*";
@ -125,39 +116,31 @@ export const getLogbookDataCache = async (
hass,
startDate,
endDate,
entityId !== ALL_ENTITIES ? entityId : undefined,
entity_matches_only
entityId !== ALL_ENTITIES ? entityId : undefined
).then((entries) => entries.reverse());
return DATA_CACHE[cacheKey][entityId];
};
const getLogbookDataFromServer = async (
export const getLogbookDataFromServer = (
hass: HomeAssistant,
startDate: string,
endDate?: string,
entityId?: string,
entitymatchesOnly?: boolean,
contextId?: string
) => {
const params = new URLSearchParams();
let params: any = {
type: "logbook/get_events",
start_time: startDate,
};
if (endDate) {
params.append("end_time", endDate);
params = { ...params, end_time: endDate };
}
if (entityId) {
params.append("entity", entityId);
params = { ...params, entity_ids: entityId.split(",") };
} else if (contextId) {
params = { ...params, context_id: contextId };
}
if (entitymatchesOnly) {
params.append("entity_matches_only", "");
}
if (contextId) {
params.append("context_id", contextId);
}
return hass.callApi<LogbookEntry[]>(
"GET",
`logbook/${startDate}?${params.toString()}`
);
return hass.callWS<LogbookEntry[]>(params);
};
export const clearLogbookCache = (startDate: string, endDate: string) => {

View File

@ -147,8 +147,7 @@ export class MoreInfoLogbook extends LitElement {
this.hass,
lastDate.toISOString(),
now.toISOString(),
this.entityId,
true
this.entityId
),
this.hass.user?.is_admin ? loadTraceContexts(this.hass) : {},
this._fetchUserPromise,

View File

@ -135,11 +135,11 @@ class HaLogbook extends LitElement {
${index === 0 ||
(item?.when &&
previous?.when &&
new Date(item.when).toDateString() !==
new Date(previous.when).toDateString())
new Date(item.when * 1000).toDateString() !==
new Date(previous.when * 1000).toDateString())
? html`
<h4 class="date">
${formatDate(new Date(item.when), this.hass.locale)}
${formatDate(new Date(item.when * 1000), this.hass.locale)}
</h4>
`
: html``}
@ -207,14 +207,14 @@ class HaLogbook extends LitElement {
<div class="secondary">
<span
>${formatTimeWithSeconds(
new Date(item.when),
new Date(item.when * 1000),
this.hass.locale
)}</span
>
-
<ha-relative-time
.hass=${this.hass}
.datetime=${item.when}
.datetime=${item.when * 1000}
capitalize
></ha-relative-time>
${["script", "automation"].includes(item.domain!) &&

View File

@ -249,8 +249,7 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard {
this.hass,
lastDate.toISOString(),
now.toISOString(),
this._configEntities!.map((entity) => entity.entity).toString(),
true
this._configEntities!.map((entity) => entity.entity).toString()
),
this._fetchUserPromise,
]);
@ -264,7 +263,7 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard {
: newEntries;
this._logbookEntries = logbookEntries.filter(
(logEntry) => new Date(logEntry.when) > hoursToShowDate
(logEntry) => new Date(logEntry.when * 1000) > hoursToShowDate
);
this._lastLogbookDate = now;