Skip to content

Commit 68f3cd0

Browse files
committed
Add use_entity_picture option to picture-entity card
Allow picture-entity cards to display the entity's entity_picture attribute instead of requiring a manually configured image URL. This is particularly useful for entities that dynamically set their entity_picture (e.g., update entities showing component logos, custom integrations, etc.). The static image configuration remains as a fallback when the entity_picture attribute is not present or when use_entity_picture is not enabled. Changes: - Add use_entity_picture boolean option to PictureEntityCardConfig - Implement logic to check entity_picture attribute when enabled - Update validation to accept use_entity_picture as valid image source - Add editor toggle with helper text - Add translation strings for the new option
1 parent b92775e commit 68f3cd0

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/panels/lovelace/cards/hui-picture-entity-card.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class HuiPictureEntityCard extends LitElement implements LovelaceCard {
7272
!["camera", "image", "person"].includes(computeDomain(config.entity)) &&
7373
!config.image &&
7474
!config.state_image &&
75-
!config.camera_image
75+
!config.camera_image &&
76+
!config.use_entity_picture
7677
) {
7778
throw new Error("No image source configured");
7879
}
@@ -143,10 +144,19 @@ class HuiPictureEntityCard extends LitElement implements LovelaceCard {
143144
}
144145

145146
const domain: string = computeDomain(this._config.entity);
146-
let image: string | undefined =
147-
(typeof this._config?.image === "object" &&
148-
this._config.image.media_content_id) ||
149-
(this._config.image as string | undefined);
147+
let image: string | undefined;
148+
149+
// Check if we should use entity_picture from the entity
150+
if (this._config.use_entity_picture && stateObj.attributes.entity_picture) {
151+
image = stateObj.attributes.entity_picture;
152+
} else {
153+
// Use configured image
154+
image =
155+
(typeof this._config?.image === "object" &&
156+
this._config.image.media_content_id) ||
157+
(this._config.image as string | undefined);
158+
}
159+
150160
if (!image) {
151161
switch (domain) {
152162
case "image":

src/panels/lovelace/cards/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ export interface PictureElementsCardConfig extends LovelaceCardConfig {
531531

532532
export interface PictureEntityCardConfig extends LovelaceCardConfig {
533533
entity: string;
534+
use_entity_picture?: boolean;
534535
name?: string | EntityNameItem | EntityNameItem[];
535536
image?: string | MediaSelectorValue;
536537
camera_image?: string;

src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const cardConfigStruct = assign(
3535
baseLovelaceCardConfig,
3636
object({
3737
entity: optional(string()),
38+
use_entity_picture: optional(boolean()),
3839
image: optional(union([string(), object()])),
3940
name: optional(entityNameStruct),
4041
camera_image: optional(string()),
@@ -68,6 +69,7 @@ export class HuiPictureEntityCardEditor
6869
(localize: LocalizeFunc) =>
6970
[
7071
{ name: "entity", required: true, selector: { entity: {} } },
72+
{ name: "use_entity_picture", selector: { boolean: {} } },
7173
{
7274
name: "name",
7375
selector: {
@@ -214,6 +216,8 @@ export class HuiPictureEntityCardEditor
214216
config.entity !== this._config?.entity &&
215217
(computeDomain(config.entity) === "image" ||
216218
(computeDomain(config.entity) === "person" &&
219+
this.hass?.states[config.entity]?.attributes.entity_picture) ||
220+
(config.use_entity_picture &&
217221
this.hass?.states[config.entity]?.attributes.entity_picture)) &&
218222
config.image === STUB_IMAGE
219223
) {
@@ -247,6 +251,10 @@ export class HuiPictureEntityCardEditor
247251
schema: SchemaUnion<ReturnType<typeof this._schema>>
248252
) => {
249253
switch (schema.name) {
254+
case "use_entity_picture":
255+
return this.hass!.localize(
256+
`ui.panel.lovelace.editor.card.generic.use_entity_picture_helper`
257+
);
250258
case "aspect_ratio":
251259
return typeof this._config?.grid_options?.rows === "number"
252260
? this.hass!.localize(

src/translations/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9325,6 +9325,8 @@
93259325
"attribute": "Attribute",
93269326
"camera_image": "Camera entity",
93279327
"image_entity": "Image entity",
9328+
"use_entity_picture": "Use entity picture",
9329+
"use_entity_picture_helper": "Use the entity's entity_picture attribute instead of a manually configured image",
93289330
"camera_view": "Camera view",
93299331
"camera_view_options": {
93309332
"auto": "Auto",

0 commit comments

Comments
 (0)