UNPKG

node-red-contrib-smartnora

Version:

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

94 lines (93 loc) 4.7 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-ac', 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 availableModes = config.modes.split(','); const speeds = config.speeds; (0, util_1.registerNoraDevice)(this, RED, config, { deviceConfig: { type: 'action.devices.types.AC_UNIT', traits: [ 'action.devices.traits.TemperatureSetting', 'action.devices.traits.FanSpeed', ], name: { name: config.devicename, }, roomHint: config.roomhint, willReportState: true, attributes: Object.assign({ availableThermostatModes: availableModes, thermostatTemperatureUnit: config.unit, bufferRangeCelsius: parseInt(config.bufferRangeCelsius, 10) || undefined, thermostatTemperatureRange: { minThresholdCelsius: parseInt(config.rangeMin, 10) || 10, maxThresholdCelsius: parseInt(config.rangeMax, 10) || 32, } }, (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, }, })), state: Object.assign({ online: true, thermostatMode: 'off', thermostatTemperatureAmbient: 25, thermostatTemperatureSetpoint: 20 }, (config.percentcontrol ? { currentFanSpeedPercent: 100 } : { currentFanSpeedSetting: speeds[0].v })), noraSpecific: {}, }, updateStatus: ({ state, update }) => { const setpoint = state.thermostatMode === 'heatcool' ? `${state.thermostatTemperatureSetpointLow}-${state.thermostatTemperatureSetpointHigh}` : `${state.thermostatTemperatureSetpoint}`; const speed = 'currentFanSpeedPercent' in state ? `${state.currentFanSpeedPercent}%` : state.currentFanSpeedSetting; const statuses = [ `${state.thermostatMode}`, `T:${state.thermostatTemperatureAmbient}`, `S:${setpoint}`, `F:${speed}`, ]; update(statuses.join('/')); }, mapStateToOutput: state => ({ payload: { mode: state.thermostatMode, activeMode: state.activeThermostatMode, setpoint: state.thermostatTemperatureSetpoint, setpointLow: state.thermostatTemperatureSetpointLow, setpointHigh: state.thermostatTemperatureSetpointHigh, 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, [ ...mapping_1.TEMPERATURE_SETTING_STATE_MAPPING, ...(0, mapping_1.FAN_STATE_MAPPING)(config.percentcontrol), ]); }, }); }); };