diff --git a/src/panels/lovelace/common/create-hui-element.js b/src/panels/lovelace/common/create-hui-element.js index 3ee45b3466..502a4aa17f 100644 --- a/src/panels/lovelace/common/create-hui-element.js +++ b/src/panels/lovelace/common/create-hui-element.js @@ -1,6 +1,6 @@ import "../elements/hui-icon-element"; import "../elements/hui-image-element.js"; -import "../elements/hui-service-button-element.js"; +import "../elements/hui-service-button-element"; import "../elements/hui-state-badge-element.js"; import "../elements/hui-state-icon-element.js"; import "../elements/hui-state-label-element"; diff --git a/src/panels/lovelace/elements/hui-service-button-element.js b/src/panels/lovelace/elements/hui-service-button-element.js deleted file mode 100644 index c58ad4ebe0..0000000000 --- a/src/panels/lovelace/elements/hui-service-button-element.js +++ /dev/null @@ -1,46 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag.js"; -import { PolymerElement } from "@polymer/polymer/polymer-element.js"; - -import "../../../components/buttons/ha-call-service-button.js"; - -class HuiServiceButtonElement extends PolymerElement { - static get template() { - return html` - - [[_config.title]] - `; - } - - static get properties() { - return { - hass: Object, - _config: Object, - _domain: String, - _service: String, - }; - } - - setConfig(config) { - if (!config || !config.service) { - throw Error("Error in element configuration"); - } - - const [domain, service] = config.service.split(".", 2); - this.setProperties({ - _config: config, - _domain: domain, - _service: service, - }); - } -} -customElements.define("hui-service-button-element", HuiServiceButtonElement); diff --git a/src/panels/lovelace/elements/hui-service-button-element.ts b/src/panels/lovelace/elements/hui-service-button-element.ts new file mode 100644 index 0000000000..c87e3099de --- /dev/null +++ b/src/panels/lovelace/elements/hui-service-button-element.ts @@ -0,0 +1,74 @@ +import { html, LitElement } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; + +import "../../../components/buttons/ha-call-service-button.js"; + +import { LovelaceElement, LovelaceElementConfig } from "./types.js"; +import { HomeAssistant } from "../../../types.js"; + +export class HuiServiceButtonElement extends LitElement + implements LovelaceElement { + public hass?: HomeAssistant; + private _config?: LovelaceElementConfig; + private _domain?: string; + private _service?: string; + + static get properties() { + return { _config: {} }; + } + + public setConfig(config: LovelaceElementConfig): void { + if (!config || !config.service) { + throw Error("Invalid Configuration: 'service' required"); + } + + [this._domain, this._service] = config.service.split(".", 2); + + if (!this._domain) { + throw Error("Invalid Configuration: 'service' does not have a domain"); + } + + if (!this._service) { + throw Error( + "Invalid Configuration: 'service' does not have a service name" + ); + } + + this._config = config; + } + + protected render(): TemplateResult { + if (!this._config) { + return html``; + } + + return html` + ${this.renderStyle()} + ${this._config.title} + `; + } + + private renderStyle(): TemplateResult { + return html` + + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-service-button-element": HuiServiceButtonElement; + } +} + +customElements.define("hui-service-button-element", HuiServiceButtonElement);