From 38bfe8c8def10d2daba331111c5fb55c4341e883 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Tue, 30 Oct 2018 05:15:12 -0500 Subject: [PATCH] Convert hui-call-service-row to TypeScript/LitElement (#1894) * Convert hui-call-service-row to TypeScript/LitElement * Update on _config change * Addressed review comments * Made `service_data` optional and verified that `callService` does use `entity` if it is available in the passed config. * Removed check in `entities-card` for `service-data` and will remove the full config check once other PRs have been applied to avoid merge conflicts * Will create a docs PR to update the docs * Addressed review comments * Removed entity config check in entities-card * Made `icon` optional. Default now `remote` * Made `action_name` optional. Default now 'Run' * `.icon` --- .../lovelace/cards/hui-entities-card.ts | 12 --- .../lovelace/common/create-row-element.js | 2 +- src/panels/lovelace/entity-rows/types.ts | 10 +- .../special-rows/hui-call-service-row.js | 80 ---------------- .../special-rows/hui-call-service-row.ts | 93 +++++++++++++++++++ 5 files changed, 103 insertions(+), 94 deletions(-) delete mode 100644 src/panels/lovelace/special-rows/hui-call-service-row.js create mode 100644 src/panels/lovelace/special-rows/hui-call-service-row.ts diff --git a/src/panels/lovelace/cards/hui-entities-card.ts b/src/panels/lovelace/cards/hui-entities-card.ts index ff064930f4..d8288198dd 100644 --- a/src/panels/lovelace/cards/hui-entities-card.ts +++ b/src/panels/lovelace/cards/hui-entities-card.ts @@ -73,18 +73,6 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement) public setConfig(config: Config): void { const entities = processConfigEntities(config.entities); - for (const entity of entities) { - if ( - entity.type === "call-service" && - (!entity.service || - !entity.name || - !entity.icon || - !entity.service_data || - !entity.action_name) - ) { - throw new Error("Missing required property when type is call-service"); - } - } this._config = { theme: "default", ...config }; this._configEntities = entities; diff --git a/src/panels/lovelace/common/create-row-element.js b/src/panels/lovelace/common/create-row-element.js index 9d196d96e1..74222bc258 100644 --- a/src/panels/lovelace/common/create-row-element.js +++ b/src/panels/lovelace/common/create-row-element.js @@ -14,7 +14,7 @@ import "../entity-rows/hui-text-entity-row.js"; import "../entity-rows/hui-timer-entity-row.js"; import "../entity-rows/hui-toggle-entity-row.js"; -import "../special-rows/hui-call-service-row.js"; +import "../special-rows/hui-call-service-row"; import "../special-rows/hui-divider-row"; import "../special-rows/hui-section-row"; import "../special-rows/hui-weblink-row"; diff --git a/src/panels/lovelace/entity-rows/types.ts b/src/panels/lovelace/entity-rows/types.ts index c4fcb7dc20..648b9cf9cb 100644 --- a/src/panels/lovelace/entity-rows/types.ts +++ b/src/panels/lovelace/entity-rows/types.ts @@ -16,11 +16,19 @@ export interface WeblinkConfig { icon?: string; url: string; } +export interface CallServiceConfig { + name: string; + icon?: string; + action_name?: string; + service: string; + service_data?: string; +} export type EntityRowConfig = | EntityConfig | DividerConfig | SectionConfig - | WeblinkConfig; + | WeblinkConfig + | CallServiceConfig; export interface EntityRow { hass?: HomeAssistant; diff --git a/src/panels/lovelace/special-rows/hui-call-service-row.js b/src/panels/lovelace/special-rows/hui-call-service-row.js deleted file mode 100644 index b9b96a51f9..0000000000 --- a/src/panels/lovelace/special-rows/hui-call-service-row.js +++ /dev/null @@ -1,80 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag.js"; -import { PolymerElement } from "@polymer/polymer/polymer-element.js"; -import "@polymer/paper-button/paper-button.js"; - -import "../../../components/ha-icon.js"; -import callService from "../common/call-service.js"; - -class HuiCallServiceRow extends PolymerElement { - static get template() { - return html` - ${this.styleTemplate} - -
-
- [[_config.name]] -
- [[_config.action_name]] -
- `; - } - - static get styleTemplate() { - return html` - - `; - } - - static get properties() { - return { - hass: Object, - _config: Object, - }; - } - - setConfig(config) { - if ( - !config || - !config.icon || - !config.name || - !config.action_name || - !config.service || - !config.service_data - ) { - throw new Error("Error in card configuration."); - } - this._config = config; - } - - _callService() { - callService(this._config, this.hass); - } -} -customElements.define("hui-call-service-row", HuiCallServiceRow); diff --git a/src/panels/lovelace/special-rows/hui-call-service-row.ts b/src/panels/lovelace/special-rows/hui-call-service-row.ts new file mode 100644 index 0000000000..1a06ad90ab --- /dev/null +++ b/src/panels/lovelace/special-rows/hui-call-service-row.ts @@ -0,0 +1,93 @@ +import { html, LitElement } from "@polymer/lit-element"; +import "@polymer/paper-button/paper-button.js"; + +import "../../../components/ha-icon.js"; + +import callService from "../common/call-service.js"; +import { EntityRow, CallServiceConfig } from "../entity-rows/types.js"; +import { HomeAssistant } from "../../../types.js"; +import { TemplateResult } from "lit-html"; + +class HuiCallServiceRow extends LitElement implements EntityRow { + public hass?: HomeAssistant; + private _config?: CallServiceConfig; + + static get properties() { + return { + hass: {}, + _config: {}, + }; + } + + public setConfig(config: CallServiceConfig): void { + if (!config || !config.name || !config.service) { + throw new Error("Error in card configuration."); + } + + this._config = { icon: "hass:remote", action_name: "Run", ...config }; + } + + protected render(): TemplateResult { + if (!this._config) { + return html``; + } + + return html` + ${this.renderStyle()} + +
+
+ ${this._config.name} +
+ ${this._config.action_name} +
+ `; + } + + private renderStyle(): TemplateResult { + return html` + + `; + } + + private _callService() { + callService(this._config, this.hass); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-call-service-row": HuiCallServiceRow; + } +} + +customElements.define("hui-call-service-row", HuiCallServiceRow);