From aea98f702b81aba22c026db06e8fe98cd464272d Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Mon, 3 Feb 2025 07:01:16 -0800 Subject: [PATCH] Prioritize local image over entity_picture in picture-entity card (#24032) * Prioritize local image over entity_picture in picture-entity * Remove the default stub image if we switch to an entity with a picture * minor cleanup --- .../lovelace/cards/hui-picture-entity-card.ts | 25 +++++++++++-------- .../hui-picture-entity-card-editor.ts | 16 +++++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/panels/lovelace/cards/hui-picture-entity-card.ts b/src/panels/lovelace/cards/hui-picture-entity-card.ts index 8d6bcb273c..901e46c225 100644 --- a/src/panels/lovelace/cards/hui-picture-entity-card.ts +++ b/src/panels/lovelace/cards/hui-picture-entity-card.ts @@ -22,6 +22,9 @@ import type { PictureEntityCardConfig } from "./types"; import type { CameraEntity } from "../../../data/camera"; import type { PersonEntity } from "../../../data/person"; +export const STUB_IMAGE = + "https://demo.home-assistant.io/stub_config/bedroom.png"; + @customElement("hui-picture-entity-card") class HuiPictureEntityCard extends LitElement implements LovelaceCard { public static async getConfigElement(): Promise { @@ -46,7 +49,7 @@ class HuiPictureEntityCard extends LitElement implements LovelaceCard { return { type: "picture-entity", entity: foundEntities[0] || "", - image: "https://demo.home-assistant.io/stub_config/bedroom.png", + image: STUB_IMAGE, }; } @@ -134,15 +137,17 @@ class HuiPictureEntityCard extends LitElement implements LovelaceCard { const domain: string = computeDomain(this._config.entity); let image: string | undefined = this._config.image; - switch (domain) { - case "image": - image = computeImageUrl(stateObj as ImageEntity); - break; - case "person": - if ((stateObj as PersonEntity).attributes.entity_picture) { - image = (stateObj as PersonEntity).attributes.entity_picture; - } - break; + if (!image) { + switch (domain) { + case "image": + image = computeImageUrl(stateObj as ImageEntity); + break; + case "person": + if ((stateObj as PersonEntity).attributes.entity_picture) { + image = (stateObj as PersonEntity).attributes.entity_picture; + } + break; + } } return html` diff --git a/src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts index 1b874b6a2a..4c9c7a0615 100644 --- a/src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts @@ -11,6 +11,8 @@ import type { LovelaceCardEditor } from "../../types"; import { actionConfigStruct } from "../structs/action-struct"; import { baseLovelaceCardConfig } from "../structs/base-card-struct"; import { configElementStyle } from "./config-elements-style"; +import { computeDomain } from "../../../../common/entity/compute_domain"; +import { STUB_IMAGE } from "../../cards/hui-picture-entity-card"; const cardConfigStruct = assign( baseLovelaceCardConfig, @@ -110,7 +112,19 @@ export class HuiPictureEntityCardEditor } private _valueChanged(ev: CustomEvent): void { - fireEvent(this, "config-changed", { config: ev.detail.value }); + const config = ev.detail.value; + if ( + config.entity && + config.entity !== this._config?.entity && + (computeDomain(config.entity) === "image" || + (computeDomain(config.entity) === "person" && + this.hass?.states[config.entity]?.attributes.entity_picture)) && + config.image === STUB_IMAGE + ) { + delete config.image; + } + + fireEvent(this, "config-changed", { config }); } private _computeLabelCallback = (schema: SchemaUnion) => {