mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-24 17:56:46 +00:00
Convert hui-toggle-entity-row to TypeScript/LitElement (#1939)
* Convert hui-toggle-entity-row to TypeScript/LitElement * Properly set generic-entity properties * Addressed review comments * Address review comments
This commit is contained in:
parent
14b959b91b
commit
9ce74e2da1
@ -1,76 +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 LocalizeMixin from "../../../mixins/localize-mixin";
|
|
||||||
|
|
||||||
/*
|
|
||||||
* @appliesMixin LocalizeMixin
|
|
||||||
*/
|
|
||||||
class HuiToggleEntityRow extends LocalizeMixin(PolymerElement) {
|
|
||||||
static get template() {
|
|
||||||
return html`
|
|
||||||
<hui-generic-entity-row
|
|
||||||
hass="[[hass]]"
|
|
||||||
config="[[_config]]"
|
|
||||||
>
|
|
||||||
${this.toggleControlTemplate}
|
|
||||||
</hui-generic-entity-row>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
static get toggleControlTemplate() {
|
|
||||||
return html`
|
|
||||||
<template is="dom-if" if="[[_canToggle]]">
|
|
||||||
<ha-entity-toggle
|
|
||||||
hass="[[hass]]"
|
|
||||||
state-obj="[[_stateObj]]"
|
|
||||||
></ha-entity-toggle>
|
|
||||||
</template>
|
|
||||||
<template is="dom-if" if="[[!_canToggle]]">
|
|
||||||
<div>
|
|
||||||
[[_computeState(_stateObj)]]
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
static get properties() {
|
|
||||||
return {
|
|
||||||
hass: Object,
|
|
||||||
_config: Object,
|
|
||||||
_stateObj: {
|
|
||||||
type: Object,
|
|
||||||
computed: "_computeStateObj(hass.states, _config.entity)",
|
|
||||||
},
|
|
||||||
_canToggle: {
|
|
||||||
type: Boolean,
|
|
||||||
computed: "_computeCanToggle(_stateObj.state)",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
_computeStateObj(states, entityId) {
|
|
||||||
return states && entityId in states ? states[entityId] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
_computeCanToggle(state) {
|
|
||||||
return state === "on" || state === "off";
|
|
||||||
}
|
|
||||||
|
|
||||||
_computeState(stateObj) {
|
|
||||||
return stateObj && computeStateDisplay(this.localize, stateObj);
|
|
||||||
}
|
|
||||||
|
|
||||||
setConfig(config) {
|
|
||||||
if (!config || !config.entity) {
|
|
||||||
throw new Error("Entity not configured.");
|
|
||||||
}
|
|
||||||
this._config = config;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
customElements.define("hui-toggle-entity-row", HuiToggleEntityRow);
|
|
78
src/panels/lovelace/entity-rows/hui-toggle-entity-row.ts
Normal file
78
src/panels/lovelace/entity-rows/hui-toggle-entity-row.ts
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
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 { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
||||||
|
import { HomeAssistant } from "../../../types";
|
||||||
|
import { EntityRow, EntityConfig } from "./types";
|
||||||
|
|
||||||
|
class HuiToggleEntityRow 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`
|
||||||
|
<hui-error-entity-row
|
||||||
|
.entity=${this._config.entity}
|
||||||
|
></hui-error-entity-row>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return html`
|
||||||
|
<hui-generic-entity-row
|
||||||
|
.hass=${this.hass}
|
||||||
|
.config=${this._config}
|
||||||
|
>
|
||||||
|
${
|
||||||
|
stateObj.state === "on" || stateObj.state === "off"
|
||||||
|
? html`
|
||||||
|
<ha-entity-toggle
|
||||||
|
.hass=${this.hass}
|
||||||
|
.stateObj=${stateObj}
|
||||||
|
></ha-entity-toggle>`
|
||||||
|
: html`
|
||||||
|
<div>
|
||||||
|
${computeStateDisplay(
|
||||||
|
this.localize,
|
||||||
|
stateObj,
|
||||||
|
this.hass!.language
|
||||||
|
)}
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
</hui-generic-entity-row>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"hui-toggle-entity-row": HuiToggleEntityRow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define("hui-toggle-entity-row", HuiToggleEntityRow);
|
Loading…
x
Reference in New Issue
Block a user