UNPKG

node-red-contrib-smartnora

Version:

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

89 lines (88 loc) 3.84 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-sprinkler', 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 deviceConfig = { type: 'action.devices.types.SPRINKLER', name: { name: config.devicename }, traits: [], roomHint: config.roomhint, state: { online: true, }, attributes: {}, willReportState: true, noraSpecific: { asyncCommandExecution: !!config.asyncCmd, }, }; if (config.startStopSupported) { deviceConfig.traits.push('action.devices.traits.StartStop'); if ((0, nora_firebase_common_1.isStartStopDevice)(deviceConfig)) { const startStopAttributes = { pausable: config.startStopPausable, availableZones: config.startStopZones }; deviceConfig.attributes = Object.assign(Object.assign({}, deviceConfig.attributes), startStopAttributes); const startStopState = { isRunning: false }; deviceConfig.state = Object.assign(Object.assign({}, deviceConfig.state), startStopState); } } if (config.timerSupported) { deviceConfig.traits.push('action.devices.traits.Timer'); if ((0, nora_firebase_common_1.isTimerDevice)(deviceConfig)) { const timerAttributes = { maxTimerLimitSec: parseInt(config.timerMaxLimitSeconds, 10) || 0, commandOnlyTimer: (config.timerMode === 'c' ? true : false) }; deviceConfig.attributes = Object.assign(Object.assign({}, deviceConfig.attributes), timerAttributes); const timerState = { timerRemainingSec: -1, timerPaused: false }; deviceConfig.state = Object.assign(Object.assign({}, deviceConfig.state), timerState); } } (0, util_1.registerNoraDevice)(this, RED, config, { deviceConfig, updateStatus: ({ state, update }) => { const statuses = []; if (isStartStopState(state)) { statuses.push(state.isRunning ? (state.isPaused ? 'paused' : 'running') : 'not running'); } if (isTimerState(state)) { if (state.timerRemainingSec !== -1) { statuses.push(`${state.timerRemainingSec}s`); } if (state.timerPaused) { statuses.push('paused'); } } update(statuses.join(' ')); }, mapStateToOutput: state => ({ payload: state, }), handleNodeInput: async ({ msg, updateState }) => { await updateState(msg === null || msg === void 0 ? void 0 : msg.payload, []); } }); function isStartStopState(state) { return (0, nora_firebase_common_1.isStartStopDevice)(deviceConfig) && 'isRunning' in state; } function isTimerState(state) { return (0, nora_firebase_common_1.isTimerDevice)(deviceConfig) && 'timerRemainingSec' in state; } }); };