From 3d2e44f1c5991ebde9237c91c83091fc336a709e Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Thu, 26 May 2022 16:58:48 +0200 Subject: [PATCH] Dynamically determine the correct action config struct --- src/data/lovelace.ts | 1 + .../lovelace/editor/structs/action-struct.ts | 41 +++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/data/lovelace.ts b/src/data/lovelace.ts index aeabf1a05d62..28d1c4886fa5 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 ef7a479bb2e0..e8a6ec82be64 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; +});