From 12c935f647302b5af9f3f6a698e186b61185a7b4 Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Mon, 25 Jan 2021 11:48:16 +0100 Subject: [PATCH] Show entity_picture in logbook (#8118) --- src/common/const.ts | 3 +++ src/components/entity/state-badge.ts | 23 ++++++++++++++---- src/panels/logbook/ha-logbook.ts | 35 ++++++++++++++++------------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/common/const.ts b/src/common/const.ts index 4b4f8e6225..2b89dbb81d 100644 --- a/src/common/const.ts +++ b/src/common/const.ts @@ -138,6 +138,9 @@ export const DOMAINS_TOGGLE = new Set([ "humidifier", ]); +/** Domains that have a dynamic entity image / picture. */ +export const DOMAINS_WITH_DYNAMIC_PICTURE = new Set(["camera", "media_player"]); + /** Temperature units. */ export const UNIT_C = "°C"; export const UNIT_F = "°F"; diff --git a/src/components/entity/state-badge.ts b/src/components/entity/state-badge.ts index 737134271e..4711bf4dfc 100644 --- a/src/components/entity/state-badge.ts +++ b/src/components/entity/state-badge.ts @@ -37,7 +37,8 @@ export class StateBadge extends LitElement { protected render(): TemplateResult { const stateObj = this.stateObj; - if (!stateObj) { + // We either need a `stateObj` or one override + if (!stateObj && !this.overrideIcon && !this.overrideImage) { return html`
`; @@ -47,7 +48,7 @@ export class StateBadge extends LitElement { return html``; } - const domain = computeStateDomain(stateObj); + const domain = stateObj ? computeStateDomain(stateObj) : undefined; return html` `; } protected updated(changedProps: PropertyValues) { - if (!changedProps.has("stateObj") || !this.stateObj) { + if ( + !changedProps.has("stateObj") && + !changedProps.has("overrideImage") && + !changedProps.has("overrideIcon") + ) { return; } const stateObj = this.stateObj; @@ -114,7 +119,15 @@ export class StateBadge extends LitElement { iconStyle.filter = `brightness(${(brightness + 245) / 5}%)`; } } + } else if (this.overrideImage) { + let imageUrl = this.overrideImage; + if (this.hass) { + imageUrl = this.hass.hassUrl(imageUrl); + } + hostStyle.backgroundImage = `url(${imageUrl})`; + this._showIcon = false; } + this._iconStyle = iconStyle; Object.assign(this.style, hostStyle); } diff --git a/src/panels/logbook/ha-logbook.ts b/src/panels/logbook/ha-logbook.ts index 781e0fca98..9f9be9eb5b 100644 --- a/src/panels/logbook/ha-logbook.ts +++ b/src/panels/logbook/ha-logbook.ts @@ -11,6 +11,7 @@ import { } from "lit-element"; import { classMap } from "lit-html/directives/class-map"; import { scroll } from "lit-virtualizer"; +import { DOMAINS_WITH_DYNAMIC_PICTURE } from "../../common/const"; import { formatDate } from "../../common/datetime/format_date"; import { formatTimeWithSeconds } from "../../common/datetime/format_time"; import { restoreScroll } from "../../common/decorators/restore-scroll"; @@ -18,8 +19,8 @@ import { fireEvent } from "../../common/dom/fire_event"; import { computeDomain } from "../../common/entity/compute_domain"; import { domainIcon } from "../../common/entity/domain_icon"; import { computeRTL, emitRTLDirection } from "../../common/util/compute_rtl"; +import "../../components/entity/state-badge"; import "../../components/ha-circular-progress"; -import "../../components/ha-icon"; import "../../components/ha-relative-time"; import { LogbookEntry } from "../../data/logbook"; import { haStyle, haStyleScrollbar } from "../../resources/styles"; @@ -111,9 +112,12 @@ class HaLogbook extends LitElement { } const previous = this.entries[index - 1]; - const state = item.entity_id ? this.hass.states[item.entity_id] : undefined; + const stateObj = item.entity_id + ? this.hass.states[item.entity_id] + : undefined; const item_username = item.context_user_id && this.userIdToName[item.context_user_id]; + const domain = item.entity_id ? computeDomain(item.entity_id) : item.domain; return html`
@@ -132,17 +136,18 @@ class HaLogbook extends LitElement {
${!this.noIcon - ? html` - + ? // We do not want to use dynamic entity pictures (e.g., from media player) for the log book rendering, + // as they would present a false state in the log (played media right now vs actual historic data). + html` + ` : ""}
@@ -295,7 +300,7 @@ class HaLogbook extends LitElement { color: var(--secondary-text-color); } - ha-icon { + state-badge { margin-right: 16px; flex-shrink: 0; color: var(--state-icon-color); @@ -330,7 +335,7 @@ class HaLogbook extends LitElement { padding: 8px; } - .narrow .icon-message ha-icon { + .narrow .icon-message state-badge { margin-left: 0; } `,