Some fixes for automation picker and editor (#13634)

This commit is contained in:
Paul Bottein 2022-09-07 13:47:56 +02:00 committed by GitHub
parent d1964e92ea
commit b553a3fd92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 39 deletions

View File

@ -17,7 +17,7 @@ export interface IconOverflowMenuItem {
narrowOnly?: boolean; narrowOnly?: boolean;
disabled?: boolean; disabled?: boolean;
tooltip?: string; tooltip?: string;
onClick: CallableFunction; action: () => any;
warning?: boolean; warning?: boolean;
} }
@ -50,7 +50,7 @@ export class HaIconOverflowMenu extends LitElement {
(item) => html` (item) => html`
<mwc-list-item <mwc-list-item
graphic="icon" graphic="icon"
.disabled=${item.disabled} ?disabled=${item.disabled}
@click=${item.action} @click=${item.action}
class=${classMap({ warning: Boolean(item.warning) })} class=${classMap({ warning: Boolean(item.warning) })}
> >
@ -80,7 +80,7 @@ export class HaIconOverflowMenu extends LitElement {
@click=${item.action} @click=${item.action}
.label=${item.label} .label=${item.label}
.path=${item.path} .path=${item.path}
.disabled=${item.disabled} ?disabled=${item.disabled}
></ha-icon-button> ></ha-icon-button>
</div> ` </div> `
)} )}

View File

