node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
101 lines (100 loc) • 4.96 kB
JavaScript
/*
* Modified from https://github.com/node-red/node-red/blob/master/nodes/core/core/80-template.js
*/
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _CustomContext_nodeContext, _CustomContext_entities;
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderTemplate = renderTemplate;
exports.generateRenderTemplate = generateRenderTemplate;
const mustache_1 = require("mustache");
const selectn_1 = __importDefault(require("selectn"));
const utils_1 = require("./utils");
function parseContext(key) {
const match = /^(flow|global)(\[(\w+)\])?\.(.+)/.exec(key);
if (match) {
const parts = {
type: match[1],
store: match[3] === '' ? 'default' : match[3],
field: match[4],
};
return parts;
}
return undefined;
}
/**
* Custom Mustache Context capable to collect message property and node
* flow and global context
*/
class CustomContext extends mustache_1.Context {
constructor(view, parentContext, nodeContext, entities) {
super(view, parentContext);
_CustomContext_nodeContext.set(this, void 0);
_CustomContext_entities.set(this, void 0);
__classPrivateFieldSet(this, _CustomContext_nodeContext, nodeContext, "f");
__classPrivateFieldSet(this, _CustomContext_entities, entities, "f");
}
lookup(name) {
var _a;
// try message first:
let value = super.lookup(name);
if (value === undefined) {
if (__classPrivateFieldGet(this, _CustomContext_nodeContext, "f")) {
// try flow/global context:
const context = parseContext(name);
if (context) {
const target = __classPrivateFieldGet(this, _CustomContext_nodeContext, "f")[context.type];
if (target) {
try {
value = target.get(context.field, context.store);
}
catch (err) { }
}
}
}
if (value === undefined && __classPrivateFieldGet(this, _CustomContext_entities, "f")) {
// try state entities
// version 0.10.3 changed from states.domain.entity to entity.d.e
const match = /^(?:states|entity)\.(\w+\.\w+)(?:\.(.+))?/.exec(name);
if (match) {
const entityId = match[1];
const path = (_a = match[2]) !== null && _a !== void 0 ? _a : 'state';
value = (0, selectn_1.default)(path, __classPrivateFieldGet(this, _CustomContext_entities, "f")[entityId]);
}
}
}
return value !== null && value !== void 0 ? value : '';
}
push(view) {
return new CustomContext(view, this, __classPrivateFieldGet(this, _CustomContext_nodeContext, "f"), __classPrivateFieldGet(this, _CustomContext_entities, "f"));
}
}
_CustomContext_nodeContext = new WeakMap(), _CustomContext_entities = new WeakMap();
function containsAltMustache(str) {
const regex = /<%(?:(?!%>).+)%>/g;
return regex.test(str);
}
function renderTemplate(str, message, nodeContext, entities, altTags = false) {
if (str &&
((altTags !== true && (0, utils_1.containsMustache)(str)) ||
(altTags === true && containsAltMustache(str)))) {
return (0, mustache_1.render)(str, new CustomContext(message, undefined, nodeContext, entities), undefined, altTags === true ? ['<%', '%>'] : undefined);
}
return str !== null && str !== void 0 ? str : '';
}
function generateRenderTemplate(message, context, states) {
return (template, altTags = false) => renderTemplate(template, message, context, states, altTags);
}
;