diff --git a/src/data/lovelace.ts b/src/data/lovelace.ts index aeabf1a05d..28d1c4886f 100644 --- a/src/data/lovelace.ts +++ b/src/data/lovelace.ts @@ -159,6 +159,7 @@ export interface CustomActionConfig extends BaseActionConfig { } export interface BaseActionConfig { + action: string; confirmation?: ConfirmationRestrictionConfig; } diff --git a/src/panels/lovelace/editor/structs/action-struct.ts b/src/panels/lovelace/editor/structs/action-struct.ts index ef7a479bb2..e8a6ec82be 100644 --- a/src/panels/lovelace/editor/structs/action-struct.ts +++ b/src/panels/lovelace/editor/structs/action-struct.ts @@ -1,14 +1,16 @@ import { - object, - string, - union, - boolean, - optional, array, - literal, + boolean, + dynamic, enums, + literal, + object, + optional, + string, type, + union, } from "superstruct"; +import { BaseActionConfig } from "../../../../data/lovelace"; const actionConfigStructUser = object({ user: string(), @@ -65,10 +67,23 @@ export const actionConfigStructType = object({ confirmation: optional(actionConfigStructConfirmation), }); -export const actionConfigStruct = union([ - actionConfigStructType, - actionConfigStructUrl, - actionConfigStructNavigate, - actionConfigStructService, - actionConfigStructCustom, -]); +export const actionConfigStruct = dynamic((value) => { + if (value && typeof value === "object" && "action" in value) { + switch ((value as BaseActionConfig).action!) { + case "call-service": { + return actionConfigStructService; + } + case "fire-dom-event": { + return actionConfigStructCustom; + } + case "navigate": { + return actionConfigStructNavigate; + } + case "url": { + return actionConfigStructUrl; + } + } + } + + return actionConfigStructType; +});