Skip to content

Commit

Permalink
feat: call variables in the JavaScript template (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosabot authored Oct 11, 2024
1 parent a681a15 commit 5e26391
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/evaluateJavascript-helper.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const getPrefixFromHass = (hass) => {
const getPrefixFromHass = (hass, variables) => {
const states = hass?.states ?? undefined;
const user = hass?.user ?? undefined;
return `
var states = ${JSON.stringify(states)};
var user = ${JSON.stringify(user)};
var variables = ${JSON.stringify(variables)};
`;
};

// eslint-disable-next-line no-eval
const doEval = (string) => eval(string);

const evaluateJavascript = (config, hass) => {
const evaluateJavascript = (config, hass, variables = {}) => {
let prefix = undefined;
const configKeys = Object.keys(config);

Expand All @@ -22,7 +23,7 @@ const evaluateJavascript = (config, hass) => {
evaluateJavascript(config[key][index], hass);
} else if (key.endsWith("_javascript")) {
if (prefix === undefined) {
prefix = getPrefixFromHass(hass);
prefix = getPrefixFromHass(hass, variables);
}

const keyWithoutJavascript = key.replace("_javascript", "");
Expand All @@ -48,7 +49,7 @@ const evaluateJavascript = (config, hass) => {
evaluateJavascript(config[key], hass);
} else if (key.endsWith("_javascript")) {
if (prefix === undefined) {
prefix = getPrefixFromHass(hass);
prefix = getPrefixFromHass(hass, variables);
}

const keyWithoutJavascript = key.replace("_javascript", "");
Expand Down
14 changes: 14 additions & 0 deletions src/evaluateJavascript-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,18 @@ describe("Given the evaluateConfig function", () => {
expect(() => evaluateConfig(config)).toThrowError();
});
});

describe("When passing variables", () => {
it("should evaluate the javascript with the variables", () => {
const config = {
saber_count_javascript:
"`${variables.saber_count + variables.saber_count}`",
};

evaluateConfig(config, hass, { saber_count: 7 });
expect(config).toEqual({
saber_count: "14",
});
});
});
});
8 changes: 7 additions & 1 deletion src/evaluteConfig-helper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import evaluateJavascript from "./evaluateJavascript-helper";
import evaluateVariables from "./evaluateVariables-helper";
import formatVariables from "./formatVariables-helper";

export default function evaluateConfig(templateConfig, variables, options) {
let config = evaluateVariables(templateConfig, variables ?? {});

const { hasJavascript, hass } = options;

if (hasJavascript && typeof hass !== "undefined") {
config = evaluateJavascript(config, hass);
const allVariables = {
...formatVariables(templateConfig.default ?? {}),
...formatVariables(variables ?? {}),
};

config = evaluateJavascript(config, hass, allVariables);
}

return config;
Expand Down

0 comments on commit 5e26391

Please sign in to comment.