diff --git a/src/panels/lovelace/cards/hui-entity-button-card.ts b/src/panels/lovelace/cards/hui-entity-button-card.ts index 0aa86b7762..62c8394539 100644 --- a/src/panels/lovelace/cards/hui-entity-button-card.ts +++ b/src/panels/lovelace/cards/hui-entity-button-card.ts @@ -1,5 +1,12 @@ -import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; -import { fireEvent } from "../../../common/dom/fire_event.js"; +import { + html, + LitElement, + PropertyDeclarations, + PropertyValues, +} from "@polymer/lit-element"; +import { HassEntity } from "home-assistant-js-websocket"; +import { TemplateResult } from "lit-html"; +import { styleMap } from "lit-html/directives/styleMap.js"; import "../../../components/ha-card.js"; @@ -9,11 +16,11 @@ import stateIcon from "../../../common/entity/state_icon.js"; import computeStateDomain from "../../../common/entity/compute_state_domain.js"; import computeStateName from "../../../common/entity/compute_state_name.js"; import applyThemesOnElement from "../../../common/dom/apply_themes_on_element.js"; -import { styleMap } from "lit-html/directives/styleMap.js"; -import { HomeAssistant } from "../../../types.js"; +import { HomeAssistant, LightEntity } from "../../../types.js"; import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; import { LovelaceCard, LovelaceConfig } from "../types.js"; import { longPress } from "../common/directives/long-press-directive"; +import { fireEvent } from "../../../common/dom/fire_event.js"; interface Config extends LovelaceConfig { entity: string; @@ -29,38 +36,47 @@ interface Config extends LovelaceConfig { class HuiEntityButtonCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { public hass?: HomeAssistant; - protected config?: Config; + private _config?: Config; static get properties(): PropertyDeclarations { return { hass: {}, - config: {}, + _config: {}, }; } - public getCardSize() { + public getCardSize(): number { return 2; } - public setConfig(config: Config) { + public setConfig(config: Config): void { if (!isValidEntityId(config.entity)) { throw new Error("Invalid Entity"); } - this.config = { theme: "default", ...config }; - - if (this.hass) { - this.requestUpdate(); - } + this._config = { theme: "default", ...config }; } - protected render() { - if (!this.config) { + protected shouldUpdate(changedProps: PropertyValues): boolean { + if (changedProps.has("_config")) { + return true; + } + + const oldHass = changedProps.get("hass") as HomeAssistant | undefined; + if (oldHass) { + return ( + oldHass.states[this._config!.entity] !== + this.hass!.states[this._config!.entity] + ); + } + return true; + } + + protected render(): TemplateResult { + if (!this._config || !this.hass) { return html``; } - const stateObj = this.hass!.states[this.config.entity]; - - applyThemesOnElement(this, this.hass!.themes, this.config.theme); + const stateObj = this.hass.states[this._config.entity]; return html` ${this.renderStyle()} @@ -72,7 +88,7 @@ class HuiEntityButtonCard extends hassLocalizeLitMixin(LitElement) ${ !stateObj ? html`