UNPKG

node-red-contrib-smartnora

Version:

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

86 lines (85 loc) 3.56 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-vacuum', 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.VACUUM', name: { name: config.devicename }, traits: ['action.devices.traits.StartStop'], roomHint: config.roomhint, state: { online: true, isRunning: false }, attributes: { pausable: config.startStopPausable }, willReportState: true, noraSpecific: {} }; if (config.onOffSupported) { deviceConfig.traits.push('action.devices.traits.OnOff'); if ((0, nora_firebase_common_1.isOnOff)(deviceConfig)) { const onOffAttributes = { commandOnlyOnOff: (config.onOffMode === 'c'), queryOnlyOnOff: (config.onOffMode === 'q') }; deviceConfig.attributes = Object.assign(Object.assign({}, deviceConfig.attributes), onOffAttributes); const onOffState = { on: false, }; deviceConfig.state = Object.assign(Object.assign({}, deviceConfig.state), onOffState); } ; } if (config.dockSupported) { deviceConfig.traits.push('action.devices.traits.Dock'); if ((0, nora_firebase_common_1.isDockDevice)(deviceConfig)) { const dockState = { isDocked: true, }; deviceConfig.state = Object.assign(Object.assign({}, deviceConfig.state), dockState); const dockNoraSpecific = { returnDockErrorCodeIfAlreadyDocked: true, }; deviceConfig.noraSpecific = Object.assign(Object.assign({}, deviceConfig.noraSpecific), dockNoraSpecific); } ; } (0, util_1.registerNoraDevice)(this, RED, config, { deviceConfig, updateStatus: ({ state, update }) => { const statuses = []; if (isOnOffState(state)) { statuses.push(state.on ? 'on' : 'off'); } statuses.push(state.isRunning ? (state.isPaused ? 'paused' : 'running') : 'not running'); if (isDockState(state) && state.isDocked) { statuses.push('docked'); } update(statuses.join(' ')); }, mapStateToOutput: state => ({ payload: state, }), handleNodeInput: async ({ msg, updateState }) => { await updateState(msg === null || msg === void 0 ? void 0 : msg.payload, []); } }); function isOnOffState(state) { return (0, nora_firebase_common_1.isOnOff)(deviceConfig) && 'on' in state; } function isDockState(state) { return (0, nora_firebase_common_1.isDockDevice)(deviceConfig) && 'isDocked' in state; } }); };