Allow yaml in dev states (#3859)

* Allow yaml in dev states

* remove json
This commit is contained in:
Bram Kragten 2019-10-02 21:25:38 +02:00 committed by GitHub
parent 4cf9472bf4
commit a02bf1fd48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,8 @@ import "@polymer/paper-input/paper-textarea";
import { html } from "@polymer/polymer/lib/utils/html-tag"; import { html } from "@polymer/polymer/lib/utils/html-tag";
import { PolymerElement } from "@polymer/polymer/polymer-element"; import { PolymerElement } from "@polymer/polymer/polymer-element";
import yaml from "js-yaml";
import "../../../components/entity/ha-entity-picker"; import "../../../components/entity/ha-entity-picker";
import "../../../resources/ha-style"; import "../../../resources/ha-style";
import { EventsMixin } from "../../../mixins/events-mixin"; import { EventsMixin } from "../../../mixins/events-mixin";
@ -88,7 +90,7 @@ class HaPanelDevState extends EventsMixin(PolymerElement) {
class="state-input" class="state-input"
></paper-input> ></paper-input>
<paper-textarea <paper-textarea
label="State attributes (JSON, optional)" label="State attributes (YAML, optional)"
autocapitalize="none" autocapitalize="none"
autocomplete="off" autocomplete="off"
spellcheck="false" spellcheck="false"
@ -211,14 +213,14 @@ class HaPanelDevState extends EventsMixin(PolymerElement) {
var state = ev.model.entity; var state = ev.model.entity;
this._entityId = state.entity_id; this._entityId = state.entity_id;
this._state = state.state; this._state = state.state;
this._stateAttributes = JSON.stringify(state.attributes, null, " "); this._stateAttributes = yaml.safeDump(state.attributes);
ev.preventDefault(); ev.preventDefault();
} }
entityIdChanged() { entityIdChanged() {
var state = this.hass.states[this._entityId]; var state = this.hass.states[this._entityId];
this._state = state.state; this._state = state.state;
this._stateAttributes = JSON.stringify(state.attributes, null, " "); this._stateAttributes = yaml.safeDump(state.attributes);
} }
entityMoreInfo(ev) { entityMoreInfo(ev) {
@ -228,12 +230,12 @@ class HaPanelDevState extends EventsMixin(PolymerElement) {
handleSetState() { handleSetState() {
var attr; var attr;
var attrRaw = this._stateAttributes.replace(/^\s+|\s+$/g, "");
try { try {
attr = attrRaw ? JSON.parse(attrRaw) : {}; attr = this._stateAttributes ? yaml.safeLoad(this._stateAttributes) : {};
} catch (err) { } catch (err) {
/* eslint-disable no-alert */ /* eslint-disable no-alert */
alert("Error parsing JSON: " + err); alert("Error parsing YAML: " + err);
/* eslint-enable no-alert */ /* eslint-enable no-alert */
return; return;
} }