From 7e62a26633dd7a7ea51f6b61e62be98649a2041b Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 29 Nov 2018 20:40:22 +0100 Subject: [PATCH 1/2] Add own types + add config validation to glances --- .../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 000000000000..b2bdfa5b6761 --- /dev/null +++ b/src/panels/lovelace/common/structs/is-entity-id.ts @@ -0,0 +1,9 @@ +export const isEntityId = (value) => { + 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 000000000000..57db9290943b --- /dev/null +++ b/src/panels/lovelace/common/structs/is-icon.ts @@ -0,0 +1,9 @@ +export const isIcon = (value) => { + 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 000000000000..4a7f6c6c1965 --- /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": (value) => isEntityId(value), + icon: (value) => isIcon(value), + }, +}); 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 e51dd1b52619..a091070cb15c 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 0f6cc2fdfe2f..672b8b6775be 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); } From 3ad73df134d8f69df750ac6198f098624f5f2a4b Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 29 Nov 2018 20:58:42 +0100 Subject: [PATCH 2/2] Cleanup --- src/panels/lovelace/common/structs/is-entity-id.ts | 4 ++-- src/panels/lovelace/common/structs/is-icon.ts | 4 ++-- src/panels/lovelace/common/structs/struct.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/panels/lovelace/common/structs/is-entity-id.ts b/src/panels/lovelace/common/structs/is-entity-id.ts index b2bdfa5b6761..da04c298a888 100644 --- a/src/panels/lovelace/common/structs/is-entity-id.ts +++ b/src/panels/lovelace/common/structs/is-entity-id.ts @@ -1,4 +1,4 @@ -export const isEntityId = (value) => { +export function isEntityId(value: any): string | boolean { if (typeof value !== "string") { return "entity id should be a string"; } @@ -6,4 +6,4 @@ export const isEntityId = (value) => { 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 index 57db9290943b..30addc012e4f 100644 --- a/src/panels/lovelace/common/structs/is-icon.ts +++ b/src/panels/lovelace/common/structs/is-icon.ts @@ -1,4 +1,4 @@ -export const isIcon = (value) => { +export function isIcon(value: any): string | boolean { if (typeof value !== "string") { return "icon should be a string"; } @@ -6,4 +6,4 @@ export const isIcon = (value) => { 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 index 4a7f6c6c1965..9ce805975c7b 100644 --- a/src/panels/lovelace/common/structs/struct.ts +++ b/src/panels/lovelace/common/structs/struct.ts @@ -4,7 +4,7 @@ import { isIcon } from "./is-icon"; export const struct = superstruct({ types: { - "entity-id": (value) => isEntityId(value), - icon: (value) => isIcon(value), + "entity-id": isEntityId, + icon: isIcon, }, });