Display an error if saving new script times out (#23527)

* Display an error if saving new automation times out

* changes

* update

* string tweak

* Fix save failed for scripts
This commit is contained in:
karwosts 2025-01-02 04:46:36 -08:00 committed by Bram Kragten
parent a7ef498d75
commit 7e80eed003
3 changed files with 52 additions and 14 deletions

View File

@ -965,6 +965,9 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
type: this.hass.localize(
"ui.panel.config.automation.editor.type_automation"
),
types: this.hass.localize(
"ui.panel.config.automation.editor.type_automation_plural"
),
}
),
warning: true,
@ -992,9 +995,9 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
navigate(`/config/automation/edit/${id}`, { replace: true });
}
} catch (errors: any) {
this._errors = errors.body.message || errors.error || errors.body;
this._errors = errors.body?.message || errors.error || errors.body;
showToast(this, {
message: errors.body.message || errors.error || errors.body,
message: errors.body?.message || errors.error || errors.body,
});
throw errors;
} finally {

View File

@ -26,6 +26,7 @@ import { navigate } from "../../../common/navigate";
import { slugify } from "../../../common/string/slugify";
import { computeRTL } from "../../../common/util/compute_rtl";
import { afterNextRender } from "../../../common/util/render-status";
import { promiseTimeout } from "../../../common/util/promise-timeout";
import "../../../components/ha-button-menu";
import "../../../components/ha-fab";
@ -915,17 +916,49 @@ export class HaScriptEditor extends SubscribeMixin(
// wait for new script to appear in entity registry
if (entityRegPromise) {
const script = await entityRegPromise;
entityId = script.entity_id;
try {
const script = await promiseTimeout(2000, entityRegPromise);
entityId = script.entity_id;
} catch (e) {
entityId = undefined;
if (e instanceof Error && e.name === "TimeoutError") {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.automation.editor.new_automation_setup_failed_title",
{
type: this.hass.localize(
"ui.panel.config.automation.editor.type_script"
),
}
),
text: this.hass.localize(
"ui.panel.config.automation.editor.new_automation_setup_failed_text",
{
type: this.hass.localize(
"ui.panel.config.automation.editor.type_script"
),
types: this.hass.localize(
"ui.panel.config.automation.editor.type_script_plural"
),
}
),
warning: true,
});
} else {
throw e;
}
}
}
await updateEntityRegistryEntry(this.hass, entityId!, {
categories: {
script: this._entityRegistryUpdate.category || null,
},
labels: this._entityRegistryUpdate.labels || [],
area_id: this._entityRegistryUpdate.area || null,
});
if (entityId) {
await updateEntityRegistryEntry(this.hass, entityId, {
categories: {
script: this._entityRegistryUpdate.category || null,
},
labels: this._entityRegistryUpdate.labels || [],
area_id: this._entityRegistryUpdate.area || null,
});
}
}
this._dirty = false;
@ -934,9 +967,9 @@ export class HaScriptEditor extends SubscribeMixin(
navigate(`/config/script/edit/${id}`, { replace: true });
}
} catch (errors: any) {
this._errors = errors.body.message || errors.error || errors.body;
this._errors = errors.body?.message || errors.error || errors.body;
showToast(this, {
message: errors.body.message || errors.error || errors.body,
message: errors.body?.message || errors.error || errors.body,
});
throw errors;
} finally {

View File

@ -3067,8 +3067,10 @@
"switch_ui_yaml_error": "There are currently YAML errors in the automation, and it cannot be parsed. Switching to UI mode may cause pending changes to be lost. Press cancel to correct any errors before proceeding to prevent loss of pending changes, or continue if you are sure.",
"type_automation": "automation",
"type_script": "script",
"type_automation_plural": "[%key:ui::panel::config::blueprint::overview::types_plural::automation%]",
"type_script_plural": "[%key:ui::panel::config::blueprint::overview::types_plural::script%]",
"new_automation_setup_failed_title": "New {type} setup failed",
"new_automation_setup_failed_text": "Your new {type} has saved, but waiting for it to setup has timed out. This could be due to errors parsing your configuration.yaml, please check the configuration in developer tools. Your {type} will not be visible until this is corrected, and automations are reloaded. Changes to area, category, or labels were not saved and must be reapplied.",
"new_automation_setup_failed_text": "Your new {type} has saved, but waiting for it to setup has timed out. This could be due to errors parsing your configuration.yaml, please check the configuration in developer tools. Your {type} will not be visible until this is corrected, and {types} are reloaded. Changes to area, category, or labels were not saved and must be reapplied.",
"triggers": {
"name": "Triggers",
"header": "When",