Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/data/entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const UNAVAILABLE = "unavailable";
197 changes: 0 additions & 197 deletions src/panels/lovelace/cards/hui-picture-entity-card.js

This file was deleted.

175 changes: 175 additions & 0 deletions src/panels/lovelace/cards/hui-picture-entity-card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import { TemplateResult } from "lit-html/lib/shady-render";
import { classMap } from "lit-html/directives/classMap";

import "../../../components/ha-card";
import "../components/hui-image";

import computeDomain from "../../../common/entity/compute_domain";
import computeStateDisplay from "../../../common/entity/compute_state_display";
import computeStateName from "../../../common/entity/compute_state_name";

import { longPress } from "../common/directives/long-press-directive";
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
import { HomeAssistant } from "../../../types";
import { LovelaceCardConfig } from "../../../data/lovelace";
import { LovelaceCard } from "../types";
import { handleClick } from "../common/handle-click";
import { UNAVAILABLE } from "../../../data/entity";

interface Config extends LovelaceCardConfig {
entity: string;
name?: string;
navigation_path?: string;
image?: string;
camera_image?: string;
state_image?: {};
aspect_ratio?: string;
tap_action?: "toggle" | "call-service" | "more-info" | "navigate";
hold_action?: "toggle" | "call-service" | "more-info" | "navigate";
service?: string;
service_data?: object;
show_name?: boolean;
show_state?: boolean;
}

class HuiPictureEntityCard extends hassLocalizeLitMixin(LitElement)
implements LovelaceCard {
public hass?: HomeAssistant;
private _config?: Config;

static get properties(): PropertyDeclarations {
return {
hass: {},
_config: {},
};
}

public getCardSize(): number {
return 3;
}

public setConfig(config: Config): void {
if (!config || !config.entity) {
throw new Error("Invalid Configuration: 'entity' required");
}

if (
computeDomain(config.entity) !== "camera" &&
(!config.image && !config.state_image && !config.camera_image)
) {
throw new Error("No image source configured.");
}

this._config = { show_name: true, show_state: true, ...config };
}

protected render(): TemplateResult {
if (!this._config || !this.hass || !this.hass.states[this._config.entity]) {
return html``;
}

const stateObj = this.hass.states[this._config.entity];
const name = this._config.name || computeStateName(stateObj);
const state = computeStateDisplay(
this.localize,
stateObj,
this.hass.language
);

let footer: TemplateResult | string = "";
if (this._config.show_name && this._config.show_state) {
footer = html`
<div class="footer both">
<div>${name}</div>
<div>${state}</div>
</div>
`;
} else if (this._config.show_name) {
footer = html`
<div class="footer">${name}</div>
`;
} else if (this._config.show_state) {
footer = html`
<div class="footer state">${state}</div>
`;
}

return html`
${this.renderStyle()}
<ha-card>
<hui-image
.hass="${this.hass}"
.image="${this._config.image}"
.stateImage="${this._config.state_image}"
.cameraImage="${
computeDomain(this._config.entity) === "camera"
? this._config.entity
: this._config.camera_image
}"
.entity="${this._config.entity}"
.aspectRatio="${this._config.aspect_ratio}"
@ha-click="${this._handleClick}"
@ha-hold="${this._handleHold}"
.longPress="${longPress()}"
class="${
classMap({
clickable: stateObj.state !== UNAVAILABLE,
})
}"
></hui-image>
${footer}
</ha-card>
`;
}

private renderStyle(): TemplateResult {
return html`
<style>
ha-card {
min-height: 75px;
overflow: hidden;
position: relative;
}
hui-image.clickable {
cursor: pointer;
}
.footer {
@apply --paper-font-common-nowrap;
position: absolute;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.3);
padding: 16px;
font-size: 16px;
line-height: 16px;
color: white;
}
.both {
display: flex;
justify-content: space-between;
}
.state {
text-align: right;
}
</style>
`;
}

private _handleClick() {
handleClick(this, this.hass!, this._config!, false);
}

private _handleHold() {
handleClick(this, this.hass!, this._config!, true);
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-picture-entity-card": HuiPictureEntityCard;
}
}

customElements.define("hui-picture-entity-card", HuiPictureEntityCard);
3 changes: 2 additions & 1 deletion src/panels/lovelace/common/handle-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { LovelaceElementConfig } from "../elements/types";
import { fireEvent } from "../../../common/dom/fire_event";
import { navigate } from "../../../common/navigate";
import { toggleEntity } from "../../../../src/panels/lovelace/common/entity/toggle-entity";
import { LovelaceCardConfig } from "../../../data/lovelace";

export const handleClick = (
node: HTMLElement,
hass: HomeAssistant,
config: LovelaceElementConfig,
config: LovelaceElementConfig | LovelaceCardConfig,
hold: boolean
): void => {
let action = config.tap_action || "more-info";
Expand Down