diff --git a/src/components/ha-attributes.ts b/src/components/ha-attributes.ts index c0c14faca8..42f5cbead8 100644 --- a/src/components/ha-attributes.ts +++ b/src/components/ha-attributes.ts @@ -10,6 +10,9 @@ import { import { HassEntity } from "home-assistant-js-websocket"; import hassAttributeUtil from "../util/hass-attributes-util"; +import { until } from "lit-html/directives/until"; + +let jsYamlPromise: Promise; @customElement("ha-attributes") class HaAttributes extends LitElement { @@ -32,7 +35,7 @@ class HaAttributes extends LitElement {
${attribute.replace(/_/g, " ")}
- ${this.formatAttributeValue(attribute)} + ${this.formatAttribute(attribute)}
` @@ -63,6 +66,10 @@ class HaAttributes extends LitElement { color: var(--secondary-text-color); text-align: right; } + pre { + font-family: inherit; + font-size: inherit; + } `; } @@ -75,18 +82,31 @@ class HaAttributes extends LitElement { }); } - private formatAttributeValue(attribute: string): string { + private formatAttribute(attribute: string): string | TemplateResult { if (!this.stateObj) { return "-"; } const value = this.stateObj.attributes[attribute]; + return this.formatAttributeValue(value); + } + + private formatAttributeValue(value: any): string | TemplateResult { if (value === null) { return "-"; } - if (Array.isArray(value)) { - return value.join(", "); + if ( + (Array.isArray(value) && value.some((val) => val instanceof Object)) || + (!Array.isArray(value) && value instanceof Object) + ) { + if (!jsYamlPromise) { + jsYamlPromise = import(/* webpackChunkName: "js-yaml" */ "js-yaml"); + } + const yaml = jsYamlPromise.then((jsYaml) => jsYaml.safeDump(value)); + return html` +
${until(yaml, "")}
+ `; } - return value instanceof Object ? JSON.stringify(value, null, 2) : value; + return Array.isArray(value) ? value.join(", ") : value; } } diff --git a/src/panels/developer-tools/state/developer-tools-state.js b/src/panels/developer-tools/state/developer-tools-state.js index 9cc5fb6bc8..f1f56b963a 100644 --- a/src/panels/developer-tools/state/developer-tools-state.js +++ b/src/panels/developer-tools/state/developer-tools-state.js @@ -357,16 +357,22 @@ class HaPanelDevState extends EventsMixin(LocalizeMixin(PolymerElement)) { for (i = 0, keys = Object.keys(entity.attributes); i < keys.length; i++) { key = keys[i]; - value = entity.attributes[key]; - if (!Array.isArray(value) && value instanceof Object) { - value = JSON.stringify(value, null, " "); - } - output += key + ": " + value + "\n"; + value = this.formatAttributeValue(entity.attributes[key]); + output += `${key}: ${value}\n`; } - return output; } + formatAttributeValue(value) { + if ( + (Array.isArray(value) && value.some((val) => val instanceof Object)) || + (!Array.isArray(value) && value instanceof Object) + ) { + return `\n${safeDump(value)}`; + } + return Array.isArray(value) ? value.join(", ") : value; + } + _computeParsedStateAttributes(stateAttributes) { try { return stateAttributes.trim() ? safeLoad(stateAttributes) : {};