import { css, CSSResult, customElement, html, LitElement, property, PropertyValues, TemplateResult, } from "lit-element"; import { computeStateDisplay } from "../../../common/entity/compute_state_display"; import { HomeAssistant } from "../../../types"; import { hasConfigOrEntityChanged } from "../common/has-changed"; import "../components/hui-generic-entity-row"; import { createEntityNotFoundWarning } from "../components/hui-warning"; import { LovelaceRow } from "./types"; import { DOMAINS_HIDE_MORE_INFO } from "../../../common/const"; import { computeDomain } from "../../../common/entity/compute_domain"; import { actionHandler } from "../common/directives/action-handler-directive"; import { hasAction } from "../common/has-action"; import { ActionHandlerEvent } from "../../../data/lovelace"; import { handleAction } from "../common/handle-action"; import { classMap } from "lit-html/directives/class-map"; import { EntitiesCardEntityConfig } from "../cards/types"; @customElement("hui-text-entity-row") class HuiTextEntityRow extends LitElement implements LovelaceRow { @property() public hass?: HomeAssistant; @property() private _config?: EntitiesCardEntityConfig; public setConfig(config: EntitiesCardEntityConfig): void { if (!config) { throw new Error("Configuration error"); } this._config = config; } protected shouldUpdate(changedProps: PropertyValues): boolean { return hasConfigOrEntityChanged(this, changedProps); } protected render(): TemplateResult { if (!this._config || !this.hass) { return html``; } const stateObj = this.hass.states[this._config.entity]; if (!stateObj) { return html` ${createEntityNotFoundWarning(this.hass, this._config.entity)} `; } const pointer = (this._config.tap_action && this._config.tap_action.action !== "none") || (this._config.entity && !DOMAINS_HIDE_MORE_INFO.includes(computeDomain(this._config.entity))); return html`
${computeStateDisplay( this.hass!.localize, stateObj, this.hass.language )}
`; } private _handleAction(ev: ActionHandlerEvent) { handleAction(this, this.hass!, this._config!, ev.detail.action); } static get styles(): CSSResult { return css` div { text-align: right; } .pointer { cursor: pointer; } `; } } declare global { interface HTMLElementTagNameMap { "hui-text-entity-row": HuiTextEntityRow; } }