Don't allow UI editor for service calls with templates (#8581)

This commit is contained in:
Bram Kragten 2021-03-07 21:02:44 +01:00
parent ba77a88714
commit d680fde759
No known key found for this signature in database
GPG Key ID: FBE2DFDB363EF55B
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,17 @@
const isTemplateRegex = new RegExp("{%|{{|{#");
export const isTemplate = (value: string): boolean =>
isTemplateRegex.test(value);
export const hasTemplate = (value: unknown): boolean => {
if (!value) {
return false;
}
if (typeof value === "string") {
return isTemplate(value);
}
if (typeof value === "object") {
const values = Array.isArray(value) ? value : Object.values(value!);
return values.some((val) => hasTemplate(val));
}
return false;
};

View File

@ -16,6 +16,7 @@ import type { HomeAssistant } from "../../../../../types";
import { EntityIdOrAll } from "../../../../../common/structs/is-entity-id";
import { ActionElement } from "../ha-automation-action-row";
import "../../../../../components/ha-service-control";
import { hasTemplate } from "../../../../../common/string/has-template";
const actionStruct = object({
service: optional(string()),
@ -46,6 +47,15 @@ export class HaServiceAction extends LitElement implements ActionElement {
assert(this.action, actionStruct);
} catch (error) {
fireEvent(this, "ui-mode-not-available", error);
return;
}
if (this.action && hasTemplate(this.action)) {
fireEvent(
this,
"ui-mode-not-available",
Error(this.hass.localize("ui.errors.config.no_template_editor_support"))
);
return;
}
if (this.action.entity_id) {
this._action = {