Add option to show entity picture in tile card (#14196)

Add option to show entity picture
This commit is contained in:
Paul Bottein 2022-10-26 18:31:10 +02:00 committed by GitHub
parent 5c4517517d
commit 504e8dd946
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 14 deletions

View File

@ -0,0 +1,41 @@
import { CSSResultGroup, html, css, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators";
@customElement("ha-tile-image")
export class HaTileImage extends LitElement {
@property() public imageUrl?: string;
protected render(): TemplateResult {
return html`
<div class="image">
${this.imageUrl ? html`<img src=${this.imageUrl} />` : null}
</div>
`;
}
static get styles(): CSSResultGroup {
return css`
.image {
position: relative;
width: 40px;
height: 40px;
border-radius: 20px;
display: flex;
flex: none;
align-items: center;
justify-content: center;
overflow: hidden;
}
.image img {
width: 100%;
height: 100%;
}
`;
}
}
declare global {
interface HTMLElementTagNameMap {
"ha-tile-image": HaTileImage;
}
}

View File

@ -1,4 +1,5 @@
import { mdiHelp } from "@mdi/js"; import { mdiHelp } from "@mdi/js";
import { HassEntity } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement } from "lit"; import { css, CSSResultGroup, html, LitElement } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { styleMap } from "lit/directives/style-map"; import { styleMap } from "lit/directives/style-map";
@ -11,7 +12,9 @@ import { stateColorCss } from "../../../common/entity/state_color";
import { stateIconPath } from "../../../common/entity/state_icon_path"; import { stateIconPath } from "../../../common/entity/state_icon_path";
import "../../../components/ha-card"; import "../../../components/ha-card";
import "../../../components/tile/ha-tile-icon"; import "../../../components/tile/ha-tile-icon";
import "../../../components/tile/ha-tile-image";
import "../../../components/tile/ha-tile-info"; import "../../../components/tile/ha-tile-info";
import { cameraUrlWithWidthHeight } from "../../../data/camera";
import { ActionHandlerEvent } from "../../../data/lovelace"; import { ActionHandlerEvent } from "../../../data/lovelace";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive"; import { actionHandler } from "../common/directives/action-handler-directive";
@ -87,6 +90,21 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
handleAction(this, this.hass!, config, "tap"); handleAction(this, this.hass!, config, "tap");
} }
private _getImageUrl(entity: HassEntity): string | undefined {
const entityPicture =
entity.attributes.entity_picture_local ||
entity.attributes.entity_picture;
if (!entityPicture) return undefined;
let imageUrl = this.hass!.hassUrl(entityPicture);
if (computeDomain(entity.entity_id) === "camera") {
imageUrl = cameraUrlWithWidthHeight(imageUrl, 80, 80);
}
return imageUrl;
}
render() { render() {
if (!this._config || !this.hass) { if (!this._config || !this.hass) {
return html``; return html``;
@ -126,17 +144,35 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
: stateColorCss(entity), : stateColorCss(entity),
}; };
const imageUrl = this._config.show_entity_picture
? this._getImageUrl(entity)
: undefined;
return html` return html`
<ha-card style=${styleMap(style)}> <ha-card style=${styleMap(style)}>
<div class="tile"> <div class="tile">
<ha-tile-icon ${imageUrl
.icon=${icon} ? html`
.iconPath=${iconPath} <ha-tile-image
role="button" class="icon"
tabindex="0" .imageUrl=${imageUrl}
@action=${this._handleIconAction} role="button"
.actionHandler=${actionHandler()} tabindex="0"
></ha-tile-icon> @action=${this._handleIconAction}
.actionHandler=${actionHandler()}
></ha-tile-image>
`
: html`
<ha-tile-icon
class="icon"
.icon=${icon}
.iconPath=${iconPath}
role="button"
tabindex="0"
@action=${this._handleIconAction}
.actionHandler=${actionHandler()}
></ha-tile-icon>
`}
<ha-tile-info <ha-tile-info
.primary=${name} .primary=${name}
.secondary=${stateDisplay} .secondary=${stateDisplay}
@ -168,7 +204,7 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
} }
ha-tile-icon { .icon {
padding: var(--tile-tap-padding); padding: var(--tile-tap-padding);
flex: none; flex: none;
margin-right: calc(12px - 2 * var(--tile-tap-padding)); margin-right: calc(12px - 2 * var(--tile-tap-padding));
@ -181,13 +217,13 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
[role="button"] { [role="button"] {
cursor: pointer; cursor: pointer;
} }
ha-tile-icon[role="button"]:focus { .icon[role="button"]:focus {
outline: none; outline: none;
} }
ha-tile-icon[role="button"]:focus-visible { .icon[role="button"]:focus-visible {
transform: scale(1.2); transform: scale(1.2);
} }
ha-tile-icon[role="button"]:active { .icon[role="button"]:active {
transform: scale(1.2); transform: scale(1.2);
} }
ha-tile-info { ha-tile-info {

View File

@ -478,6 +478,7 @@ export interface TileCardConfig extends LovelaceCardConfig {
name?: string; name?: string;
icon?: string; icon?: string;
color?: string; color?: string;
show_entity_picture?: string;
tap_action?: ActionConfig; tap_action?: ActionConfig;
icon_tap_action?: ActionConfig; icon_tap_action?: ActionConfig;
} }

View File

@ -3,7 +3,7 @@ import { HassEntity } from "home-assistant-js-websocket";
import { css, html, LitElement, TemplateResult } from "lit"; import { css, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one"; import memoizeOne from "memoize-one";
import { assert, assign, object, optional, string } from "superstruct"; import { assert, assign, boolean, object, optional, string } from "superstruct";
import { THEME_COLORS } from "../../../../common/color/compute-color"; import { THEME_COLORS } from "../../../../common/color/compute-color";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain"; import { computeDomain } from "../../../../common/entity/compute_domain";
@ -25,6 +25,7 @@ const cardConfigStruct = assign(
name: optional(string()), name: optional(string()),
icon: optional(string()), icon: optional(string()),
color: optional(string()), color: optional(string()),
show_entity_picture: optional(boolean()),
tap_action: optional(actionConfigStruct), tap_action: optional(actionConfigStruct),
icon_tap_action: optional(actionConfigStruct), icon_tap_action: optional(actionConfigStruct),
}) })
@ -90,6 +91,12 @@ export class HuiTileCardEditor
}, },
}, },
}, },
{
name: "show_entity_picture",
selector: {
boolean: {},
},
},
] as const, ] as const,
}, },
] as const ] as const
@ -205,9 +212,11 @@ export class HuiTileCardEditor
switch (schema.name) { switch (schema.name) {
case "color": case "color":
case "icon_tap_action": case "icon_tap_action":
case "show_entity_picture":
return this.hass!.localize( return this.hass!.localize(
`ui.panel.lovelace.editor.card.tile.${schema.name}` `ui.panel.lovelace.editor.card.tile.${schema.name}`
); );
default: default:
return this.hass!.localize( return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}` `ui.panel.lovelace.editor.card.generic.${schema.name}`

View File

@ -4140,7 +4140,8 @@
"icon_tap_action": "Icon tap action", "icon_tap_action": "Icon tap action",
"actions": "Actions", "actions": "Actions",
"appearance": "Appearance", "appearance": "Appearance",
"default_color": "Default color (state)" "default_color": "Default color (state)",
"show_entity_picture": "Show entity picture"
}, },
"vertical-stack": { "vertical-stack": {
"name": "Vertical Stack", "name": "Vertical Stack",