UNPKG

node-red-contrib-smartnora

Version:

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

73 lines (72 loc) 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("./util"); module.exports = function (RED) { RED.nodes.registerType('noraf-garage', function (config) { RED.nodes.createNode(this, config); const noraConfig = RED.nodes.getNode(config.nora); if (!(noraConfig === null || noraConfig === void 0 ? void 0 : noraConfig.valid)) { return; } const { value: openValue, type: openType } = (0, util_1.convertValueType)(RED, config.openvalue, config.openvalueType, { defaultValue: true }); const { value: closeValue, type: closeType } = (0, util_1.convertValueType)(RED, config.closevalue, config.closevalueType, { defaultValue: false }); (0, util_1.registerNoraDevice)(this, RED, config, { deviceConfig: { type: 'action.devices.types.GARAGE', traits: ['action.devices.traits.OpenClose'], name: { name: config.devicename, }, roomHint: config.roomhint, willReportState: true, state: { online: true, openPercent: 0, }, noraSpecific: { returnOpenCloseErrorCodeIfStateAlreadySet: !!config.errorifstateunchaged, }, attributes: { discreteOnlyOpenClose: true, }, }, updateStatus: ({ state, update }) => { if ('openPercent' in state) { if (state.openPercent === 0) { update('(closed)'); } else { update('(open)'); } } }, mapStateToOutput: state => { if ('openPercent' in state) { if (state.openPercent === 0) { return { payload: (0, util_1.getValue)(RED, this, closeValue, closeType), }; } else { return { payload: (0, util_1.getValue)(RED, this, openValue, openType), }; } } }, handleNodeInput: async ({ msg, updateState }) => { const myOpenValue = (0, util_1.getValue)(RED, this, openValue, openType); const myCloseValue = (0, util_1.getValue)(RED, this, closeValue, closeType); if (RED.util.compareObjects(myOpenValue, msg.payload)) { await updateState({ openPercent: 100 }); } else if (RED.util.compareObjects(myCloseValue, msg.payload)) { await updateState({ openPercent: 0 }); } else { await updateState(msg.payload); } }, }); }); };