UNPKG

node-red-contrib-smartnora

Version:

Google Smart Home integration via Smart Nora https://smart-nora.eu/

84 lines (83 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const nora_firebase_common_1 = require("@andrei-tatar/nora-firebase-common"); const util_1 = require("./util"); module.exports = function (RED) { RED.nodes.registerType('noraf-security', function (config) { var _a; RED.nodes.createNode(this, config); const armLevels = config.armLevels; const deviceConfig = { type: 'action.devices.types.SECURITYSYSTEM', traits: ['action.devices.traits.ArmDisarm'], name: { name: config.devicename, }, roomHint: config.roomhint, willReportState: true, noraSpecific: { asyncCommandExecution: !!config.asyncCmd, returnArmDisarmErrorCodeIfStateAlreadySet: !!config.errorifstateunchaged, }, state: { online: true, currentArmLevel: (_a = armLevels === null || armLevels === void 0 ? void 0 : armLevels[0]) === null || _a === void 0 ? void 0 : _a.v, isArmed: false, }, attributes: { availableArmLevels: armLevels && (armLevels === null || armLevels === void 0 ? void 0 : armLevels.length) > 0 ? { levels: armLevels.map(({ v, n }) => ({ level_name: v, level_values: [{ level_synonym: n.split(',').map(p => p.trim()), lang: config.language, }], })), ordered: true, } : undefined, }, }; if (config.supportStatusReport) { deviceConfig.traits.push('action.devices.traits.StatusReport'); if ((0, nora_firebase_common_1.isStatusReportDevice)(deviceConfig)) { const statusReportState = { currentStatusReport: [], }; Object.assign(deviceConfig.state, statusReportState); } } (0, util_1.registerNoraDevice)(this, RED, config, { deviceConfig, updateStatus: ({ state, update }) => { const statuses = []; if (state.isArmed) { statuses.push('armed'); if (state.currentArmLevel) { statuses.push(state.currentArmLevel); } } else { statuses.push('disarmed'); } if (typeof state.exitAllowance === 'number') { statuses.push(`${state.exitAllowance} sec`); } update(statuses.join(',')); }, mapStateToOutput: (state) => ({ payload: state, }), handleNodeInput: async ({ msg, updateState }) => { var _a, _b; if (((_b = (_a = msg === null || msg === void 0 ? void 0 : msg.payload) === null || _a === void 0 ? void 0 : _a.currentArmLevel) !== null && _b !== void 0 ? _b : undefined) !== undefined && !(armLevels === null || armLevels === void 0 ? void 0 : armLevels.find(s => s.v === msg.payload.currentArmLevel))) { this.warn(`invalid arm level: ${msg.payload.currentArmLevel}`); return; } await updateState(msg === null || msg === void 0 ? void 0 : msg.payload); }, }); }); };