Compute the icon based on the logbook state and not the current state (#12725)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
J. Nick Koston 2022-05-19 23:12:17 -05:00 committed by GitHub
parent bfeb90780f
commit a0a7ce014f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ import {
PropertyValues, PropertyValues,
TemplateResult, TemplateResult,
} from "lit"; } from "lit";
import type { HassEntity } from "home-assistant-js-websocket";
import { customElement, eventOptions, property } from "lit/decorators"; import { customElement, eventOptions, property } from "lit/decorators";
import { classMap } from "lit/directives/class-map"; import { classMap } from "lit/directives/class-map";
import { DOMAINS_WITH_DYNAMIC_PICTURE } from "../../common/const"; import { DOMAINS_WITH_DYNAMIC_PICTURE } from "../../common/const";
@ -15,7 +16,6 @@ import { formatTimeWithSeconds } from "../../common/datetime/format_time";
import { restoreScroll } from "../../common/decorators/restore-scroll"; import { restoreScroll } from "../../common/decorators/restore-scroll";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { computeDomain } from "../../common/entity/compute_domain"; import { computeDomain } from "../../common/entity/compute_domain";
import { domainIconWithoutDefault } from "../../common/entity/domain_icon";
import { isComponentLoaded } from "../../common/config/is_component_loaded"; import { isComponentLoaded } from "../../common/config/is_component_loaded";
import { computeRTL, emitRTLDirection } from "../../common/util/compute_rtl"; import { computeRTL, emitRTLDirection } from "../../common/util/compute_rtl";
import "../../components/entity/state-badge"; import "../../components/entity/state-badge";
@ -131,7 +131,7 @@ class HaLogbookRenderer extends LitElement {
const seenEntityIds: string[] = []; const seenEntityIds: string[] = [];
const previous = this.entries[index - 1]; const previous = this.entries[index - 1];
const stateObj = item.entity_id const currentStateObj = item.entity_id
? this.hass.states[item.entity_id] ? this.hass.states[item.entity_id]
: undefined; : undefined;
const item_username = const item_username =
@ -140,25 +140,36 @@ class HaLogbookRenderer extends LitElement {
? computeDomain(item.entity_id) ? computeDomain(item.entity_id)
: // Domain is there if there is no entity ID. : // Domain is there if there is no entity ID.
item.domain!; item.domain!;
const overrideIcon = const historicStateObj = item.entity_id ? <HassEntity>(<unknown>{
item.icon || entity_id: item.entity_id,
(item.domain && !stateObj state: item.state,
? domainIconWithoutDefault(item.domain!) attributes: {
: undefined); // Rebuild the historical state by copying static attributes only
const overrideImage = !DOMAINS_WITH_DYNAMIC_PICTURE.has(domain) device_class: currentStateObj?.attributes.device_class,
? stateObj?.attributes.entity_picture_local || source_type: currentStateObj?.attributes.source_type,
stateObj?.attributes.entity_picture || has_date: currentStateObj?.attributes.has_date,
(!stateObj && has_time: currentStateObj?.attributes.has_time,
!overrideIcon && // We do not want to use dynamic entity pictures (e.g., from media player) for the log book rendering,
item.domain && // as they would present a false state in the log (played media right now vs actual historic data).
isComponentLoaded(this.hass, item.domain) entity_picture_local: DOMAINS_WITH_DYNAMIC_PICTURE.has(domain)
? undefined
: currentStateObj?.attributes.entity_picture_local,
entity_picture: DOMAINS_WITH_DYNAMIC_PICTURE.has(domain)
? undefined
: currentStateObj?.attributes.entity_picture,
},
}) : undefined;
const overrideImage =
!historicStateObj &&
!item.icon &&
domain &&
isComponentLoaded(this.hass, domain)
? brandsUrl({ ? brandsUrl({
domain: item.domain!, domain: domain!,
type: "icon", type: "icon",
useFallback: true, useFallback: true,
darkOptimized: this.hass.themes?.darkMode, darkOptimized: this.hass.themes?.darkMode,
}) })
: undefined)
: undefined; : undefined;
return html` return html`
@ -178,14 +189,12 @@ class HaLogbookRenderer extends LitElement {
<div class="entry ${classMap({ "no-entity": !item.entity_id })}"> <div class="entry ${classMap({ "no-entity": !item.entity_id })}">
<div class="icon-message"> <div class="icon-message">
${!this.noIcon ${!this.noIcon
? // We do not want to use dynamic entity pictures (e.g., from media player) for the log book rendering, ? html`
// as they would present a false state in the log (played media right now vs actual historic data).
html`
<state-badge <state-badge
.hass=${this.hass} .hass=${this.hass}
.overrideIcon=${overrideIcon} .overrideIcon=${item.icon}
.overrideImage=${overrideImage} .overrideImage=${overrideImage}
.stateObj=${stateObj} .stateObj=${item.icon ? undefined : historicStateObj}
.stateColor=${false} .stateColor=${false}
></state-badge> ></state-badge>
` `