Normalize automation config (#13938)

* Normalize automation config

* gen

* Update ha-automation-action-choose.ts
This commit is contained in:
Bram Kragten 2022-10-02 00:02:24 +02:00 committed by GitHub
parent 6393944a1b
commit 4f4a95c04e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 23 deletions

View File

@ -189,7 +189,11 @@ export const describeTrigger = (
// Time Trigger
if (trigger.platform === "time" && trigger.at) {
const at = trigger.at.includes(".")
? `entity ${computeStateName(hass.states[trigger.at]) || trigger.at}`
? `entity ${
hass.states[trigger.at]
? computeStateName(hass.states[trigger.at])
: trigger.at
}`
: trigger.at;
return `When the time is equal to ${at}`;

View File

@ -55,7 +55,7 @@ export class HaChooseAction extends LitElement implements ActionElement {
)}:
</h3>
<ha-automation-condition
.conditions=${option.conditions}
.conditions=${ensureArray<string | Condition>(option.conditions)}
.reOrderMode=${this.reOrderMode}
.disabled=${this.disabled}
.hass=${this.hass}
@ -68,7 +68,7 @@ export class HaChooseAction extends LitElement implements ActionElement {
)}:
</h3>
<ha-automation-action
.actions=${option.sequence || []}
.actions=${ensureArray(option.sequence) || []}
.reOrderMode=${this.reOrderMode}
.disabled=${this.disabled}
.hass=${this.hass}
@ -96,7 +96,7 @@ export class HaChooseAction extends LitElement implements ActionElement {
)}:
</h2>
<ha-automation-action
.actions=${action.default || []}
.actions=${ensureArray(action.default) || []}
.reOrderMode=${this.reOrderMode}
.disabled=${this.disabled}
@value-changed=${this._defaultChanged}

View File

@ -444,7 +444,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
if (changedProps.has("entityId") && this.entityId) {
getAutomationStateConfig(this.hass, this.entityId).then((c) => {
this._config = c.config;
this._config = this._normalizeConfig(c.config);
});
this._entityId = this.entityId;
this._dirty = false;
@ -473,24 +473,27 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
this._entityId = automation?.entity_id;
}
private _normalizeConfig(config: AutomationConfig): AutomationConfig {
// Normalize data: ensure trigger, action and condition are lists
// Happens when people copy paste their automations into the config
for (const key of ["trigger", "condition", "action"]) {
const value = config[key];
if (value && !Array.isArray(value)) {
config[key] = [value];
}
}
return config;
}
private async _loadConfig() {
try {
const config = await fetchAutomationFileConfig(
this.hass,
this.automationId as string
);
// Normalize data: ensure trigger, action and condition are lists
// Happens when people copy paste their automations into the config
for (const key of ["trigger", "condition", "action"]) {
const value = config[key];
if (value && !Array.isArray(value)) {
config[key] = [value];
}
}
this._dirty = false;
this._readOnly = false;
this._config = config;
this._config = this._normalizeConfig(config);
} catch (err: any) {
const entity = Object.values(this.hass.entities).find(
(ent) =>

View File

@ -469,15 +469,9 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
) {
fetchScriptFileConfig(this.hass, this.scriptId).then(
(config) => {
// Normalize data: ensure sequence is a list
// Happens when people copy paste their scripts into the config
const value = config.sequence;
if (value && !Array.isArray(value)) {
config.sequence = [value];
}
this._dirty = false;
this._readOnly = false;
this._config = config;
this._config = this._normalizeConfig(config);
},
(resp) => {
const entity = Object.values(this.hass.entities).find(
@ -524,7 +518,7 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
if (changedProps.has("entityId") && this.entityId) {
getScriptStateConfig(this.hass, this.entityId).then((c) => {
this._config = c.config;
this._config = this._normalizeConfig(c.config);
});
const regEntry = this.hass.entities[this.entityId];
if (regEntry?.unique_id) {
@ -536,6 +530,16 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
}
}
private _normalizeConfig(config: ScriptConfig): ScriptConfig {
// Normalize data: ensure sequence is a list
// Happens when people copy paste their scripts into the config
const value = config.sequence;
if (value && !Array.isArray(value)) {
config.sequence = [value];
}
return config;
}
private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>,
data: HaFormDataContainer