diff --git a/src/data/automation.ts b/src/data/automation.ts index d25fb5790b..c34a7ff0fb 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -15,6 +15,16 @@ import { CONDITION_BUILDING_BLOCKS } from "./condition"; export const AUTOMATION_DEFAULT_MODE: (typeof MODES)[number] = "single"; export const AUTOMATION_DEFAULT_MAX = 10; +declare global { + interface HASSDomEvents { + /** + * Dispatched to open the automation editor. + * Used by custom cards/panels to trigger the editor view. + */ + "show-automation-editor": ShowAutomationEditorParams; + } +} + export interface AutomationEntity extends HassEntityBase { attributes: HassEntityAttributeBase & { id?: string; @@ -546,3 +556,8 @@ export interface AutomationClipboard { condition?: Condition; action?: Action; } + +export interface ShowAutomationEditorParams { + data?: Partial; + expanded?: boolean; +} diff --git a/src/state/automation-editor-mixin.ts b/src/state/automation-editor-mixin.ts new file mode 100644 index 0000000000..458c02702c --- /dev/null +++ b/src/state/automation-editor-mixin.ts @@ -0,0 +1,26 @@ +import type { PropertyValues } from "lit"; +import type { HASSDomEvent } from "../common/dom/fire_event"; +import { + showAutomationEditor, + type ShowAutomationEditorParams, +} from "../data/automation"; +import type { Constructor } from "../types"; +import type { HassBaseEl } from "./hass-base-mixin"; + +export default >(superClass: T) => + class extends superClass { + protected firstUpdated(changedProps: PropertyValues) { + super.firstUpdated(changedProps); + this.addEventListener("show-automation-editor", (ev) => + this._handleShowAutomationEditor( + ev as HASSDomEvent + ) + ); + } + + private _handleShowAutomationEditor( + ev: HASSDomEvent + ) { + showAutomationEditor(ev.detail.data, ev.detail.expanded); + } + }; diff --git a/src/state/hass-element.ts b/src/state/hass-element.ts index c0e7ca92a5..381d5dea66 100644 --- a/src/state/hass-element.ts +++ b/src/state/hass-element.ts @@ -8,6 +8,7 @@ import { HassBaseEl } from "./hass-base-mixin"; import { loggingMixin } from "./logging-mixin"; import { contextMixin } from "./context-mixin"; import MoreInfoMixin from "./more-info-mixin"; +import AutomationEditorMixin from "./automation-editor-mixin"; import ActionMixin from "./action-mixin"; import NotificationMixin from "./notification-mixin"; import { panelTitleMixin } from "./panel-title-mixin"; @@ -26,6 +27,7 @@ export class HassElement extends ext(HassBaseEl, [ TranslationsMixin, StateDisplayMixin, MoreInfoMixin, + AutomationEditorMixin, ActionMixin, SidebarMixin, DisconnectToastMixin,