@ -314,11 +314,25 @@ let inititialAutomationEditorData: Partial<AutomationConfig> | undefined;
export const getAutomationConfig = (hass: HomeAssistant, id: string) => export const getAutomationConfig = (hass: HomeAssistant, id: string) =>
hass.callApi<AutomationConfig>("GET", `config/automation/config/${id}`); hass.callApi<AutomationConfig>("GET", `config/automation/config/${id}`);
export const saveAutomationConfig = (
hass: HomeAssistant,
id: string,
config: AutomationConfig
) => hass.callApi<void>("POST", `config/automation/config/${id}`, config);
export const showAutomationEditor = (data?: Partial<AutomationConfig>) => { export const showAutomationEditor = (data?: Partial<AutomationConfig>) => {
inititialAutomationEditorData = data; inititialAutomationEditorData = data;
navigate("/config/automation/edit/new"); navigate("/config/automation/edit/new");
}; };
export const duplicateAutomation = (config: AutomationConfig) => {
showAutomationEditor({
...config,
id: undefined,
alias: undefined,
});
};
export const getAutomationEditorInitData = () => { export const getAutomationEditorInitData = () => {
const data = inititialAutomationEditorData; const data = inititialAutomationEditorData;
inititialAutomationEditorData = undefined; inititialAutomationEditorData = undefined;

View File

@ -45,6 +45,7 @@ import {
deleteAutomation, deleteAutomation,
getAutomationConfig, getAutomationConfig,
getAutomationEditorInitData, getAutomationEditorInitData,
saveAutomationConfig,
showAutomationEditor, showAutomationEditor,
triggerAutomationActions, triggerAutomationActions,
} from "../../../data/automation"; } from "../../../data/automation";
@ -203,7 +204,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
<mwc-list-item <mwc-list-item
graphic="icon" graphic="icon"
@click=${this._promptAutomationMode} @click=${this._promptAutomationMode}
.disabled=${!this.automationId || this._mode === "yaml"} .disabled=${this._mode === "yaml"}
> >
${this.hass.localize( ${this.hass.localize(
"ui.panel.config.automation.editor.change_mode" "ui.panel.config.automation.editor.change_mode"
@ -452,7 +453,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
this._dirty = false; this._dirty = false;
this._config = config; this._config = config;
} catch (err: any) { } catch (err: any) {
showAlertDialog(this, { await showAlertDialog(this, {
text: text:
err.status_code === 404 err.status_code === 404
? this.hass.localize( ? this.hass.localize(
@ -463,7 +464,8 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
"err_no", "err_no",
err.status_code err.status_code
), ),
}).then(() => history.back()); });
history.back();
} }
} }
@ -582,9 +584,11 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
} }
private async _delete() { private async _delete() {
await deleteAutomation(this.hass, this.automationId as string); if (this.automationId) {
await deleteAutomation(this.hass, this.automationId);
history.back(); history.back();
} }
}
private _switchUiMode() { private _switchUiMode() {
this._mode = "gui"; this._mode = "gui";
@ -636,26 +640,21 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
await this._promptAutomationAlias(); await this._promptAutomationAlias();
} }
this.hass!.callApi( try {
"POST", await saveAutomationConfig(this.hass, id, this._config!);
"config/automation/config/" + id, } catch (errors: any) {
this._config
).then(
() => {
this._dirty = false;
if (!this.automationId) {
navigate(`/config/automation/edit/${id}`, { replace: true });
}
},
(errors) => {
this._errors = errors.body.message || errors.error || errors.body; this._errors = errors.body.message || errors.error || errors.body;
showToast(this, { showToast(this, {
message: errors.body.message || errors.error || errors.body, message: errors.body.message || errors.error || errors.body,
}); });
throw errors; throw errors;
} }
);
this._dirty = false;
if (!this.automationId) {
navigate(`/config/automation/edit/${id}`, { replace: true });
}
} }
private _subscribeAutomationConfig(ev) { private _subscribeAutomationConfig(ev) {

View File

@ -33,8 +33,8 @@ import "../../../components/ha-svg-icon";
import { import {
AutomationEntity, AutomationEntity,
deleteAutomation, deleteAutomation,
duplicateAutomation,
getAutomationConfig, getAutomationConfig,
showAutomationEditor,
triggerAutomationActions, triggerAutomationActions,
} from "../../../data/automation"; } from "../../../data/automation";
import { import {
@ -348,20 +348,46 @@ class HaAutomationPicker extends LitElement {
} }
private async _delete(automation) { private async _delete(automation) {
try {
await deleteAutomation(this.hass, automation.attributes.id); await deleteAutomation(this.hass, automation.attributes.id);
} catch (err: any) {
await showAlertDialog(this, {
text:
err.status_code === 400
? this.hass.localize(
"ui.panel.config.automation.editor.load_error_not_deletable"
)
: this.hass.localize(
"ui.panel.config.automation.editor.load_error_unknown",
"err_no",
err.status_code
),
});
}
} }
private async duplicate(automation) { private async duplicate(automation) {
try {
const config = await getAutomationConfig( const config = await getAutomationConfig(
this.hass, this.hass,
automation.attributes.id automation.attributes.id
); );
showAutomationEditor({ duplicateAutomation(config);
...config, } catch (err: any) {
id: undefined, await showAlertDialog(this, {
alias: undefined, text:
err.status_code === 404
? this.hass.localize(
"ui.panel.config.automation.editor.load_error_not_duplicable"
)
: this.hass.localize(
"ui.panel.config.automation.editor.load_error_unknown",
"err_no",
err.status_code
),
}); });
} }
}
private _showHelp() { private _showHelp() {
showAlertDialog(this, { showAlertDialog(this, {
@ -389,7 +415,7 @@ class HaAutomationPicker extends LitElement {
); );
if (automation?.attributes.id) { if (automation?.attributes.id) {
navigate(`/config/automation/edit/${automation?.attributes.id}`); navigate(`/config/automation/edit/${automation.attributes.id}`);
} }
} }

View File

@ -1828,6 +1828,8 @@
"default_name": "New Automation", "default_name": "New Automation",
"missing_name": "Cannot save automation without a name", "missing_name": "Cannot save automation without a name",
"load_error_not_editable": "Only automations in automations.yaml are editable.", "load_error_not_editable": "Only automations in automations.yaml are editable.",
"load_error_not_duplicable": "Only automations in automations.yaml can be duplicated.",
"load_error_not_deletable": "Only automations in automations.yaml can be deleted.",
"load_error_unknown": "Error loading automation ({err_no}).", "load_error_unknown": "Error loading automation ({err_no}).",
"save": "Save", "save": "Save",
"unsaved_confirm": "You have unsaved changes. Are you sure you want to leave?", "unsaved_confirm": "You have unsaved changes. Are you sure you want to leave?",