diff --git a/src/components/ha-attributes.js b/src/components/ha-attributes.js deleted file mode 100644 index 1a1836c2f2..0000000000 --- a/src/components/ha-attributes.js +++ /dev/null @@ -1,91 +0,0 @@ -import "@polymer/iron-flex-layout/iron-flex-layout-classes"; -import { html } from "@polymer/polymer/lib/utils/html-tag"; -import { PolymerElement } from "@polymer/polymer/polymer-element"; - -import hassAttributeUtil from "../util/hass-attributes-util"; - -class HaAttributes extends PolymerElement { - static get template() { - return html` - - - -
- -
- [[computeAttribution(stateObj)]] -
-
- `; - } - - static get properties() { - return { - stateObj: { - type: Object, - }, - extraFilters: { - type: String, - value: "", - }, - filtersArray: { - type: Array, - computed: "computeFiltersArray(extraFilters)", - }, - }; - } - - computeFiltersArray(extraFilters) { - return Object.keys(hassAttributeUtil.LOGIC_STATE_ATTRIBUTES).concat( - extraFilters ? extraFilters.split(",") : [] - ); - } - - computeDisplayAttributes(stateObj, filtersArray) { - if (!stateObj) { - return []; - } - return Object.keys(stateObj.attributes).filter(function(key) { - return filtersArray.indexOf(key) === -1; - }); - } - - formatAttribute(attribute) { - return attribute.replace(/_/g, " "); - } - - formatAttributeValue(stateObj, attribute) { - var value = stateObj.attributes[attribute]; - if (value === null) return "-"; - if (Array.isArray(value)) { - return value.join(", "); - } - return value instanceof Object ? JSON.stringify(value, null, 2) : value; - } - - computeAttribution(stateObj) { - return stateObj.attributes.attribution; - } -} - -customElements.define("ha-attributes", HaAttributes); diff --git a/src/components/ha-attributes.ts b/src/components/ha-attributes.ts new file mode 100644 index 0000000000..d2eab599fe --- /dev/null +++ b/src/components/ha-attributes.ts @@ -0,0 +1,97 @@ +import { + property, + LitElement, + TemplateResult, + html, + CSSResult, + css, + customElement, +} from "lit-element"; +import { HassEntity } from "home-assistant-js-websocket"; + +import hassAttributeUtil from "../util/hass-attributes-util"; + +@customElement("ha-attributes") +class HaAttributes extends LitElement { + @property() public stateObj?: HassEntity; + @property() public extraFilters?: string; + + protected render(): TemplateResult | void { + if (!this.stateObj) { + return html``; + } + + return html` +
+ ${this.computeDisplayAttributes( + Object.keys(hassAttributeUtil.LOGIC_STATE_ATTRIBUTES).concat( + this.extraFilters ? this.extraFilters.split(",") : [] + ) + ).map( + (attribute) => html` +
+
${attribute.replace(/_/g, " ")}
+
+ ${this.formatAttributeValue(attribute)} +
+
+ ` + )} + ${this.stateObj.attributes.attribution + ? html` +
+ ${this.stateObj.attributes.attribution} +
+ ` + : ""} +
+ `; + } + + static get styles(): CSSResult { + return css` + .data-entry { + display: flex; + flex-direction: row; + justify-content: space-between; + } + .data-entry .value { + max-width: 200px; + overflow-wrap: break-word; + } + .attribution { + color: var(--secondary-text-color); + text-align: right; + } + `; + } + + private computeDisplayAttributes(filtersArray: string[]): string[] { + if (!this.stateObj) { + return []; + } + return Object.keys(this.stateObj.attributes).filter((key) => { + return filtersArray.indexOf(key) === -1; + }); + } + + private formatAttributeValue(attribute: string): string { + if (!this.stateObj) { + return "-"; + } + const value = this.stateObj.attributes[attribute]; + if (value === null) { + return "-"; + } + if (Array.isArray(value)) { + return value.join(", "); + } + return value instanceof Object ? JSON.stringify(value, null, 2) : value; + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-attributes": HaAttributes; + } +}