UNPKG

node-red-contrib-home-assistant-websocket

Version:
122 lines (121 loc) 6.66 kB
"use strict"; 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 _ComparatorService_nodeRedContextService, _ComparatorService_homeAssistant, _ComparatorService_jsonataService, _ComparatorService_transformState; Object.defineProperty(exports, "__esModule", { value: true }); const selectn_1 = __importDefault(require("selectn")); const NodeRedContextService_1 = require("./NodeRedContextService"); class ComparatorService { constructor({ nodeRedContextService, homeAssistant, jsonataService, transformState, }) { _ComparatorService_nodeRedContextService.set(this, void 0); _ComparatorService_homeAssistant.set(this, void 0); _ComparatorService_jsonataService.set(this, void 0); _ComparatorService_transformState.set(this, void 0); __classPrivateFieldSet(this, _ComparatorService_nodeRedContextService, nodeRedContextService, "f"); __classPrivateFieldSet(this, _ComparatorService_homeAssistant, homeAssistant, "f"); __classPrivateFieldSet(this, _ComparatorService_jsonataService, jsonataService, "f"); __classPrivateFieldSet(this, _ComparatorService_transformState, transformState, "f"); } async getComparatorResult(comparatorType, // is, is not, less, greater, less or equal, greater or equal comparatorValue, // user entered value actualValue, // value to compare against, state comparatorValueDataType, // datatype of the comparator value, str, num, bool { message, entity, prevEntity, } = {}) { var _a; let cValue; if ((0, NodeRedContextService_1.isContextLocation)(comparatorValueDataType)) { cValue = __classPrivateFieldGet(this, _ComparatorService_nodeRedContextService, "f").get(comparatorValueDataType, comparatorValue, message); } else if (['entity', 'prevEntity'].includes(comparatorValueDataType)) { cValue = (0, selectn_1.default)(comparatorValue, comparatorValueDataType === 'entity' ? entity : prevEntity); } else if (comparatorType !== 'jsonata' && comparatorValueDataType === 'jsonata' && comparatorValue) { cValue = await __classPrivateFieldGet(this, _ComparatorService_jsonataService, "f").evaluate(comparatorValue, { message, entity, prevEntity, }); } else if (comparatorValueDataType === 'bool') { cValue = comparatorValue === 'true'; } else { if (comparatorType === 'includes' || comparatorType === 'does_not_include') { comparatorValueDataType = 'list'; } cValue = __classPrivateFieldGet(this, _ComparatorService_transformState, "f").transform(comparatorValueDataType, comparatorValue); } // TODO: Use migration service to convert old comparators to new comparators switch (comparatorType) { case 'is': case 'is_not': { // Datatype might be num, bool, str, re (regular expression) const isMatch = comparatorValueDataType === 're' ? cValue.test(actualValue) : cValue === actualValue; return comparatorType === 'is' ? isMatch : !isMatch; } case 'includes': case 'does_not_include': { const isIncluded = cValue.includes(actualValue); return comparatorType === 'includes' ? isIncluded : !isIncluded; } case 'cont': if (cValue === '') return false; return (actualValue + '').indexOf(cValue) !== -1; case 'greater_than': // here for backwards compatibility case '>': case 'gt': return actualValue > cValue; case '>=': case 'gte': return actualValue >= cValue; case 'less_than': // here for backwards compatibility case '<': case 'lt': return actualValue < cValue; case '<=': case 'lte': return actualValue <= cValue; case 'starts_with': if (cValue === '') return false; return actualValue === null || actualValue === void 0 ? void 0 : actualValue.startsWith(cValue); case 'in_group': { const ent = (_a = __classPrivateFieldGet(this, _ComparatorService_homeAssistant, "f")) === null || _a === void 0 ? void 0 : _a.websocket.getState(cValue); const groupEntities = (0, selectn_1.default)('attributes.entity_id', ent) || []; return groupEntities.includes(actualValue); } case 'jsonata': { if (!cValue) return true; const jsonataResult = await __classPrivateFieldGet(this, _ComparatorService_jsonataService, "f").evaluate(cValue, { message, entity, prevEntity, }); return jsonataResult === true; } default: return Boolean(actualValue); } } } _ComparatorService_nodeRedContextService = new WeakMap(), _ComparatorService_homeAssistant = new WeakMap(), _ComparatorService_jsonataService = new WeakMap(), _ComparatorService_transformState = new WeakMap(); exports.default = ComparatorService;