diff --git a/src/dialogs/more-info/ha-more-info-logbook.ts b/src/dialogs/more-info/ha-more-info-logbook.ts index 3a307e9370..4f3e89b3a9 100644 --- a/src/dialogs/more-info/ha-more-info-logbook.ts +++ b/src/dialogs/more-info/ha-more-info-logbook.ts @@ -5,6 +5,7 @@ import { computeStateDomain } from "../../common/entity/compute_state_domain"; import { throttle } from "../../common/util/throttle"; import "../../components/ha-circular-progress"; import "../../components/state-history-charts"; +import { fetchUsers } from "../../data/user"; import { getLogbookData, LogbookEntry } from "../../data/logbook"; import { loadTraceContexts, TraceContexts } from "../../data/trace"; import "../../panels/logbook/ha-logbook"; @@ -22,10 +23,12 @@ export class MoreInfoLogbook extends LitElement { @state() private _traceContexts?: TraceContexts; - @state() private _persons = {}; + @state() private _userIdToName = {}; private _lastLogbookDate?: Date; + private _fetchUserPromise?: Promise; + private _throttleGetLogbookEntries = throttle(() => { this._getLogBookData(); }, 10000); @@ -59,7 +62,7 @@ export class MoreInfoLogbook extends LitElement { .hass=${this.hass} .entries=${this._logbookEntries} .traceContexts=${this._traceContexts} - .userIdToName=${this._persons} + .userIdToName=${this._userIdToName} > ` : html`
@@ -70,7 +73,7 @@ export class MoreInfoLogbook extends LitElement { } protected firstUpdated(): void { - this._fetchPersonNames(); + this._fetchUserPromise = this._fetchUserNames(); this.addEventListener("click", (ev) => { if ((ev.composedPath()[0] as HTMLElement).tagName === "A") { setTimeout(() => closeDialog("ha-more-info-dialog"), 500); @@ -125,6 +128,7 @@ export class MoreInfoLogbook extends LitElement { true ), loadTraceContexts(this.hass), + this._fetchUserPromise, ]); this._logbookEntries = this._logbookEntries ? [...newEntries, ...this._logbookEntries] @@ -133,16 +137,34 @@ export class MoreInfoLogbook extends LitElement { this._traceContexts = traceContexts; } - private _fetchPersonNames() { + private async _fetchUserNames() { + const userIdToName = {}; + + // Start loading users + const userProm = this.hass.user?.is_admin && fetchUsers(this.hass); + + // Process persons Object.values(this.hass.states).forEach((entity) => { if ( entity.attributes.user_id && computeStateDomain(entity) === "person" ) { - this._persons[entity.attributes.user_id] = + this._userIdToName[entity.attributes.user_id] = entity.attributes.friendly_name; } }); + + // Process users + if (userProm) { + const users = await userProm; + for (const user of users) { + if (!(user.id in userIdToName)) { + userIdToName[user.id] = user.name; + } + } + } + + this._userIdToName = userIdToName; } static get styles() { diff --git a/src/panels/logbook/ha-panel-logbook.ts b/src/panels/logbook/ha-panel-logbook.ts index 5a6b89ee92..aa5aab7245 100644 --- a/src/panels/logbook/ha-panel-logbook.ts +++ b/src/panels/logbook/ha-panel-logbook.ts @@ -5,6 +5,7 @@ import "@polymer/app-layout/app-toolbar/app-toolbar"; import { css, html, LitElement, PropertyValues } from "lit"; import { customElement, property, state } from "lit/decorators"; import { isComponentLoaded } from "../../common/config/is_component_loaded"; +import { computeStateDomain } from "../../common/entity/compute_state_domain"; import { computeRTL } from "../../common/util/compute_rtl"; import "../../components/entity/ha-entity-picker"; import "../../components/ha-circular-progress"; @@ -16,7 +17,6 @@ import { getLogbookData, LogbookEntry, } from "../../data/logbook"; -import { fetchPersons } from "../../data/person"; import { loadTraceContexts, TraceContexts } from "../../data/trace"; import { fetchUsers } from "../../data/user"; import "../../layouts/ha-app-layout"; @@ -44,7 +44,7 @@ export class HaPanelLogbook extends LitElement { @state() private _ranges?: DateRangePickerRanges; - private _fetchUserDone?: Promise; + private _fetchUserPromise?: Promise; @state() private _userIdToName = {}; @@ -136,7 +136,7 @@ export class HaPanelLogbook extends LitElement { super.firstUpdated(changedProps); this.hass.loadBackendTranslation("title"); - this._fetchUserDone = this._fetchUserNames(); + this._fetchUserPromise = this._fetchUserNames(); const today = new Date(); today.setHours(0, 0, 0, 0); @@ -198,23 +198,19 @@ export class HaPanelLogbook extends LitElement { private async _fetchUserNames() { const userIdToName = {}; - // Start loading all the data - const personProm = fetchPersons(this.hass); - const userProm = this.hass.user!.is_admin && fetchUsers(this.hass); + // Start loading users + const userProm = this.hass.user?.is_admin && fetchUsers(this.hass); // Process persons - const persons = await personProm; - - for (const person of persons.storage) { - if (person.user_id) { - userIdToName[person.user_id] = person.name; + Object.values(this.hass.states).forEach((entity) => { + if ( + entity.attributes.user_id && + computeStateDomain(entity) === "person" + ) { + this._userIdToName[entity.attributes.user_id] = + entity.attributes.friendly_name; } - } - for (const person of persons.config) { - if (person.user_id) { - userIdToName[person.user_id] = person.name; - } - } + }); // Process users if (userProm) { @@ -262,7 +258,7 @@ export class HaPanelLogbook extends LitElement { this._entityId ), isComponentLoaded(this.hass, "trace") ? loadTraceContexts(this.hass) : {}, - this._fetchUserDone, + this._fetchUserPromise, ]); this._entries = entries; diff --git a/src/panels/lovelace/cards/hui-logbook-card.ts b/src/panels/lovelace/cards/hui-logbook-card.ts index 5681ce47f6..a500e99fc1 100644 --- a/src/panels/lovelace/cards/hui-logbook-card.ts +++ b/src/panels/lovelace/cards/hui-logbook-card.ts @@ -9,11 +9,12 @@ import { import { customElement, property, state } from "lit/decorators"; import { classMap } from "lit/directives/class-map"; import { isComponentLoaded } from "../../../common/config/is_component_loaded"; -import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { computeStateDomain } from "../../../common/entity/compute_state_domain"; +import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { throttle } from "../../../common/util/throttle"; import "../../../components/ha-card"; import "../../../components/ha-circular-progress"; +import { fetchUsers } from "../../../data/user"; import { getLogbookData, LogbookEntry } from "../../../data/logbook"; import type { HomeAssistant } from "../../../types"; import "../../logbook/ha-logbook"; @@ -51,18 +52,20 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { }; } - @property({ attribute: false }) public hass?: HomeAssistant; + @property({ attribute: false }) public hass!: HomeAssistant; @state() private _config?: LogbookCardConfig; @state() private _logbookEntries?: LogbookEntry[]; - @state() private _persons = {}; - @state() private _configEntities?: EntityConfig[]; + @state() private _userIdToName = {}; + private _lastLogbookDate?: Date; + private _fetchUserPromise?: Promise; + private _throttleGetLogbookEntries = throttle(() => { this._getLogBookData(); }, 10000); @@ -114,7 +117,7 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { } protected firstUpdated(): void { - this._fetchPersonNames(); + this._fetchUserPromise = this._fetchUserNames(); } protected updated(changedProperties: PropertyValues) { @@ -199,7 +202,7 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { virtualize .hass=${this.hass} .entries=${this._logbookEntries} - .userIdToName=${this._persons} + .userIdToName=${this._userIdToName} > ` : html` @@ -229,13 +232,16 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { const lastDate = this._lastLogbookDate || hoursToShowDate; const now = new Date(); - const newEntries = await getLogbookData( - this.hass, - lastDate.toISOString(), - now.toISOString(), - this._configEntities!.map((entity) => entity.entity).toString(), - true - ); + const [newEntries] = await Promise.all([ + getLogbookData( + this.hass, + lastDate.toISOString(), + now.toISOString(), + this._configEntities!.map((entity) => entity.entity).toString(), + true + ), + this._fetchUserPromise, + ]); const logbookEntries = this._logbookEntries ? [...newEntries, ...this._logbookEntries] @@ -248,20 +254,34 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { this._lastLogbookDate = now; } - private _fetchPersonNames() { - if (!this.hass) { - return; - } + private async _fetchUserNames() { + const userIdToName = {}; + // Start loading users + const userProm = this.hass.user?.is_admin && fetchUsers(this.hass); + + // Process persons Object.values(this.hass!.states).forEach((entity) => { if ( entity.attributes.user_id && computeStateDomain(entity) === "person" ) { - this._persons[entity.attributes.user_id] = + this._userIdToName[entity.attributes.user_id] = entity.attributes.friendly_name; } }); + + // Process users + if (userProm) { + const users = await userProm; + for (const user of users) { + if (!(user.id in userIdToName)) { + userIdToName[user.id] = user.name; + } + } + } + + this._userIdToName = userIdToName; } static get styles(): CSSResultGroup {