diff --git a/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts index 78b18d7230..585de1f5f2 100644 --- a/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts @@ -49,6 +49,7 @@ import { SubElementEditorConfig, } from "../types"; import { configElementStyle } from "./config-elements-style"; +import { buttonEntityConfigStruct } from "../structs/button-entity-struct"; const buttonEntitiesRowConfigStruct = object({ type: literal("button"), @@ -112,22 +113,7 @@ const webLinkEntitiesRowConfigStruct = object({ const buttonsEntitiesRowConfigStruct = object({ type: literal("buttons"), - entities: array( - union([ - object({ - entity: string(), - name: optional(string()), - icon: optional(string()), - image: optional(string()), - show_name: optional(boolean()), - show_icon: optional(boolean()), - tap_action: optional(actionConfigStruct), - hold_action: optional(actionConfigStruct), - double_tap_action: optional(actionConfigStruct), - }), - string(), - ]) - ), + entities: array(buttonEntityConfigStruct), }); const attributeEntitiesRowConfigStruct = object({ diff --git a/src/panels/lovelace/editor/structs/button-entity-struct.ts b/src/panels/lovelace/editor/structs/button-entity-struct.ts new file mode 100644 index 0000000000..8a80b89884 --- /dev/null +++ b/src/panels/lovelace/editor/structs/button-entity-struct.ts @@ -0,0 +1,14 @@ +import { boolean, object, optional, string } from "superstruct"; +import { actionConfigStruct } from "./action-struct"; + +export const buttonEntityConfigStruct = object({ + entity: string(), + name: optional(string()), + icon: optional(string()), + image: optional(string()), + show_name: optional(boolean()), + show_icon: optional(boolean()), + tap_action: optional(actionConfigStruct), + hold_action: optional(actionConfigStruct), + double_tap_action: optional(actionConfigStruct), +}); diff --git a/src/panels/lovelace/header-footer/structs.ts b/src/panels/lovelace/header-footer/structs.ts index 2c275c63c9..f913c8b32f 100644 --- a/src/panels/lovelace/header-footer/structs.ts +++ b/src/panels/lovelace/header-footer/structs.ts @@ -1,6 +1,15 @@ -import { object, string, optional, array, number, union } from "superstruct"; +import { + array, + dynamic, + number, + object, + optional, + union, + string, +} from "superstruct"; import { actionConfigStruct } from "../editor/structs/action-struct"; -import { entitiesConfigStruct } from "../editor/structs/entities-struct"; +import { buttonEntityConfigStruct } from "../editor/structs/button-entity-struct"; +import { LovelaceHeaderFooterConfig } from "./types"; export const pictureHeaderFooterConfigStruct = object({ type: string(), @@ -12,7 +21,7 @@ export const pictureHeaderFooterConfigStruct = object({ export const buttonsHeaderFooterConfigStruct = object({ type: string(), - entities: array(entitiesConfigStruct), + entities: array(buttonEntityConfigStruct), }); export const graphHeaderFooterConfigStruct = object({ @@ -22,8 +31,25 @@ export const graphHeaderFooterConfigStruct = object({ hours_to_show: optional(number()), }); -export const headerFooterConfigStructs = union([ - pictureHeaderFooterConfigStruct, - buttonsHeaderFooterConfigStruct, - graphHeaderFooterConfigStruct, -]); +export const headerFooterConfigStructs = dynamic((value) => { + if (value && typeof value === "object" && "type" in value) { + switch ((value as LovelaceHeaderFooterConfig).type!) { + case "buttons": { + return buttonsHeaderFooterConfigStruct; + } + case "graph": { + return graphHeaderFooterConfigStruct; + } + case "picture": { + return pictureHeaderFooterConfigStruct; + } + } + } + + // No "type" property => we fallback to a union of all potential types + return union([ + buttonsHeaderFooterConfigStruct, + graphHeaderFooterConfigStruct, + pictureHeaderFooterConfigStruct, + ]); +});