UNPKG

node-red-contrib-smartnora

Version:

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

73 lines (72 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const mapping_1 = require("./mapping"); const util_1 = require("./util"); module.exports = function (RED) { RED.nodes.registerType('noraf-airpurifier', 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 speeds = config.speeds; (0, util_1.registerNoraDevice)(this, RED, config, { deviceConfig: { type: 'action.devices.types.AIRPURIFIER', traits: ['action.devices.traits.OnOff', 'action.devices.traits.FanSpeed'], name: { name: config.devicename, }, roomHint: config.roomhint, willReportState: true, noraSpecific: {}, state: Object.assign({ on: false, online: true }, (config.percentcontrol ? { currentFanSpeedPercent: 100 } : { currentFanSpeedSetting: speeds[0].v })), attributes: Object.assign({}, (config.percentcontrol ? { supportsFanSpeedPercent: true, } : { supportsFanSpeedPercent: false, availableFanSpeeds: { speeds: speeds.map(s => ({ speed_name: s.v.trim(), speed_values: [{ speed_synonym: s.n.split(',').map(v => v.trim()), lang: config.language, }], })), ordered: true, }, })), }, updateStatus: ({ state, update }) => { const speed = 'currentFanSpeedPercent' in state ? `${state.currentFanSpeedPercent}%` : state.currentFanSpeedSetting; update(`${state.on ? 'on' : 'off'} - ${speed}`); }, mapStateToOutput: (state) => ({ payload: { on: state.on, speed: 'currentFanSpeedPercent' in state ? state.currentFanSpeedPercent : state.currentFanSpeedSetting, }, }), handleNodeInput: async ({ msg, updateState }) => { var _a, _b; if (!config.percentcontrol && ((_b = (_a = msg === null || msg === void 0 ? void 0 : msg.payload) === null || _a === void 0 ? void 0 : _a.speed) !== null && _b !== void 0 ? _b : undefined) !== undefined && !speeds.find(s => s.v === msg.payload.speed)) { this.warn(`invalid fan speed: ${msg.payload.speed}`); return; } await updateState(msg === null || msg === void 0 ? void 0 : msg.payload, [ ...(0, mapping_1.FAN_STATE_MAPPING)(config.percentcontrol), ]); }, }); }); };