Skip to content

Commit 33eb6df

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., custom integrations, person entities, etc.). Changes: - Add use_entity_picture boolean option to PictureEntityCardConfig - Implement logic to check entity_picture attribute when enabled - Add editor toggle with helper text - Add translation strings for the new option
1 parent 274ec50 commit 33eb6df

4 files changed

Lines changed: 39 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: 21 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: {
@@ -209,17 +211,32 @@ export class HuiPictureEntityCardEditor
209211

210212
private _valueChanged(ev: CustomEvent): void {
211213
const config = ev.detail.value;
214+
215+
// Remove stub image when entity changes to one with entity_picture
212216
if (
213217
config.entity &&
214218
config.entity !== this._config?.entity &&
215219
(computeDomain(config.entity) === "image" ||
216220
(computeDomain(config.entity) === "person" &&
221+
this.hass?.states[config.entity]?.attributes.entity_picture) ||
222+
(config.use_entity_picture &&
217223
this.hass?.states[config.entity]?.attributes.entity_picture)) &&
218224
config.image === STUB_IMAGE
219225
) {
220226
delete config.image;
221227
}
222228

229+
// Remove stub image when use_entity_picture is enabled on an entity with entity_picture
230+
if (
231+
config.use_entity_picture &&
232+
!this._config?.use_entity_picture &&
233+
config.entity &&
234+
this.hass?.states[config.entity]?.attributes.entity_picture &&
235+
config.image === STUB_IMAGE
236+
) {
237+
delete config.image;
238+
}
239+
223240
fireEvent(this, "config-changed", { config });
224241
}
225242

@@ -247,6 +264,10 @@ export class HuiPictureEntityCardEditor
247264
schema: SchemaUnion<ReturnType<typeof this._schema>>
248265
) => {
249266
switch (schema.name) {
267+
case "use_entity_picture":
268+
return this.hass!.localize(
269+
`ui.panel.lovelace.editor.card.generic.use_entity_picture_helper`
270+
);
250271
case "aspect_ratio":
251272
return typeof this._config?.grid_options?.rows === "number"
252273
? 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)