Align has template functions (#8784)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Bram Kragten 2021-04-01 22:49:59 +02:00 committed by GitHub
parent deca6f03ba
commit 8e3a7576ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 9 deletions

View File

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

View File

@ -1,6 +1,7 @@
import secondsToDuration from "../common/datetime/seconds_to_duration";
import { ensureArray } from "../common/ensure-array";
import { computeStateName } from "../common/entity/compute_state_name";
import { isTemplate } from "../common/string/has-template";
import { HomeAssistant } from "../types";
import { Condition } from "./automation";
import { describeCondition, describeTrigger } from "./automation_i18n";
@ -14,7 +15,6 @@ import {
VariablesAction,
EventAction,
} from "./script";
import { isDynamicTemplate } from "./template";
export const describeAction = <T extends ActionType>(
hass: HomeAssistant,
@ -35,7 +35,7 @@ export const describeAction = <T extends ActionType>(
if (
config.service_template ||
(config.service && isDynamicTemplate(config.service))
(config.service && isTemplate(config.service))
) {
base = "Call a service based on a template";
} else if (config.service) {
@ -63,7 +63,7 @@ export const describeAction = <T extends ActionType>(
let renderValues = true;
for (const targetThing of keyConf) {
if (isDynamicTemplate(targetThing)) {
if (isTemplate(targetThing)) {
targets.push(`templated ${label}`);
renderValues = false;
break;
@ -92,7 +92,7 @@ export const describeAction = <T extends ActionType>(
if (typeof config.delay === "number") {
duration = `for ${secondsToDuration(config.delay)!}`;
} else if (typeof config.delay === "string") {
duration = isDynamicTemplate(config.delay)
duration = isTemplate(config.delay)
? "based on a template"
: `for ${config.delay}`;
} else {
@ -124,7 +124,7 @@ export const describeAction = <T extends ActionType>(
if (actionType === "fire_event") {
const config = action as EventAction;
if (isDynamicTemplate(config.event)) {
if (isTemplate(config.event)) {
return "Fire event based on a template";
}
return `Fire event ${config.event}`;

View File

@ -1 +0,0 @@
export const isDynamicTemplate = (value: string) => value.includes("{{");

View File

@ -286,7 +286,7 @@ class HaPanelDevService extends LitElement {
}
private _checkUiSupported() {
if (hasTemplate(this._serviceData)) {
if (this._serviceData && hasTemplate(this._serviceData)) {
this._yamlMode = true;
this._uiAvailable = false;
} else {