Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/panels/lovelace/common/structs/is-entity-id.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions src/panels/lovelace/common/structs/is-icon.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions src/panels/lovelace/common/structs/struct.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -24,11 +24,11 @@ import "../../../../components/ha-icon";

const entitiesConfigStruct = struct.union([
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this union? I can imagine it will be a lot easier for us to just convert it to an object before validating it ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if that is easier, would rather validate before processing it. Otherwise, we will still have to manual check to see if entities is present and if it is an array etc...

If we can just add this to the struct, I think it is easier this way. But until the editor supports all the functions we have to create custom ones :-(

{
entity: "string",
entity: "entity-id",
name: "string?",
icon: "string?",
icon: "icon?",
},
"string",
"entity-id",
]);

const cardConfigStruct = struct({
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -20,13 +21,35 @@ 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;
private _config?: Config;
private _configEntities?: ConfigEntity[];

public setConfig(config: Config): void {
config = cardConfigStruct(config);

this._config = { type: "glance", ...config };
this._configEntities = processEditorEntities(config.entities);
}
Expand Down