From 1aa40cb6dfd318857f06b13206ed0f67f7887730 Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Thu, 14 Jan 2021 12:49:29 +0100 Subject: [PATCH] Convert state-card-display to LitElement and use timestamp display (#8150) Co-authored-by: Bram Kragten --- src/components/entity/state-info.ts | 2 +- src/state-summary/state-card-display.js | 102 ---------------------- src/state-summary/state-card-display.ts | 110 ++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 103 deletions(-) delete mode 100755 src/state-summary/state-card-display.js create mode 100755 src/state-summary/state-card-display.ts diff --git a/src/components/entity/state-info.ts b/src/components/entity/state-info.ts index a56f36638f..d493fb625d 100644 --- a/src/components/entity/state-info.ts +++ b/src/components/entity/state-info.ts @@ -23,7 +23,7 @@ class StateInfo extends LitElement { @property({ type: Boolean }) public inDialog = false; - // property used only in css + // property used only in CSS @property({ type: Boolean, reflect: true }) public rtl = false; protected render(): TemplateResult { diff --git a/src/state-summary/state-card-display.js b/src/state-summary/state-card-display.js deleted file mode 100755 index b49bb16845..0000000000 --- a/src/state-summary/state-card-display.js +++ /dev/null @@ -1,102 +0,0 @@ -import "@polymer/iron-flex-layout/iron-flex-layout-classes"; -import { html } from "@polymer/polymer/lib/utils/html-tag"; -/* eslint-plugin-disable lit */ -import { PolymerElement } from "@polymer/polymer/polymer-element"; -import { attributeClassNames } from "../common/entity/attribute_class_names"; -import { computeStateDisplay } from "../common/entity/compute_state_display"; -import { computeRTL } from "../common/util/compute_rtl"; -import "../components/entity/state-info"; -import LocalizeMixin from "../mixins/localize-mixin"; - -/* - * @appliesMixin LocalizeMixin - */ -class StateCardDisplay extends LocalizeMixin(PolymerElement) { - static get template() { - return html` - - - ${this.stateInfoTemplate} -
- [[computeStateDisplay(localize, stateObj, hass.language)]] -
- `; - } - - static get stateInfoTemplate() { - return html` - - `; - } - - static get properties() { - return { - hass: Object, - stateObj: Object, - inDialog: { - type: Boolean, - value: false, - }, - rtl: { - type: Boolean, - reflectToAttribute: true, - computed: "_computeRTL(hass)", - }, - }; - } - - computeStateDisplay(localize, stateObj, language) { - return computeStateDisplay(localize, stateObj, language); - } - - computeClassNames(stateObj) { - const classes = [ - "state", - attributeClassNames(stateObj, ["unit_of_measurement"]), - ]; - return classes.join(" "); - } - - _computeRTL(hass) { - return computeRTL(hass); - } -} -customElements.define("state-card-display", StateCardDisplay); diff --git a/src/state-summary/state-card-display.ts b/src/state-summary/state-card-display.ts new file mode 100755 index 0000000000..23b31b8d6b --- /dev/null +++ b/src/state-summary/state-card-display.ts @@ -0,0 +1,110 @@ +import "@polymer/iron-flex-layout/iron-flex-layout-classes"; +import type { HassEntity } from "home-assistant-js-websocket"; +import { + css, + CSSResult, + customElement, + html, + LitElement, + property, + TemplateResult, +} from "lit-element"; +import { classMap } from "lit-html/directives/class-map"; +import { computeDomain } from "../common/entity/compute_domain"; +import { computeStateDisplay } from "../common/entity/compute_state_display"; +import { computeRTL } from "../common/util/compute_rtl"; +import "../components/entity/state-info"; +import { UNAVAILABLE_STATES } from "../data/entity"; +import "../panels/lovelace/components/hui-timestamp-display"; +import { haStyle } from "../resources/styles"; +import type { HomeAssistant } from "../types"; + +@customElement("state-card-display") +export class StateCardDisplay extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property({ attribute: false }) public stateObj!: HassEntity; + + @property({ type: Boolean }) public inDialog = false; + + // property used only in CSS + @property({ type: Boolean, reflect: true }) public rtl = false; + + protected render(): TemplateResult { + return html` +
+ + +
+ ${computeDomain(this.stateObj.entity_id) === "sensor" && + this.stateObj.attributes.device_class === "timestamp" && + !UNAVAILABLE_STATES.includes(this.stateObj.state) + ? html` ` + : computeStateDisplay( + this.hass!.localize, + this.stateObj, + this.hass.language + )} +
+
+ `; + } + + protected updated(changedProps) { + super.updated(changedProps); + if (!changedProps.has("hass")) { + return; + } + + const oldHass = changedProps.get("hass") as HomeAssistant | undefined; + if (!oldHass || oldHass.language !== this.hass.language) { + this.rtl = computeRTL(this.hass); + } + } + + static get styles(): CSSResult[] { + return [ + haStyle, + css` + :host([rtl]) { + direction: rtl; + text-align: right; + } + + state-info { + flex: 1 1 auto; + min-width: 0; + } + .state { + color: var(--primary-text-color); + margin-left: 16px; + text-align: right; + flex: 0 0 auto; + overflow-wrap: break-word; + } + :host([rtl]) .state { + margin-right: 16px; + margin-left: 0; + text-align: left; + } + + .state.has-unit_of_measurement { + white-space: nowrap; + } + `, + ]; + } +}