Fix script config form (#13878)

This commit is contained in:
Paul Bottein 2022-09-27 15:40:38 +02:00 committed by GitHub
parent 8d5c36a96a
commit 0848c096b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -332,7 +332,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
"yaml-mode": this._mode === "yaml", "yaml-mode": this._mode === "yaml",
})}" })}"
> >
${this._errors ? html`<div class="errors">${this._errors}</div>` : ""}
${this._mode === "gui" ${this._mode === "gui"
? html` ? html`
<div <div
@ -343,6 +342,13 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
${this._config ${this._config
? html` ? html`
<div class="config-container"> <div class="config-container">
${this._errors
? html`
<ha-alert alert-type="error">
${this._errors}
</ha-alert>
`
: ""}
<ha-card outlined> <ha-card outlined>
<div class="card-content"> <div class="card-content">
<ha-form <ha-form
@ -382,6 +388,11 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
` `
: this._mode === "yaml" : this._mode === "yaml"
? html` ? html`
${this._errors
? html`
<ha-alert alert-type="error">${this._errors}</ha-alert>
`
: ""}
<ha-yaml-editor <ha-yaml-editor
.hass=${this.hass} .hass=${this.hass}
.defaultValue=${this._preprocessYaml()} .defaultValue=${this._preprocessYaml()}
@ -546,28 +557,7 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
}); });
} }
private _modeChanged(mode) { private _computeEntityIdFromAlias(alias: string) {
const curMode = this._config!.mode || MODES[0];
if (mode === curMode) {
return;
}
this._config = { ...this._config!, mode };
if (!isMaxMode(mode)) {
delete this._config.max;
}
this._dirty = true;
}
private _aliasChanged(alias: string) {
if (
this.scriptEntityId ||
(this._entityId && this._entityId !== slugify(this._config!.alias))
) {
return;
}
const aliasSlugify = slugify(alias); const aliasSlugify = slugify(alias);
let id = aliasSlugify; let id = aliasSlugify;
let i = 2; let i = 2;
@ -575,11 +565,10 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
id = `${aliasSlugify}_${i}`; id = `${aliasSlugify}_${i}`;
i++; i++;
} }
return id;
this._entityId = id;
} }
private _idChanged(id: string) { private _setEntityId(id?: string) {
this._entityId = id; this._entityId = id;
if (this.hass.states[`script.${this._entityId}`]) { if (this.hass.states[`script.${this._entityId}`]) {
this._idError = true; this._idError = true;
@ -588,47 +577,60 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
} }
} }
private updateEntityId(
newId: string | undefined,
newAlias: string | undefined
) {
const currentAlias = this._config?.alias ?? "";
const currentEntityId = this._entityId ?? "";
if (newId !== this._entityId) {
this._setEntityId(newId || undefined);
return;
}
const currentComputedEntity = this._computeEntityIdFromAlias(currentAlias);
if (currentComputedEntity === currentEntityId || !this._entityId) {
const newComputedId = newAlias
? this._computeEntityIdFromAlias(newAlias)
: undefined;
this._setEntityId(newComputedId);
}
}
private _valueChanged(ev: CustomEvent) { private _valueChanged(ev: CustomEvent) {
ev.stopPropagation(); ev.stopPropagation();
this._errors = undefined;
const values = ev.detail.value as any; const values = ev.detail.value as any;
const currentId = this._entityId;
let changed = false; let changed = false;
const newValues: Omit<ScriptConfig, "sequence"> = {
alias: values.alias ?? "",
icon: values.icon,
mode: values.mode,
max: isMaxMode(values.mode) ? values.max : undefined,
};
for (const key of Object.keys(values)) { if (!this.scriptEntityId) {
if (key === "sequence") { this.updateEntityId(values.id, values.alias);
}
for (const key of Object.keys(newValues)) {
const value = newValues[key];
if (value === this._config![key]) {
continue; continue;
} }
if (value === undefined) {
const value = values[key];
if (
value === this._config![key] ||
(key === "id" && currentId === value)
) {
continue;
}
changed = true;
switch (key) {
case "id":
this._idChanged(value);
break;
case "alias":
this._aliasChanged(value);
break;
case "mode":
this._modeChanged(value);
break;
}
if (values[key] === undefined) {
const newConfig = { ...this._config! }; const newConfig = { ...this._config! };
delete newConfig![key]; delete newConfig![key];
this._config = newConfig; this._config = newConfig;
} else { } else {
this._config = { ...this._config!, [key]: value }; this._config = { ...this._config!, [key]: value };
} }
changed = true;
} }
if (changed) { if (changed) {
@ -638,6 +640,7 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
private _configChanged(ev) { private _configChanged(ev) {
this._config = ev.detail.value; this._config = ev.detail.value;
this._errors = undefined;
this._dirty = true; this._dirty = true;
} }
@ -759,7 +762,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
this.hass!.callApi("POST", "config/script/config/" + id, this._config).then( this.hass!.callApi("POST", "config/script/config/" + id, this._config).then(
() => { () => {
this._dirty = false; this._dirty = false;
if (!this.scriptEntityId) { if (!this.scriptEntityId) {
navigate(`/config/script/edit/${id}`, { replace: true }); navigate(`/config/script/edit/${id}`, { replace: true });
} }
@ -806,6 +808,10 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
max-width: 1040px; max-width: 1040px;
padding: 28px 20px 0; padding: 28px 20px 0;
} }
.config-container ha-alert {
margin-bottom: 16px;
display: block;
}
ha-yaml-editor { ha-yaml-editor {
flex-grow: 1; flex-grow: 1;
--code-mirror-height: 100%; --code-mirror-height: 100%;