Skip to content

Commit

Permalink
Add translations and catch error
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Jun 26, 2024
1 parent 88abea4 commit 9541475
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 64 deletions.
76 changes: 44 additions & 32 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,23 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
</ha-list-item>
`
: nothing}
<ha-list-item
.disabled=${!this._readOnly && !this.automationId}
graphic="icon"
@click=${this._duplicate}
>
${this.hass.localize(
this._readOnly
? "ui.panel.config.automation.editor.migrate"
: "ui.panel.config.automation.editor.duplicate"
)}
<ha-svg-icon
slot="graphic"
.path=${mdiContentDuplicate}
></ha-svg-icon>
</ha-list-item>
${useBlueprint
? html`
<ha-list-item
Expand All @@ -240,22 +257,6 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
`
: nothing}
<ha-list-item
.disabled=${!this._readOnly && !this.automationId}
graphic="icon"
@click=${this._duplicate}
>
${this.hass.localize(
this._readOnly
? "ui.panel.config.automation.editor.migrate"
: "ui.panel.config.automation.editor.duplicate"
)}
<ha-svg-icon
slot="graphic"
.path=${mdiContentDuplicate}
></ha-svg-icon>
</ha-list-item>
<li divider role="separator"></li>
<ha-list-item graphic="icon" @click=${this._switchUiMode}>
Expand Down Expand Up @@ -651,28 +652,39 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
const config = this._config as BlueprintAutomationConfig;

const confirmation = await showConfirmationDialog(this, {
title: "Take control of automation?",
text: "This automation is using a blueprint. By taking control, you will be able to edit it directly. Are you sure you want to take control?",
title: this.hass!.localize(
"ui.panel.config.automation.editor.take_control_confirmation.title"
),
text: this.hass!.localize(
"ui.panel.config.automation.editor.take_control_confirmation.text"
),
confirmText: this.hass!.localize(
"ui.panel.config.automation.editor.take_control_confirmation.action"
),
});

if (!confirmation) return;

const result = await substituteBlueprint(
this.hass,
"automation",
config.use_blueprint.path,
config.use_blueprint.input || {}
);
try {
const result = await substituteBlueprint(
this.hass,
"automation",
config.use_blueprint.path,
config.use_blueprint.input || {}
);

const newConfig = {
...normalizeAutomationConfig(result.substituted_config),
alias: config.alias,
description: config.description,
};
const newConfig = {
...normalizeAutomationConfig(result.substituted_config),
alias: config.alias,
description: config.description,
};

this._config = newConfig;
this._dirty = true;
this._errors = undefined;
this._config = newConfig;
this._dirty = true;
this._errors = undefined;
} catch (err: any) {
this._errors = err.message;
}
}

private async _duplicate() {
Expand Down
76 changes: 44 additions & 32 deletions src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
</ha-list-item>
`
: nothing}
<ha-list-item
.disabled=${!this._readOnly && !this.scriptId}
graphic="icon"
@click=${this._duplicate}
>
${this.hass.localize(
this._readOnly
? "ui.panel.config.script.editor.migrate"
: "ui.panel.config.script.editor.duplicate"
)}
<ha-svg-icon
slot="graphic"
.path=${mdiContentDuplicate}
></ha-svg-icon>
</ha-list-item>
${useBlueprint
? html`
<ha-list-item
Expand All @@ -232,22 +249,6 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
`
: nothing}
<ha-list-item
.disabled=${!this._readOnly && !this.scriptId}
graphic="icon"
@click=${this._duplicate}
>
${this.hass.localize(
this._readOnly
? "ui.panel.config.script.editor.migrate"
: "ui.panel.config.script.editor.duplicate"
)}
<ha-svg-icon
slot="graphic"
.path=${mdiContentDuplicate}
></ha-svg-icon>
</ha-list-item>
<li divider role="separator"></li>
<ha-list-item graphic="icon" @click=${this._switchUiMode}>
Expand Down Expand Up @@ -625,28 +626,39 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
const config = this._config as BlueprintScriptConfig;

const confirmation = await showConfirmationDialog(this, {
title: "Take control of automation?",
text: "This automation is using a blueprint. By taking control, you will be able to edit it directly. Are you sure you want to take control?",
title: this.hass!.localize(
"ui.panel.config.script.editor.take_control_confirmation.title"
),
text: this.hass!.localize(
"ui.panel.config.script.editor.take_control_confirmation.text"
),
confirmText: this.hass!.localize(
"ui.panel.config.script.editor.take_control_confirmation.action"
),
});

if (!confirmation) return;

const result = await substituteBlueprint(
this.hass,
"script",
config.use_blueprint.path,
config.use_blueprint.input || {}
);
try {
const result = await substituteBlueprint(
this.hass,
"script",
config.use_blueprint.path,
config.use_blueprint.input || {}
);

const newConfig = {
...this._normalizeConfig(result.substituted_config),
alias: config.alias,
description: config.description,
};
const newConfig = {
...this._normalizeConfig(result.substituted_config),
alias: config.alias,
description: config.description,
};

this._config = newConfig;
this._dirty = true;
this._errors = undefined;
this._config = newConfig;
this._dirty = true;
this._errors = undefined;
} catch (err: any) {
this._errors = err.message;
}
}

private async _duplicate() {
Expand Down
10 changes: 10 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2760,6 +2760,11 @@
"migrate": "Migrate",
"duplicate": "[%key:ui::common::duplicate%]",
"take_control": "Take control",
"take_control_confirmation": {
"title": "Take control of automation?",
"text": "This automation is using a blueprint. By taking control, your automation will be converted into a regular automation using triggers, conditions and actions. You will be able to edit it directly and you won't be able to convert it back to a blueprint.",
"action": "Take control"
},
"run": "[%key:ui::panel::config::automation::editor::actions::run%]",
"rename": "[%key:ui::panel::config::automation::editor::triggers::rename%]",
"show_trace": "Traces",
Expand Down Expand Up @@ -3631,6 +3636,11 @@
"rename": "[%key:ui::panel::config::automation::editor::triggers::rename%]",
"change_mode": "[%key:ui::panel::config::automation::editor::change_mode%]",
"take_control": "[%key:ui::panel::config::automation::editor::take_control%]",
"take_control_confirmation": {
"title": "Take control of script?",
"text": "This script is using a blueprint. By taking control, your script will be converted into a regular automation using actions. You will be able to edit it directly and you won't be able to convert it back to a blueprint.",
"action": "[%key:ui::panel::config::automation::editor::take_control_confirmation::action%]"
},
"read_only": "This script cannot be edited from the UI, because it is not stored in the ''scripts.yaml'' file.",
"unavailable": "Script is unavailable",
"migrate": "Migrate",
Expand Down

0 comments on commit 9541475

Please sign in to comment.