From 0e6f6ddbdaaa187ebec89e5f234cf147e4de33a1 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 30 Nov 2018 19:35:55 +0100 Subject: [PATCH] Add own types + add config validation to glances (#2150) * Add own types + add config validation to glances * Cleanup --- .../lovelace/common/structs/is-entity-id.ts | 9 ++++++++ src/panels/lovelace/common/structs/is-icon.ts | 9 ++++++++ src/panels/lovelace/common/structs/struct.ts | 10 ++++++++ .../hui-entities-card-editor.ts | 8 +++---- .../config-elements/hui-glance-card-editor.ts | 23 +++++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/panels/lovelace/common/structs/is-entity-id.ts create mode 100644 src/panels/lovelace/common/structs/is-icon.ts create mode 100644 src/panels/lovelace/common/structs/struct.ts diff --git a/src/panels/lovelace/common/structs/is-entity-id.ts b/src/panels/lovelace/common/structs/is-entity-id.ts new file mode 100644 index 0000000000..da04c298a8 --- /dev/null +++ b/src/panels/lovelace/common/structs/is-entity-id.ts @@ -0,0 +1,9 @@ +export function isEntityId(value: any): string | boolean { + if (typeof value !== "string") { + return "entity id should be a string"; + } + if (!value.includes(".")) { + return "entity id should be in the format 'domain.entity'"; + } + return true; +} diff --git a/src/panels/lovelace/common/structs/is-icon.ts b/src/panels/lovelace/common/structs/is-icon.ts new file mode 100644 index 0000000000..30addc012e --- /dev/null +++ b/src/panels/lovelace/common/structs/is-icon.ts @@ -0,0 +1,9 @@ +export function isIcon(value: any): string | boolean { + if (typeof value !== "string") { + return "icon should be a string"; + } + if (!value.includes(":")) { + return "icon should be in the format 'mdi:icon'"; + } + return true; +} diff --git a/src/panels/lovelace/common/structs/struct.ts b/src/panels/lovelace/common/structs/struct.ts new file mode 100644 index 0000000000..9ce805975c --- /dev/null +++ b/src/panels/lovelace/common/structs/struct.ts @@ -0,0 +1,10 @@ +import { superstruct } from "superstruct"; +import { isEntityId } from "./is-entity-id"; +import { isIcon } from "./is-icon"; + +export const struct = superstruct({ + types: { + "entity-id": isEntityId, + icon: isIcon, + }, +}); 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 e51dd1b526..a091070cb1 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 @@ -1,6 +1,6 @@ import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; import { TemplateResult } from "lit-html"; -import { struct } from "superstruct"; +import { struct } from "../../common/structs/struct"; import "@polymer/paper-dropdown-menu/paper-dropdown-menu"; import "@polymer/paper-item/paper-item"; import "@polymer/paper-listbox/paper-listbox"; @@ -24,11 +24,11 @@ import "../../../../components/ha-icon"; const entitiesConfigStruct = struct.union([ { - entity: "string", + entity: "entity-id", name: "string?", - icon: "string?", + icon: "icon?", }, - "string", + "entity-id", ]); const cardConfigStruct = struct({ diff --git a/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts index 0f6cc2fdfe..672b8b6775 100644 --- a/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts @@ -1,5 +1,6 @@ import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; import { TemplateResult } from "lit-html"; +import { struct } from "../../common/structs/struct"; import "@polymer/paper-dropdown-menu/paper-dropdown-menu"; import "@polymer/paper-item/paper-item"; import "@polymer/paper-listbox/paper-listbox"; @@ -20,6 +21,26 @@ import "../../components/hui-entity-editor"; import "../../../../components/ha-card"; import "../../../../components/ha-icon"; +const entitiesConfigStruct = struct.union([ + { + entity: "entity-id", + name: "string?", + icon: "icon?", + }, + "entity-id", +]); + +const cardConfigStruct = struct({ + type: "string", + id: "string|number", + title: "string|number?", + theme: "string?", + columns: "number?", + show_name: "boolean?", + show_state: "boolean?", + entities: [entitiesConfigStruct], +}); + export class HuiGlanceCardEditor extends hassLocalizeLitMixin(LitElement) implements LovelaceCardEditor { public hass?: HomeAssistant; @@ -27,6 +48,8 @@ export class HuiGlanceCardEditor extends hassLocalizeLitMixin(LitElement) private _configEntities?: ConfigEntity[]; public setConfig(config: Config): void { + config = cardConfigStruct(config); + this._config = { type: "glance", ...config }; this._configEntities = processEditorEntities(config.entities); }