Add own types + add config validation to glances (#2150)

* Add own types + add config validation to glances

* Cleanup
This commit is contained in:
Bram Kragten 2018-11-30 19:35:55 +01:00 committed by Zack Arnett
parent 882c503fa9
commit 0e6f6ddbda
5 changed files with 55 additions and 4 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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,
},
});

View File

@ -1,6 +1,6 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import { TemplateResult } from "lit-html"; 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-dropdown-menu/paper-dropdown-menu";
import "@polymer/paper-item/paper-item"; import "@polymer/paper-item/paper-item";
import "@polymer/paper-listbox/paper-listbox"; import "@polymer/paper-listbox/paper-listbox";
@ -24,11 +24,11 @@ import "../../../../components/ha-icon";
const entitiesConfigStruct = struct.union([ const entitiesConfigStruct = struct.union([
{ {
entity: "string", entity: "entity-id",
name: "string?", name: "string?",
icon: "string?", icon: "icon?",
}, },
"string", "entity-id",
]); ]);
const cardConfigStruct = struct({ const cardConfigStruct = struct({

View File

@ -1,5 +1,6 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import { TemplateResult } from "lit-html"; import { TemplateResult } from "lit-html";
import { struct } from "../../common/structs/struct";
import "@polymer/paper-dropdown-menu/paper-dropdown-menu"; import "@polymer/paper-dropdown-menu/paper-dropdown-menu";
import "@polymer/paper-item/paper-item"; import "@polymer/paper-item/paper-item";
import "@polymer/paper-listbox/paper-listbox"; import "@polymer/paper-listbox/paper-listbox";
@ -20,6 +21,26 @@ import "../../components/hui-entity-editor";
import "../../../../components/ha-card"; import "../../../../components/ha-card";
import "../../../../components/ha-icon"; 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) export class HuiGlanceCardEditor extends hassLocalizeLitMixin(LitElement)
implements LovelaceCardEditor { implements LovelaceCardEditor {
public hass?: HomeAssistant; public hass?: HomeAssistant;
@ -27,6 +48,8 @@ export class HuiGlanceCardEditor extends hassLocalizeLitMixin(LitElement)
private _configEntities?: ConfigEntity[]; private _configEntities?: ConfigEntity[];
public setConfig(config: Config): void { public setConfig(config: Config): void {
config = cardConfigStruct(config);
this._config = { type: "glance", ...config }; this._config = { type: "glance", ...config };
this._configEntities = processEditorEntities(config.entities); this._configEntities = processEditorEntities(config.entities);
} }