Skip to content

Commit

Permalink
Prioritize local image over entity_picture in picture-entity card (#2…
Browse files Browse the repository at this point in the history
…4032)

* 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
  • Loading branch information
karwosts authored Feb 3, 2025
1 parent 863ff62 commit aea98f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
25 changes: 15 additions & 10 deletions src/panels/lovelace/cards/hui-picture-entity-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LovelaceCardEditor> {
Expand All @@ -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,
};
}

Expand Down Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<typeof SCHEMA>) => {
Expand Down

0 comments on commit aea98f7

Please sign in to comment.