Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce valid entity ID in card config YAML #14792

Merged
merged 2 commits into from
Dec 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/panels/lovelace/cards/hui-map-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
config.geo_location_sources &&
!Array.isArray(config.geo_location_sources)
) {
throw new Error("Geo_location_sources needs to be an array");
throw new Error("Parameter geo_location_sources needs to be an array");
}

this._config = config;
Expand Down
6 changes: 2 additions & 4 deletions src/panels/lovelace/common/process-config-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ export const processConfigEntities = <
config = { entity: entityConf } as T;
} else if (typeof entityConf === "object" && !Array.isArray(entityConf)) {
if (!("entity" in entityConf)) {
throw new Error(
`Entity object at position ${index} is missing entity field.`
);
throw new Error(`Object at position ${index} is missing entity field`);
}
config = entityConf as T;
} else {
throw new Error(`Invalid entity specified at position ${index}.`);
throw new Error(`Invalid entity ID at position ${index}`);
}

if (checkEntityId && !isValidEntityId((config as EntityConfig).entity!)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import "../../../../components/ha-form/ha-form";
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { array, assert, assign, object, optional, string } from "superstruct";
import memoizeOne from "memoize-one";
import { array, assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import type { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { AlarmPanelCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { LocalizeFunc } from "../../../../common/translations/localize";

const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
states: optional(array()),
theme: optional(string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { assert, assign, boolean, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain";
import { domainIcon } from "../../../../common/entity/domain_icon";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
Expand All @@ -18,7 +19,7 @@ import { configElementStyle } from "./config-elements-style";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
show_name: optional(boolean()),
icon: optional(string()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import "../../../../components/ha-form/ha-form";
import type { HassEntity } from "home-assistant-js-websocket/dist/types";
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { assert, assign, boolean, object, optional, string } from "superstruct";
import type { HassEntity } from "home-assistant-js-websocket/dist/types";
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain";
import { domainIcon } from "../../../../common/entity/domain_icon";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { EntityCardConfig } from "../../cards/types";
Expand All @@ -17,7 +18,7 @@ import { baseLovelaceCardConfig } from "../structs/base-card-struct";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
icon: optional(string()),
attribute: optional(string()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../../../../components/ha-form/ha-form";
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import {
array,
assert,
Expand All @@ -11,8 +11,9 @@ import {
optional,
string,
} from "superstruct";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { GaugeCardConfig } from "../../cards/types";
Expand All @@ -29,7 +30,7 @@ const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
name: optional(string()),
entity: optional(string()),
entity: optional(entityId()),
unit: optional(string()),
min: optional(number()),
max: optional(number()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "../../../../components/ha-form/ha-form";
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { HumidifierCardConfig } from "../../cards/types";
Expand All @@ -12,7 +13,7 @@ import { baseLovelaceCardConfig } from "../structs/base-card-struct";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
theme: optional(string()),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain";
import { domainIcon } from "../../../../common/entity/domain_icon";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
Expand All @@ -19,7 +20,7 @@ const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
name: optional(string()),
entity: optional(string()),
entity: optional(entityId()),
theme: optional(string()),
icon: optional(string()),
hold_action: optional(actionConfigStruct),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import "../../../../components/ha-form/ha-form";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import {
array,
assert,
assign,
boolean,
number,
object,
optional,
string,
assign,
} from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import { SchemaUnion } from "../../../../components/ha-form/types";
import "../../../../components/ha-formfield";
import "../../../../components/ha-switch";
import { PolymerChangedEvent } from "../../../../polymer-types";
Expand All @@ -22,11 +24,9 @@ import "../../components/hui-input-list-editor";
import { EntityConfig } from "../../entity-rows/types";
import { LovelaceCardEditor } from "../../types";
import { processEditorEntities } from "../process-editor-entities";
import { entitiesConfigStruct } from "../structs/entities-struct";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { EntitiesEditorEvent } from "../types";
import { configElementStyle } from "./config-elements-style";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { SchemaUnion } from "../../../../components/ha-form/types";

const cardConfigStruct = assign(
baseLovelaceCardConfig,
Expand All @@ -35,9 +35,10 @@ const cardConfigStruct = assign(
aspect_ratio: optional(string()),
default_zoom: optional(number()),
dark_mode: optional(boolean()),
entities: array(entitiesConfigStruct),
entities: array(entityId()),
hours_to_show: optional(number()),
geo_location_sources: optional(array(string())),
auto_fit: optional(boolean()),
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/entity/ha-entity-picker";
import "../../../../components/ha-theme-picker";
import { HomeAssistant } from "../../../../types";
Expand All @@ -13,7 +14,7 @@ import { EditorTarget, EntitiesEditorEvent } from "../types";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
theme: optional(string()),
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, boolean, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { PictureEntityCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { actionConfigStruct } from "../structs/action-struct";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { configElementStyle } from "./config-elements-style";
import "../../../../components/ha-form/ha-form";

const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
image: optional(string()),
name: optional(string()),
camera_image: optional(string()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "../../../../components/ha-form/ha-form";
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { PlantStatusCardConfig } from "../../cards/types";
Expand All @@ -12,7 +13,7 @@ import { baseLovelaceCardConfig } from "../structs/base-card-struct";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
theme: optional(string()),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "../../../../components/ha-form/ha-form";
import type { HassEntity } from "home-assistant-js-websocket";
import { CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
Expand All @@ -16,6 +15,8 @@ import {
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain";
import { domainIcon } from "../../../../common/entity/domain_icon";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { SensorCardConfig } from "../../cards/types";
Expand All @@ -26,7 +27,7 @@ import { configElementStyle } from "./config-elements-style";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
icon: optional(string()),
graph: optional(union([literal("line"), literal("none")])),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "../../../../components/ha-form/ha-form";
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { ThermostatCardConfig } from "../../cards/types";
Expand All @@ -12,7 +13,7 @@ import { baseLovelaceCardConfig } from "../structs/base-card-struct";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
theme: optional(string()),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain";
import { domainIcon } from "../../../../common/entity/domain_icon";
import { entityId } from "../../../../common/structs/is-entity-id";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
Expand All @@ -35,7 +36,7 @@ import "./hui-tile-card-features-editor";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
icon: optional(string()),
color: optional(string()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import "../../../../components/ha-form/ha-form";
import { memoize } from "@fullcalendar/common";
import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, boolean, object, optional, string, assign } from "superstruct";
import { memoize } from "@fullcalendar/common";
import { assert, assign, boolean, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { entityId } from "../../../../common/structs/is-entity-id";
import type { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import { UNAVAILABLE } from "../../../../data/entity";
import type { WeatherEntity } from "../../../../data/weather";
import type { HomeAssistant } from "../../../../types";
import type { WeatherForecastCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { actionConfigStruct } from "../structs/action-struct";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { UNAVAILABLE } from "../../../../data/entity";
import type { WeatherEntity } from "../../../../data/weather";
import type { LocalizeFunc } from "../../../../common/translations/localize";
import type { SchemaUnion } from "../../../../components/ha-form/types";

const cardConfigStruct = assign(
baseLovelaceCardConfig,
object({
entity: optional(string()),
entity: optional(entityId()),
name: optional(string()),
theme: optional(string()),
show_current: optional(boolean()),
Expand Down