UNPKG

node-red-contrib-home-assistant-websocket

Version:
103 lines (102 loc) 5.1 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 _TypedInputService_nodeConfig, _TypedInputService_context, _TypedInputService_jsonata; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const selectn_1 = __importDefault(require("selectn")); const const_1 = require("../../const"); class TypedInputService { constructor({ nodeConfig, context, jsonata, }) { _TypedInputService_nodeConfig.set(this, void 0); _TypedInputService_context.set(this, void 0); _TypedInputService_jsonata.set(this, void 0); __classPrivateFieldSet(this, _TypedInputService_nodeConfig, nodeConfig, "f"); __classPrivateFieldSet(this, _TypedInputService_context, context, "f"); __classPrivateFieldSet(this, _TypedInputService_jsonata, jsonata, "f"); } async getValue(value, valueType, props = {}) { let val; switch (valueType) { case const_1.TypedInputTypes.Message: case const_1.TypedInputTypes.Flow: case const_1.TypedInputTypes.Global: val = __classPrivateFieldGet(this, _TypedInputService_context, "f").get(valueType, value, props.message); break; case const_1.TypedInputTypes.Boolean: val = value === 'true'; break; case const_1.TypedInputTypes.JSON: try { val = JSON.parse(value); } catch (e) { // error parsing } break; case const_1.TypedInputTypes.Date: val = Date.now(); break; case const_1.TypedInputTypes.JSONata: // no reason to error just return undefined if (value === '') { val = undefined; break; } val = await __classPrivateFieldGet(this, _TypedInputService_jsonata, "f").evaluate(value, { data: props.data, entity: props.entity, entityId: props.entityId, eventData: props.eventData, message: props.message, prevEntity: props.prevEntity, results: props.results, }); break; case const_1.TypedInputTypes.Number: val = Number(value); break; case const_1.TypedInputTypes.None: val = undefined; break; case const_1.TypedInputTypes.Config: { val = (0, lodash_1.cloneDeep)(value.length ? (0, selectn_1.default)(value, __classPrivateFieldGet(this, _TypedInputService_nodeConfig, "f")) : __classPrivateFieldGet(this, _TypedInputService_nodeConfig, "f")); break; } case const_1.TypedInputTypes.Data: case const_1.TypedInputTypes.DeviceId: case const_1.TypedInputTypes.Entity: case const_1.TypedInputTypes.EntityState: case const_1.TypedInputTypes.EventData: case const_1.TypedInputTypes.Headers: case const_1.TypedInputTypes.Params: case const_1.TypedInputTypes.PrevEntity: case const_1.TypedInputTypes.PreviousValue: case const_1.TypedInputTypes.Results: case const_1.TypedInputTypes.TriggerId: case const_1.TypedInputTypes.Value: case const_1.TypedInputTypes.CalendarItem: val = props[valueType]; break; default: val = value; } return val; } } _TypedInputService_nodeConfig = new WeakMap(), _TypedInputService_context = new WeakMap(), _TypedInputService_jsonata = new WeakMap(); exports.default = TypedInputService;