mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 11:46:42 +00:00
Add own types + add config validation to glances (#2150)
* Add own types + add config validation to glances * Cleanup
This commit is contained in:
parent
882c503fa9
commit
0e6f6ddbda
9
src/panels/lovelace/common/structs/is-entity-id.ts
Normal file
9
src/panels/lovelace/common/structs/is-entity-id.ts
Normal 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;
|
||||||
|
}
|
9
src/panels/lovelace/common/structs/is-icon.ts
Normal file
9
src/panels/lovelace/common/structs/is-icon.ts
Normal 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;
|
||||||
|
}
|
10
src/panels/lovelace/common/structs/struct.ts
Normal file
10
src/panels/lovelace/common/structs/struct.ts
Normal 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,
|
||||||
|
},
|
||||||
|
});
|
@ -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({
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user