Convert Hass.io add-on options to YAML (#4717)

* Convert Hass.io options to YAML

* Fix yaml editors on other places

* Update ha-automation-action-service.ts

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Paulus Schoutsen 2020-02-03 02:06:47 -08:00 committed by GitHub
parent ad676d7fd3
commit 24c591fbf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 61 additions and 28 deletions

View File

@ -10,6 +10,7 @@ import {
property, property,
PropertyValues, PropertyValues,
TemplateResult, TemplateResult,
query,
} from "lit-element"; } from "lit-element";
import { HomeAssistant } from "../../../src/types"; import { HomeAssistant } from "../../../src/types";
@ -20,30 +21,41 @@ import {
} from "../../../src/data/hassio/addon"; } from "../../../src/data/hassio/addon";
import { hassioStyle } from "../resources/hassio-style"; import { hassioStyle } from "../resources/hassio-style";
import { haStyle } from "../../../src/resources/styles"; import { haStyle } from "../../../src/resources/styles";
import { PolymerChangedEvent } from "../../../src/polymer-types";
import { fireEvent } from "../../../src/common/dom/fire_event"; 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") @customElement("hassio-addon-config")
class HassioAddonConfig extends LitElement { class HassioAddonConfig extends LitElement {
@property() public hass!: HomeAssistant; @property() public hass!: HomeAssistant;
@property() public addon!: HassioAddonDetails; @property() public addon!: HassioAddonDetails;
@property() private _error?: string; @property() private _error?: string;
@property() private _config!: string;
@property({ type: Boolean }) private _configHasChanged = false; @property({ type: Boolean }) private _configHasChanged = false;
@query("ha-yaml-editor") private _editor!: HaYamlEditor;
protected render(): TemplateResult { protected render(): TemplateResult {
const editor = this._editor;
// If editor not rendered, don't show the error.
const valid = editor ? editor.isValid : true;
return html` return html`
<paper-card heading="Config"> <paper-card heading="Config">
<div class="card-content"> <div class="card-content">
<ha-yaml-editor
@value-changed=${this._configChanged}
></ha-yaml-editor>
${this._error ${this._error
? html` ? html`
<div class="errors">${this._error}</div> <div class="errors">${this._error}</div>
` `
: ""} : ""}
<iron-autogrow-textarea ${valid
@value-changed=${this._configChanged} ? ""
.value=${this._config} : html`
></iron-autogrow-textarea> <div class="errors">Invalid YAML</div>
`}
</div> </div>
<div class="card-actions"> <div class="card-actions">
<mwc-button class="warning" @click=${this._resetTapped}> <mwc-button class="warning" @click=${this._resetTapped}>
@ -51,7 +63,7 @@ class HassioAddonConfig extends LitElement {
</mwc-button> </mwc-button>
<mwc-button <mwc-button
@click=${this._saveTapped} @click=${this._saveTapped}
.disabled=${!this._configHasChanged} .disabled=${!this._configHasChanged || !valid}
> >
Save Save
</mwc-button> </mwc-button>
@ -77,7 +89,7 @@ class HassioAddonConfig extends LitElement {
} }
.errors { .errors {
color: var(--google-red-500); color: var(--google-red-500);
margin-bottom: 16px; margin-top: 16px;
} }
iron-autogrow-textarea { iron-autogrow-textarea {
width: 100%; width: 100%;
@ -93,15 +105,13 @@ class HassioAddonConfig extends LitElement {
protected updated(changedProperties: PropertyValues): void { protected updated(changedProperties: PropertyValues): void {
super.updated(changedProperties); super.updated(changedProperties);
if (changedProperties.has("addon")) { if (changedProperties.has("addon")) {
this._config = JSON.stringify(this.addon.options, null, 2); this._editor.setValue(this.addon.options);
} }
} }
private _configChanged(ev: PolymerChangedEvent<string>): void { private _configChanged(): void {
this._config = this._configHasChanged = true;
ev.detail.value || JSON.stringify(this.addon.options, null, 2); this.requestUpdate();
this._configHasChanged =
this._config !== JSON.stringify(this.addon.options, null, 2);
} }
private async _resetTapped(): Promise<void> { private async _resetTapped(): Promise<void> {
@ -129,7 +139,7 @@ class HassioAddonConfig extends LitElement {
this._error = undefined; this._error = undefined;
try { try {
data = { data = {
options: JSON.parse(this._config), options: this._editor.value,
}; };
} catch (err) { } catch (err) {
this._error = err; this._error = err;

View File

@ -21,9 +21,10 @@ const isEmpty = (obj: object) => {
@customElement("ha-yaml-editor") @customElement("ha-yaml-editor")
export class HaYamlEditor extends LitElement { export class HaYamlEditor extends LitElement {
@property() public value?: any; @property() public value?: any;
@property() public defaultValue?: any;
@property() public isValid = true; @property() public isValid = true;
@property() public label?: string; @property() public label?: string;
@property() private _yaml?: string; @property() private _yaml: string = "";
@query("ha-code-editor") private _editor?: HaCodeEditor; @query("ha-code-editor") private _editor?: HaCodeEditor;
public setValue(value) { public setValue(value) {
@ -40,7 +41,9 @@ export class HaYamlEditor extends LitElement {
} }
protected firstUpdated() { protected firstUpdated() {
this.setValue(this.value); if (this.defaultValue) {
this.setValue(this.defaultValue);
}
} }
protected render() { protected render() {
@ -71,7 +74,6 @@ export class HaYamlEditor extends LitElement {
if (value) { if (value) {
try { try {
parsed = safeLoad(value); parsed = safeLoad(value);
isValid = true;
} catch (err) { } catch (err) {
// Invalid YAML // Invalid YAML
isValid = false; isValid = false;
@ -83,9 +85,7 @@ export class HaYamlEditor extends LitElement {
this.value = parsed; this.value = parsed;
this.isValid = isValid; this.isValid = isValid;
if (isValid) { fireEvent(this, "value-changed", { value: parsed, isValid } as any);
fireEvent(this, "value-changed", { value: parsed });
}
} }
} }

View File

@ -157,7 +157,7 @@ export default class HaAutomationActionRow extends LitElement {
` `
: ""} : ""}
<ha-yaml-editor <ha-yaml-editor
.value=${this.action} .defaultValue=${this.action}
@value-changed=${this._onYamlChange} @value-changed=${this._onYamlChange}
></ha-yaml-editor> ></ha-yaml-editor>
</div> </div>
@ -238,6 +238,9 @@ export default class HaAutomationActionRow extends LitElement {
private _onYamlChange(ev: CustomEvent) { private _onYamlChange(ev: CustomEvent) {
ev.stopPropagation(); ev.stopPropagation();
if (!ev.detail.isValid) {
return;
}
fireEvent(this, "value-changed", { value: ev.detail.value }); fireEvent(this, "value-changed", { value: ev.detail.value });
} }

View File

@ -57,13 +57,17 @@ export class HaEventAction extends LitElement implements ActionElement {
"ui.panel.config.automation.editor.actions.type.event.service_data" "ui.panel.config.automation.editor.actions.type.event.service_data"
)} )}
.name=${"event_data"} .name=${"event_data"}
.value=${event_data} .defaultValue=${event_data}
@value-changed=${this._dataChanged} @value-changed=${this._dataChanged}
></ha-yaml-editor> ></ha-yaml-editor>
`; `;
} }
private _dataChanged(ev: CustomEvent): void { private _dataChanged(ev: CustomEvent): void {
ev.stopPropagation();
if (!ev.detail.isValid) {
return;
}
this._actionData = ev.detail.value; this._actionData = ev.detail.value;
handleChangeEvent(this, ev); handleChangeEvent(this, ev);
} }

View File

@ -94,13 +94,17 @@ export class HaServiceAction extends LitElement implements ActionElement {
"ui.panel.config.automation.editor.actions.type.service.service_data" "ui.panel.config.automation.editor.actions.type.service.service_data"
)} )}
.name=${"data"} .name=${"data"}
.value=${data} .defaultValue=${data}
@value-changed=${this._dataChanged} @value-changed=${this._dataChanged}
></ha-yaml-editor> ></ha-yaml-editor>
`; `;
} }
private _dataChanged(ev: CustomEvent): void { private _dataChanged(ev: CustomEvent): void {
ev.stopPropagation();
if (!ev.detail.isValid) {
return;
}
this._actionData = ev.detail.value; this._actionData = ev.detail.value;
handleChangeEvent(this, ev); handleChangeEvent(this, ev);
} }

View File

@ -54,7 +54,7 @@ export default class HaAutomationConditionEditor extends LitElement {
` `
: ""} : ""}
<ha-yaml-editor <ha-yaml-editor
.value=${this.condition} .defaultValue=${this.condition}
@value-changed=${this._onYamlChange} @value-changed=${this._onYamlChange}
></ha-yaml-editor> ></ha-yaml-editor>
</div> </div>
@ -114,6 +114,9 @@ export default class HaAutomationConditionEditor extends LitElement {
private _onYamlChange(ev: CustomEvent) { private _onYamlChange(ev: CustomEvent) {
ev.stopPropagation(); ev.stopPropagation();
if (!ev.detail.isValid) {
return;
}
fireEvent(this, "value-changed", { value: ev.detail.value }); fireEvent(this, "value-changed", { value: ev.detail.value });
} }
} }

View File

@ -139,7 +139,7 @@ export default class HaAutomationTriggerRow extends LitElement {
` `
: ""} : ""}
<ha-yaml-editor <ha-yaml-editor
.value=${this.trigger} .defaultValue=${this.trigger}
@value-changed=${this._onYamlChange} @value-changed=${this._onYamlChange}
></ha-yaml-editor> ></ha-yaml-editor>
</div> </div>
@ -213,6 +213,9 @@ export default class HaAutomationTriggerRow extends LitElement {
private _onYamlChange(ev: CustomEvent) { private _onYamlChange(ev: CustomEvent) {
ev.stopPropagation(); ev.stopPropagation();
if (!ev.detail.isValid) {
return;
}
fireEvent(this, "value-changed", { value: ev.detail.value }); fireEvent(this, "value-changed", { value: ev.detail.value });
} }

View File

@ -35,13 +35,17 @@ export class HaEventTrigger extends LitElement implements TriggerElement {
"ui.panel.config.automation.editor.triggers.type.event.event_data" "ui.panel.config.automation.editor.triggers.type.event.event_data"
)} )}
.name=${"event_data"} .name=${"event_data"}
.value=${event_data} .defaultValue=${event_data}
@value-changed=${this._valueChanged} @value-changed=${this._valueChanged}
></ha-yaml-editor> ></ha-yaml-editor>
`; `;
} }
private _valueChanged(ev: CustomEvent): void { private _valueChanged(ev: CustomEvent): void {
ev.stopPropagation();
if (!ev.detail.isValid) {
return;
}
handleChangeEvent(this, ev); handleChangeEvent(this, ev);
} }
} }

View File

@ -84,7 +84,9 @@ export class HuiDialogSuggestCard extends LitElement {
${this._yamlMode && this._cardConfig ${this._yamlMode && this._cardConfig
? html` ? html`
<div class="editor"> <div class="editor">
<ha-yaml-editor .value=${this._cardConfig}></ha-yaml-editor> <ha-yaml-editor
.defaultValue=${this._cardConfig}
></ha-yaml-editor>
</div> </div>
` `
: ""} : ""}