From 6c44a92e2c229f6597e9e2665d87a0b03ff9f023 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Thu, 8 Nov 2018 02:41:46 -0600 Subject: [PATCH] Convert hui-group-entity-row to TypeScript/LitElement (#2015) * Convert hui-group-entity-row to TypeScript/LitElement * Address review comment and Travis errors --- .../entity-rows/hui-group-entity-row.js | 73 --------------- .../entity-rows/hui-group-entity-row.ts | 88 +++++++++++++++++++ 2 files changed, 88 insertions(+), 73 deletions(-) delete mode 100644 src/panels/lovelace/entity-rows/hui-group-entity-row.js create mode 100644 src/panels/lovelace/entity-rows/hui-group-entity-row.ts diff --git a/src/panels/lovelace/entity-rows/hui-group-entity-row.js b/src/panels/lovelace/entity-rows/hui-group-entity-row.js deleted file mode 100644 index d66a42c015..0000000000 --- a/src/panels/lovelace/entity-rows/hui-group-entity-row.js +++ /dev/null @@ -1,73 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag"; -import { PolymerElement } from "@polymer/polymer/polymer-element"; - -import "../components/hui-generic-entity-row"; -import "../../../components/entity/ha-entity-toggle"; - -import computeStateDisplay from "../../../common/entity/compute_state_display"; -import { DOMAINS_TOGGLE } from "../../../common/const"; -import LocalizeMixin from "../../../mixins/localize-mixin"; - -/* - * @appliesMixin LocalizeMixin - */ -class HuiGroupEntityRow extends LocalizeMixin(PolymerElement) { - static get template() { - return html` - - ${this.groupControlTemplate} - - `; - } - - static get groupControlTemplate() { - return html` - - - `; - } - - static get properties() { - return { - hass: Object, - _config: Object, - _stateObj: { - type: Object, - computed: "_computeStateObj(hass.states, _config.entity)", - }, - _canToggle: { - type: Boolean, - computed: "_computeCanToggle(_stateObj.attributes.entity_id)", - }, - }; - } - - setConfig(config) { - if (!config || !config.entity) { - throw new Error("Entity not configured."); - } - this._config = config; - } - - _computeStateObj(states, entityId) { - return states && entityId in states ? states[entityId] : null; - } - - _computeCanToggle(entityIds) { - return entityIds.some((entityId) => - DOMAINS_TOGGLE.has(entityId.split(".", 1)[0]) - ); - } - - _computeState(stateObj) { - return computeStateDisplay(this.localize, stateObj); - } -} -customElements.define("hui-group-entity-row", HuiGroupEntityRow); diff --git a/src/panels/lovelace/entity-rows/hui-group-entity-row.ts b/src/panels/lovelace/entity-rows/hui-group-entity-row.ts new file mode 100644 index 0000000000..86b4a41281 --- /dev/null +++ b/src/panels/lovelace/entity-rows/hui-group-entity-row.ts @@ -0,0 +1,88 @@ +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; + +import "../components/hui-generic-entity-row"; +import "../../../components/entity/ha-entity-toggle"; +import "./hui-error-entity-row"; + +import computeStateDisplay from "../../../common/entity/compute_state_display"; +import { DOMAINS_TOGGLE } from "../../../common/const"; +import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; +import { HomeAssistant } from "../../../types"; +import { EntityRow, EntityConfig } from "./types"; + +class HuiGroupEntityRow extends hassLocalizeLitMixin(LitElement) + implements EntityRow { + public hass?: HomeAssistant; + private _config?: EntityConfig; + + static get properties(): PropertyDeclarations { + return { + hass: {}, + _config: {}, + }; + } + + public setConfig(config: EntityConfig): void { + if (!config) { + throw new Error("Configuration error"); + } + this._config = config; + } + + protected render(): TemplateResult { + if (!this._config || !this.hass) { + return html``; + } + + const stateObj = this.hass.states[this._config.entity]; + + if (!stateObj) { + return html` + + `; + } + + return html` + + > + ${ + this._computeCanToggle(stateObj.attributes.entity_id) + ? html` + + ` + : html` +
+ ${ + computeStateDisplay( + this.localize, + stateObj, + this.hass.language + ) + } +
+ ` + } +
+ `; + } + + private _computeCanToggle(entityIds): boolean { + return entityIds.some((entityId) => + DOMAINS_TOGGLE.has(entityId.split(".", 1)[0]) + ); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-group-entity-row": HuiGroupEntityRow; + } +} + +customElements.define("hui-group-entity-row", HuiGroupEntityRow);