From aeabf980282b4aa631d080156cedbcf26367990e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 10 Apr 2020 09:14:50 -0700 Subject: [PATCH 1/2] Add entity row that displays text --- .../create-element/create-row-element.ts | 1 + src/panels/lovelace/entity-rows/types.ts | 9 ++- .../lovelace/special-rows/hui-text-row.ts | 72 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/panels/lovelace/special-rows/hui-text-row.ts diff --git a/src/panels/lovelace/create-element/create-row-element.ts b/src/panels/lovelace/create-element/create-row-element.ts index 8fd0e6019e3b..2769955b070d 100644 --- a/src/panels/lovelace/create-element/create-row-element.ts +++ b/src/panels/lovelace/create-element/create-row-element.ts @@ -41,6 +41,7 @@ const LAZY_LOAD_TYPES = { cast: () => import("../special-rows/hui-cast-row"), buttons: () => import("../special-rows/hui-buttons-row"), attribute: () => import("../special-rows/hui-attribute-row"), + text: () => import("../special-rows/hui-text-row"), }; const DOMAIN_TO_ELEMENT_TYPE = { _domain_not_found: "text", diff --git a/src/panels/lovelace/entity-rows/types.ts b/src/panels/lovelace/entity-rows/types.ts index 29b133c33600..ade21c6c03e1 100644 --- a/src/panels/lovelace/entity-rows/types.ts +++ b/src/panels/lovelace/entity-rows/types.ts @@ -29,6 +29,12 @@ export interface WeblinkConfig { icon?: string; url: string; } +export interface TextConfig { + type: "text"; + name: string; + icon?: string; + text: string; +} export interface CallServiceConfig extends EntityConfig { type: "call-service"; service: string; @@ -65,7 +71,8 @@ export type LovelaceRowConfig = | ButtonRowConfig | ButtonsRowConfig | ConditionalRowConfig - | AttributeRowConfig; + | AttributeRowConfig + | TextConfig; export interface LovelaceRow extends HTMLElement { hass?: HomeAssistant; diff --git a/src/panels/lovelace/special-rows/hui-text-row.ts b/src/panels/lovelace/special-rows/hui-text-row.ts new file mode 100644 index 000000000000..36eb0bbba193 --- /dev/null +++ b/src/panels/lovelace/special-rows/hui-text-row.ts @@ -0,0 +1,72 @@ +import { + html, + LitElement, + TemplateResult, + customElement, + property, + css, + CSSResult, +} from "lit-element"; + +import { LovelaceRow, TextConfig } from "../entity-rows/types"; +import { HomeAssistant } from "../../../types"; + +import "../../../components/ha-icon"; + +@customElement("hui-text-row") +class HuiTextRow extends LitElement implements LovelaceRow { + public hass?: HomeAssistant; + + @property() private _config?: TextConfig; + + public setConfig(config: TextConfig): void { + if (!config || !config.name || !config.text) { + throw new Error("Invalid Configuration: 'name' and 'url' required"); + } + + this._config = config; + } + + protected render(): TemplateResult { + if (!this._config) { + return html``; + } + + return html` + +
${this._config.name}
+
${this._config.text}
+ `; + } + + static get styles(): CSSResult { + return css` + :host { + display: flex; + align-items: center; + } + ha-icon { + padding: 8px; + color: var(--paper-item-icon-color); + } + div { + flex: 1; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } + .name { + margin-left: 16px; + } + .text { + text-align: right; + } + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-text-row": HuiTextRow; + } +} From 4645cc297c83af95013c7a90a96ed33ca6e4e00e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 10 Apr 2020 09:19:50 -0700 Subject: [PATCH 2/2] Remove hass type --- src/panels/lovelace/special-rows/hui-text-row.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/panels/lovelace/special-rows/hui-text-row.ts b/src/panels/lovelace/special-rows/hui-text-row.ts index 36eb0bbba193..fac5b08046df 100644 --- a/src/panels/lovelace/special-rows/hui-text-row.ts +++ b/src/panels/lovelace/special-rows/hui-text-row.ts @@ -9,19 +9,16 @@ import { } from "lit-element"; import { LovelaceRow, TextConfig } from "../entity-rows/types"; -import { HomeAssistant } from "../../../types"; import "../../../components/ha-icon"; @customElement("hui-text-row") class HuiTextRow extends LitElement implements LovelaceRow { - public hass?: HomeAssistant; - @property() private _config?: TextConfig; public setConfig(config: TextConfig): void { if (!config || !config.name || !config.text) { - throw new Error("Invalid Configuration: 'name' and 'url' required"); + throw new Error("Invalid Configuration: 'name' and 'text' required"); } this._config = config;