import type { HassEntity } from "home-assistant-js-websocket"; import type { CSSResultGroup, PropertyValues } from "lit"; import { css, html, LitElement, nothing } from "lit"; import { customElement, property, state } from "lit/decorators"; import { computeAttributeNameDisplay } from "../common/entity/compute_attribute_display"; import { STATE_ATTRIBUTES } from "../data/entity_attributes"; import { haStyle } from "../resources/styles"; import type { HomeAssistant } from "../types"; import "./ha-attribute-value"; import "./ha-expansion-panel"; @customElement("ha-attributes") class HaAttributes extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public stateObj?: HassEntity; @property({ attribute: "extra-filters" }) public extraFilters?: string; @state() private _expanded = false; private get _filteredAttributes() { return this._computeDisplayAttributes( STATE_ATTRIBUTES.concat( this.extraFilters ? this.extraFilters.split(",") : [] ) ); } protected willUpdate(changedProperties: PropertyValues): void { if ( changedProperties.has("extraFilters") || changedProperties.has("stateObj") ) { this.toggleAttribute("empty", this._filteredAttributes.length === 0); } } protected render() { if (!this.stateObj) { return nothing; } const attributes = this._filteredAttributes; if (attributes.length === 0) { return nothing; } return html`
${this._expanded ? html` ${attributes.map( (attribute) => html`
${computeAttributeNameDisplay( this.hass.localize, this.stateObj!, this.hass.entities, attribute )}
` )} ` : ""}
${this.stateObj.attributes.attribution ? html`
${this.stateObj.attributes.attribution}
` : ""} `; } static get styles(): CSSResultGroup { return [ haStyle, css` .attribute-container { margin-bottom: 8px; direction: ltr; } .data-entry { display: flex; flex-direction: row; justify-content: space-between; } .data-entry .value { max-width: 60%; overflow-wrap: break-word; text-align: right; } .key { flex-grow: 1; } .attribution { color: var(--secondary-text-color); text-align: center; margin-top: 16px; } hr { border-color: var(--divider-color); border-bottom: none; margin: 16px 0; } `, ]; } private _computeDisplayAttributes(filtersArray: string[]): string[] { if (!this.stateObj) { return []; } return Object.keys(this.stateObj.attributes).filter( (key) => filtersArray.indexOf(key) === -1 ); } private _expandedChanged(ev) { this._expanded = ev.detail.expanded; } } declare global { interface HTMLElementTagNameMap { "ha-attributes": HaAttributes; } }