diff --git a/hassio/src/addon-view/hassio-addon-config.ts b/hassio/src/addon-view/hassio-addon-config.ts index 5d36267a81..95f821cfea 100644 --- a/hassio/src/addon-view/hassio-addon-config.ts +++ b/hassio/src/addon-view/hassio-addon-config.ts @@ -10,6 +10,7 @@ import { property, PropertyValues, TemplateResult, + query, } from "lit-element"; import { HomeAssistant } from "../../../src/types"; @@ -20,30 +21,41 @@ import { } from "../../../src/data/hassio/addon"; import { hassioStyle } from "../resources/hassio-style"; import { haStyle } from "../../../src/resources/styles"; -import { PolymerChangedEvent } from "../../../src/polymer-types"; import { fireEvent } from "../../../src/common/dom/fire_event"; +import "../../../src/components/ha-yaml-editor"; +// tslint:disable-next-line: no-duplicate-imports +import { HaYamlEditor } from "../../../src/components/ha-yaml-editor"; @customElement("hassio-addon-config") class HassioAddonConfig extends LitElement { @property() public hass!: HomeAssistant; @property() public addon!: HassioAddonDetails; @property() private _error?: string; - @property() private _config!: string; @property({ type: Boolean }) private _configHasChanged = false; + @query("ha-yaml-editor") private _editor!: HaYamlEditor; + protected render(): TemplateResult { + const editor = this._editor; + // If editor not rendered, don't show the error. + const valid = editor ? editor.isValid : true; + return html`
+ ${this._error ? html`
${this._error}
` : ""} - + ${valid + ? "" + : html` +
Invalid YAML
+ `}
@@ -51,7 +63,7 @@ class HassioAddonConfig extends LitElement { Save @@ -77,7 +89,7 @@ class HassioAddonConfig extends LitElement { } .errors { color: var(--google-red-500); - margin-bottom: 16px; + margin-top: 16px; } iron-autogrow-textarea { width: 100%; @@ -93,15 +105,13 @@ class HassioAddonConfig extends LitElement { protected updated(changedProperties: PropertyValues): void { super.updated(changedProperties); if (changedProperties.has("addon")) { - this._config = JSON.stringify(this.addon.options, null, 2); + this._editor.setValue(this.addon.options); } } - private _configChanged(ev: PolymerChangedEvent): void { - this._config = - ev.detail.value || JSON.stringify(this.addon.options, null, 2); - this._configHasChanged = - this._config !== JSON.stringify(this.addon.options, null, 2); + private _configChanged(): void { + this._configHasChanged = true; + this.requestUpdate(); } private async _resetTapped(): Promise { @@ -129,7 +139,7 @@ class HassioAddonConfig extends LitElement { this._error = undefined; try { data = { - options: JSON.parse(this._config), + options: this._editor.value, }; } catch (err) { this._error = err; diff --git a/src/components/ha-yaml-editor.ts b/src/components/ha-yaml-editor.ts index a07297b365..a637a28cc3 100644 --- a/src/components/ha-yaml-editor.ts +++ b/src/components/ha-yaml-editor.ts @@ -21,9 +21,10 @@ const isEmpty = (obj: object) => { @customElement("ha-yaml-editor") export class HaYamlEditor extends LitElement { @property() public value?: any; + @property() public defaultValue?: any; @property() public isValid = true; @property() public label?: string; - @property() private _yaml?: string; + @property() private _yaml: string = ""; @query("ha-code-editor") private _editor?: HaCodeEditor; public setValue(value) { @@ -40,7 +41,9 @@ export class HaYamlEditor extends LitElement { } protected firstUpdated() { - this.setValue(this.value); + if (this.defaultValue) { + this.setValue(this.defaultValue); + } } protected render() { @@ -71,7 +74,6 @@ export class HaYamlEditor extends LitElement { if (value) { try { parsed = safeLoad(value); - isValid = true; } catch (err) { // Invalid YAML isValid = false; @@ -83,9 +85,7 @@ export class HaYamlEditor extends LitElement { this.value = parsed; this.isValid = isValid; - if (isValid) { - fireEvent(this, "value-changed", { value: parsed }); - } + fireEvent(this, "value-changed", { value: parsed, isValid } as any); } } diff --git a/src/panels/config/automation/action/ha-automation-action-row.ts b/src/panels/config/automation/action/ha-automation-action-row.ts index f308d6d84a..43f756ae84 100644 --- a/src/panels/config/automation/action/ha-automation-action-row.ts +++ b/src/panels/config/automation/action/ha-automation-action-row.ts @@ -157,7 +157,7 @@ export default class HaAutomationActionRow extends LitElement { ` : ""}
@@ -238,6 +238,9 @@ export default class HaAutomationActionRow extends LitElement { private _onYamlChange(ev: CustomEvent) { ev.stopPropagation(); + if (!ev.detail.isValid) { + return; + } fireEvent(this, "value-changed", { value: ev.detail.value }); } diff --git a/src/panels/config/automation/action/types/ha-automation-action-event.ts b/src/panels/config/automation/action/types/ha-automation-action-event.ts index e66fa62e8b..115b11680b 100644 --- a/src/panels/config/automation/action/types/ha-automation-action-event.ts +++ b/src/panels/config/automation/action/types/ha-automation-action-event.ts @@ -57,13 +57,17 @@ export class HaEventAction extends LitElement implements ActionElement { "ui.panel.config.automation.editor.actions.type.event.service_data" )} .name=${"event_data"} - .value=${event_data} + .defaultValue=${event_data} @value-changed=${this._dataChanged} > `; } private _dataChanged(ev: CustomEvent): void { + ev.stopPropagation(); + if (!ev.detail.isValid) { + return; + } this._actionData = ev.detail.value; handleChangeEvent(this, ev); } diff --git a/src/panels/config/automation/action/types/ha-automation-action-service.ts b/src/panels/config/automation/action/types/ha-automation-action-service.ts index cd59512da6..a0ac5a6ebe 100644 --- a/src/panels/config/automation/action/types/ha-automation-action-service.ts +++ b/src/panels/config/automation/action/types/ha-automation-action-service.ts @@ -94,13 +94,17 @@ export class HaServiceAction extends LitElement implements ActionElement { "ui.panel.config.automation.editor.actions.type.service.service_data" )} .name=${"data"} - .value=${data} + .defaultValue=${data} @value-changed=${this._dataChanged} > `; } private _dataChanged(ev: CustomEvent): void { + ev.stopPropagation(); + if (!ev.detail.isValid) { + return; + } this._actionData = ev.detail.value; handleChangeEvent(this, ev); } diff --git a/src/panels/config/automation/condition/ha-automation-condition-editor.ts b/src/panels/config/automation/condition/ha-automation-condition-editor.ts index 57a9726bc3..4082b68265 100644 --- a/src/panels/config/automation/condition/ha-automation-condition-editor.ts +++ b/src/panels/config/automation/condition/ha-automation-condition-editor.ts @@ -54,7 +54,7 @@ export default class HaAutomationConditionEditor extends LitElement { ` : ""} @@ -114,6 +114,9 @@ export default class HaAutomationConditionEditor extends LitElement { private _onYamlChange(ev: CustomEvent) { ev.stopPropagation(); + if (!ev.detail.isValid) { + return; + } fireEvent(this, "value-changed", { value: ev.detail.value }); } } diff --git a/src/panels/config/automation/trigger/ha-automation-trigger-row.ts b/src/panels/config/automation/trigger/ha-automation-trigger-row.ts index cb5f6bc2a9..3559ca2053 100644 --- a/src/panels/config/automation/trigger/ha-automation-trigger-row.ts +++ b/src/panels/config/automation/trigger/ha-automation-trigger-row.ts @@ -139,7 +139,7 @@ export default class HaAutomationTriggerRow extends LitElement { ` : ""} @@ -213,6 +213,9 @@ export default class HaAutomationTriggerRow extends LitElement { private _onYamlChange(ev: CustomEvent) { ev.stopPropagation(); + if (!ev.detail.isValid) { + return; + } fireEvent(this, "value-changed", { value: ev.detail.value }); } diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts index 7a70ba055a..ef163d50d2 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-event.ts @@ -35,13 +35,17 @@ export class HaEventTrigger extends LitElement implements TriggerElement { "ui.panel.config.automation.editor.triggers.type.event.event_data" )} .name=${"event_data"} - .value=${event_data} + .defaultValue=${event_data} @value-changed=${this._valueChanged} > `; } private _valueChanged(ev: CustomEvent): void { + ev.stopPropagation(); + if (!ev.detail.isValid) { + return; + } handleChangeEvent(this, ev); } } diff --git a/src/panels/lovelace/editor/card-editor/hui-dialog-suggest-card.ts b/src/panels/lovelace/editor/card-editor/hui-dialog-suggest-card.ts index a81d54ceb3..bc19be2282 100755 --- a/src/panels/lovelace/editor/card-editor/hui-dialog-suggest-card.ts +++ b/src/panels/lovelace/editor/card-editor/hui-dialog-suggest-card.ts @@ -84,7 +84,9 @@ export class HuiDialogSuggestCard extends LitElement { ${this._yamlMode && this._cardConfig ? html`
- +
` : ""}