From 7e80eed003f961f1070e40ccebabeaab57fd2964 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Thu, 2 Jan 2025 04:46:36 -0800 Subject: [PATCH] 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 --- .../config/automation/ha-automation-editor.ts | 7 ++- src/panels/config/script/ha-script-editor.ts | 55 +++++++++++++++---- src/translations/en.json | 4 +- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/panels/config/automation/ha-automation-editor.ts b/src/panels/config/automation/ha-automation-editor.ts index e6ca397584..05df54facf 100644 --- a/src/panels/config/automation/ha-automation-editor.ts +++ b/src/panels/config/automation/ha-automation-editor.ts @@ -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 { diff --git a/src/panels/config/script/ha-script-editor.ts b/src/panels/config/script/ha-script-editor.ts index c1d9bc39ca..8f7a639a32 100644 --- a/src/panels/config/script/ha-script-editor.ts +++ b/src/panels/config/script/ha-script-editor.ts @@ -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 { diff --git a/src/translations/en.json b/src/translations/en.json index 09a80f5f99..01f4d1436a 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -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",