mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Take convert of blueprint automation and script (#21151)
* substituteBlueprint * WIP ux * Simplify feature * Add take control to scripts * Add translations and catch error * Clean import --------- Co-authored-by: Paul Bottein <paul.bottein@gmail.com>
This commit is contained in:
parent
182111912c
commit
55b66250f4
@ -352,6 +352,22 @@ export const saveAutomationConfig = (
|
|||||||
config: AutomationConfig
|
config: AutomationConfig
|
||||||
) => hass.callApi<void>("POST", `config/automation/config/${id}`, config);
|
) => hass.callApi<void>("POST", `config/automation/config/${id}`, config);
|
||||||
|
|
||||||
|
export const normalizeAutomationConfig = <
|
||||||
|
T extends Partial<AutomationConfig> | AutomationConfig,
|
||||||
|
>(
|
||||||
|
config: T
|
||||||
|
): T => {
|
||||||
|
// 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;
|
||||||
|
};
|
||||||
|
|
||||||
export const showAutomationEditor = (data?: Partial<AutomationConfig>) => {
|
export const showAutomationEditor = (data?: Partial<AutomationConfig>) => {
|
||||||
initialAutomationEditorData = data;
|
initialAutomationEditorData = data;
|
||||||
navigate("/config/automation/edit/new");
|
navigate("/config/automation/edit/new");
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
|
import { ManualAutomationConfig } from "./automation";
|
||||||
|
import { ManualScriptConfig } from "./script";
|
||||||
import { Selector } from "./selector";
|
import { Selector } from "./selector";
|
||||||
|
|
||||||
export type BlueprintDomain = "automation" | "script";
|
export type BlueprintDomain = "automation" | "script";
|
||||||
@ -42,6 +44,11 @@ export interface BlueprintImportResult {
|
|||||||
validation_errors: string[] | null;
|
validation_errors: string[] | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface BlueprintSubstituteResults {
|
||||||
|
automation: { substituted_config: ManualAutomationConfig };
|
||||||
|
script: { substituted_config: ManualScriptConfig };
|
||||||
|
}
|
||||||
|
|
||||||
export const fetchBlueprints = (hass: HomeAssistant, domain: BlueprintDomain) =>
|
export const fetchBlueprints = (hass: HomeAssistant, domain: BlueprintDomain) =>
|
||||||
hass.callWS<Blueprints>({ type: "blueprint/list", domain });
|
hass.callWS<Blueprints>({ type: "blueprint/list", domain });
|
||||||
|
|
||||||
@ -91,3 +98,18 @@ export const getBlueprintSourceType = (
|
|||||||
}
|
}
|
||||||
return "community";
|
return "community";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const substituteBlueprint = <
|
||||||
|
T extends BlueprintDomain = BlueprintDomain,
|
||||||
|
>(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
domain: T,
|
||||||
|
path: string,
|
||||||
|
input: Record<string, any>
|
||||||
|
) =>
|
||||||
|
hass.callWS<BlueprintSubstituteResults[T]>({
|
||||||
|
type: "blueprint/substitute",
|
||||||
|
domain,
|
||||||
|
path,
|
||||||
|
input,
|
||||||
|
});
|
||||||
|
@ -3,10 +3,10 @@ import { HassEntity } from "home-assistant-js-websocket";
|
|||||||
import { html, nothing } from "lit";
|
import { html, nothing } from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import "../../../components/ha-alert";
|
import "../../../components/ha-alert";
|
||||||
|
import "../../../components/ha-markdown";
|
||||||
import { BlueprintAutomationConfig } from "../../../data/automation";
|
import { BlueprintAutomationConfig } from "../../../data/automation";
|
||||||
import { fetchBlueprints } from "../../../data/blueprint";
|
import { fetchBlueprints } from "../../../data/blueprint";
|
||||||
import { HaBlueprintGenericEditor } from "../blueprint/blueprint-generic-editor";
|
import { HaBlueprintGenericEditor } from "../blueprint/blueprint-generic-editor";
|
||||||
import "../../../components/ha-markdown";
|
|
||||||
|
|
||||||
@customElement("blueprint-automation-editor")
|
@customElement("blueprint-automation-editor")
|
||||||
export class HaBlueprintAutomationEditor extends HaBlueprintGenericEditor {
|
export class HaBlueprintAutomationEditor extends HaBlueprintGenericEditor {
|
||||||
|
@ -6,6 +6,7 @@ import {
|
|||||||
mdiDebugStepOver,
|
mdiDebugStepOver,
|
||||||
mdiDelete,
|
mdiDelete,
|
||||||
mdiDotsVertical,
|
mdiDotsVertical,
|
||||||
|
mdiFileEdit,
|
||||||
mdiInformationOutline,
|
mdiInformationOutline,
|
||||||
mdiPlay,
|
mdiPlay,
|
||||||
mdiPlayCircleOutline,
|
mdiPlayCircleOutline,
|
||||||
@ -40,10 +41,12 @@ import "../../../components/ha-yaml-editor";
|
|||||||
import {
|
import {
|
||||||
AutomationConfig,
|
AutomationConfig,
|
||||||
AutomationEntity,
|
AutomationEntity,
|
||||||
|
BlueprintAutomationConfig,
|
||||||
deleteAutomation,
|
deleteAutomation,
|
||||||
fetchAutomationFileConfig,
|
fetchAutomationFileConfig,
|
||||||
getAutomationEditorInitData,
|
getAutomationEditorInitData,
|
||||||
getAutomationStateConfig,
|
getAutomationStateConfig,
|
||||||
|
normalizeAutomationConfig,
|
||||||
saveAutomationConfig,
|
saveAutomationConfig,
|
||||||
showAutomationEditor,
|
showAutomationEditor,
|
||||||
triggerAutomationActions,
|
triggerAutomationActions,
|
||||||
@ -65,6 +68,7 @@ import { showAutomationModeDialog } from "./automation-mode-dialog/show-dialog-a
|
|||||||
import { showAutomationRenameDialog } from "./automation-rename-dialog/show-dialog-automation-rename";
|
import { showAutomationRenameDialog } from "./automation-rename-dialog/show-dialog-automation-rename";
|
||||||
import "./blueprint-automation-editor";
|
import "./blueprint-automation-editor";
|
||||||
import "./manual-automation-editor";
|
import "./manual-automation-editor";
|
||||||
|
import { substituteBlueprint } from "../../../data/blueprint";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface HTMLElementTagNameMap {
|
interface HTMLElementTagNameMap {
|
||||||
@ -235,6 +239,24 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
></ha-svg-icon>
|
></ha-svg-icon>
|
||||||
</ha-list-item>
|
</ha-list-item>
|
||||||
|
|
||||||
|
${useBlueprint
|
||||||
|
? html`
|
||||||
|
<ha-list-item
|
||||||
|
graphic="icon"
|
||||||
|
@click=${this._takeControl}
|
||||||
|
.disabled=${this._readOnly || this._mode === "yaml"}
|
||||||
|
>
|
||||||
|
${this.hass.localize(
|
||||||
|
"ui.panel.config.automation.editor.take_control"
|
||||||
|
)}
|
||||||
|
<ha-svg-icon
|
||||||
|
slot="graphic"
|
||||||
|
.path=${mdiFileEdit}
|
||||||
|
></ha-svg-icon>
|
||||||
|
</ha-list-item>
|
||||||
|
`
|
||||||
|
: nothing}
|
||||||
|
|
||||||
<li divider role="separator"></li>
|
<li divider role="separator"></li>
|
||||||
|
|
||||||
<ha-list-item graphic="icon" @click=${this._switchUiMode}>
|
<ha-list-item graphic="icon" @click=${this._switchUiMode}>
|
||||||
@ -432,7 +454,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
this._config = {
|
this._config = {
|
||||||
...baseConfig,
|
...baseConfig,
|
||||||
...initData,
|
...(initData ? normalizeAutomationConfig(initData) : initData),
|
||||||
} as AutomationConfig;
|
} as AutomationConfig;
|
||||||
this._entityId = undefined;
|
this._entityId = undefined;
|
||||||
this._readOnly = false;
|
this._readOnly = false;
|
||||||
@ -441,7 +463,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
|
|
||||||
if (changedProps.has("entityId") && this.entityId) {
|
if (changedProps.has("entityId") && this.entityId) {
|
||||||
getAutomationStateConfig(this.hass, this.entityId).then((c) => {
|
getAutomationStateConfig(this.hass, this.entityId).then((c) => {
|
||||||
this._config = this._normalizeConfig(c.config);
|
this._config = normalizeAutomationConfig(c.config);
|
||||||
this._checkValidation();
|
this._checkValidation();
|
||||||
});
|
});
|
||||||
this._entityId = this.entityId;
|
this._entityId = this.entityId;
|
||||||
@ -497,18 +519,6 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
private async _loadConfig() {
|
||||||
try {
|
try {
|
||||||
const config = await fetchAutomationFileConfig(
|
const config = await fetchAutomationFileConfig(
|
||||||
@ -517,7 +527,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
);
|
);
|
||||||
this._dirty = false;
|
this._dirty = false;
|
||||||
this._readOnly = false;
|
this._readOnly = false;
|
||||||
this._config = this._normalizeConfig(config);
|
this._config = normalizeAutomationConfig(config);
|
||||||
this._checkValidation();
|
this._checkValidation();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const entityRegistry = await fetchEntityRegistry(this.hass.connection);
|
const entityRegistry = await fetchEntityRegistry(this.hass.connection);
|
||||||
@ -638,6 +648,45 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private async _takeControl() {
|
||||||
|
const config = this._config as BlueprintAutomationConfig;
|
||||||
|
|
||||||
|
const confirmation = await showConfirmationDialog(this, {
|
||||||
|
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;
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
this._config = newConfig;
|
||||||
|
this._dirty = true;
|
||||||
|
this._errors = undefined;
|
||||||
|
} catch (err: any) {
|
||||||
|
this._errors = err.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async _duplicate() {
|
private async _duplicate() {
|
||||||
const result = this._readOnly
|
const result = this._readOnly
|
||||||
? await showConfirmationDialog(this, {
|
? await showConfirmationDialog(this, {
|
||||||
|
@ -2,10 +2,10 @@ import "@material/mwc-button/mwc-button";
|
|||||||
import { html, nothing } from "lit";
|
import { html, nothing } from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import "../../../components/ha-alert";
|
import "../../../components/ha-alert";
|
||||||
import { BlueprintScriptConfig } from "../../../data/script";
|
|
||||||
import { fetchBlueprints } from "../../../data/blueprint";
|
|
||||||
import { HaBlueprintGenericEditor } from "../blueprint/blueprint-generic-editor";
|
|
||||||
import "../../../components/ha-markdown";
|
import "../../../components/ha-markdown";
|
||||||
|
import { fetchBlueprints } from "../../../data/blueprint";
|
||||||
|
import { BlueprintScriptConfig } from "../../../data/script";
|
||||||
|
import { HaBlueprintGenericEditor } from "../blueprint/blueprint-generic-editor";
|
||||||
|
|
||||||
@customElement("blueprint-script-editor")
|
@customElement("blueprint-script-editor")
|
||||||
export class HaBlueprintScriptEditor extends HaBlueprintGenericEditor {
|
export class HaBlueprintScriptEditor extends HaBlueprintGenericEditor {
|
||||||
|
@ -6,6 +6,7 @@ import {
|
|||||||
mdiDebugStepOver,
|
mdiDebugStepOver,
|
||||||
mdiDelete,
|
mdiDelete,
|
||||||
mdiDotsVertical,
|
mdiDotsVertical,
|
||||||
|
mdiFileEdit,
|
||||||
mdiFormTextbox,
|
mdiFormTextbox,
|
||||||
mdiInformationOutline,
|
mdiInformationOutline,
|
||||||
mdiPlay,
|
mdiPlay,
|
||||||
@ -40,6 +41,7 @@ import { validateConfig } from "../../../data/config";
|
|||||||
import { UNAVAILABLE } from "../../../data/entity";
|
import { UNAVAILABLE } from "../../../data/entity";
|
||||||
import { EntityRegistryEntry } from "../../../data/entity_registry";
|
import { EntityRegistryEntry } from "../../../data/entity_registry";
|
||||||
import {
|
import {
|
||||||
|
BlueprintScriptConfig,
|
||||||
ScriptConfig,
|
ScriptConfig,
|
||||||
deleteScript,
|
deleteScript,
|
||||||
fetchScriptFileConfig,
|
fetchScriptFileConfig,
|
||||||
@ -61,6 +63,7 @@ import { showAutomationRenameDialog } from "../automation/automation-rename-dial
|
|||||||
import "./blueprint-script-editor";
|
import "./blueprint-script-editor";
|
||||||
import "./manual-script-editor";
|
import "./manual-script-editor";
|
||||||
import type { HaManualScriptEditor } from "./manual-script-editor";
|
import type { HaManualScriptEditor } from "./manual-script-editor";
|
||||||
|
import { substituteBlueprint } from "../../../data/blueprint";
|
||||||
|
|
||||||
export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
|
export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
@ -228,6 +231,24 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
></ha-svg-icon>
|
></ha-svg-icon>
|
||||||
</ha-list-item>
|
</ha-list-item>
|
||||||
|
|
||||||
|
${useBlueprint
|
||||||
|
? html`
|
||||||
|
<ha-list-item
|
||||||
|
graphic="icon"
|
||||||
|
@click=${this._takeControl}
|
||||||
|
.disabled=${this._readOnly || this._mode === "yaml"}
|
||||||
|
>
|
||||||
|
${this.hass.localize(
|
||||||
|
"ui.panel.config.script.editor.take_control"
|
||||||
|
)}
|
||||||
|
<ha-svg-icon
|
||||||
|
slot="graphic"
|
||||||
|
.path=${mdiFileEdit}
|
||||||
|
></ha-svg-icon>
|
||||||
|
</ha-list-item>
|
||||||
|
`
|
||||||
|
: nothing}
|
||||||
|
|
||||||
<li divider role="separator"></li>
|
<li divider role="separator"></li>
|
||||||
|
|
||||||
<ha-list-item graphic="icon" @click=${this._switchUiMode}>
|
<ha-list-item graphic="icon" @click=${this._switchUiMode}>
|
||||||
@ -601,6 +622,45 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private async _takeControl() {
|
||||||
|
const config = this._config as BlueprintScriptConfig;
|
||||||
|
|
||||||
|
const confirmation = await showConfirmationDialog(this, {
|
||||||
|
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;
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
this._config = newConfig;
|
||||||
|
this._dirty = true;
|
||||||
|
this._errors = undefined;
|
||||||
|
} catch (err: any) {
|
||||||
|
this._errors = err.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async _duplicate() {
|
private async _duplicate() {
|
||||||
const result = this._readOnly
|
const result = this._readOnly
|
||||||
? await showConfirmationDialog(this, {
|
? await showConfirmationDialog(this, {
|
||||||
|
@ -2759,6 +2759,12 @@
|
|||||||
"unavailable": "Automation is unavailable",
|
"unavailable": "Automation is unavailable",
|
||||||
"migrate": "Migrate",
|
"migrate": "Migrate",
|
||||||
"duplicate": "[%key:ui::common::duplicate%]",
|
"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%]",
|
"run": "[%key:ui::panel::config::automation::editor::actions::run%]",
|
||||||
"rename": "[%key:ui::panel::config::automation::editor::triggers::rename%]",
|
"rename": "[%key:ui::panel::config::automation::editor::triggers::rename%]",
|
||||||
"show_trace": "Traces",
|
"show_trace": "Traces",
|
||||||
@ -3629,6 +3635,12 @@
|
|||||||
"show_info": "[%key:ui::panel::config::automation::editor::show_info%]",
|
"show_info": "[%key:ui::panel::config::automation::editor::show_info%]",
|
||||||
"rename": "[%key:ui::panel::config::automation::editor::triggers::rename%]",
|
"rename": "[%key:ui::panel::config::automation::editor::triggers::rename%]",
|
||||||
"change_mode": "[%key:ui::panel::config::automation::editor::change_mode%]",
|
"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.",
|
"read_only": "This script cannot be edited from the UI, because it is not stored in the ''scripts.yaml'' file.",
|
||||||
"unavailable": "Script is unavailable",
|
"unavailable": "Script is unavailable",
|
||||||
"migrate": "Migrate",
|
"migrate": "Migrate",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user