UNPKG

node-red-contrib-smartnora

Version:

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

199 lines (198 loc) 9.51 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-light', function (config) { var _a, _b, _c, _d; RED.nodes.createNode(this, config); const noraConfig = RED.nodes.getNode(config.nora); if (!(noraConfig === null || noraConfig === void 0 ? void 0 : noraConfig.valid)) { return; } const brightnessControl = !!config.brightnesscontrol; const statepayload = !!config.statepayload; const colorControl = !!config.lightcolor; const turnOnWhenBrightnessChanges = !!config.turnonwhenbrightnesschanges; const { value: onValue, type: onType } = (0, util_1.convertValueType)(RED, config.onvalue, config.onvalueType, { defaultValue: true }); const { value: offValue, type: offType } = (0, util_1.convertValueType)(RED, config.offvalue, config.offvalueType, { defaultValue: false }); const brightnessOverride = Math.max(0, Math.min(100, Math.round(config.brightnessoverride))) || 0; const deviceConfig = { type: 'action.devices.types.LIGHT', traits: ['action.devices.traits.OnOff'], name: { name: config.devicename, }, roomHint: config.roomhint, willReportState: true, state: { online: true, on: false, }, noraSpecific: { returnOnOffErrorCodeIfStateAlreadySet: !!config.errorifstateunchaged, }, attributes: {}, }; if (brightnessControl) { deviceConfig.traits.push('action.devices.traits.Brightness'); if ((0, nora_firebase_common_1.isBrightness)(deviceConfig)) { deviceConfig.state.brightness = 100; deviceConfig.noraSpecific.turnOnWhenBrightnessChanges = turnOnWhenBrightnessChanges; } } const colorType = (_a = config.colortype) !== null && _a !== void 0 ? _a : 'hsv'; if (!['rgb', 'hsv', 'temperature'].includes(colorType)) { this.warn(`Invalid color type ${colorType}`); return; } if (colorControl) { deviceConfig.traits.push('action.devices.traits.ColorSetting'); if (!(0, nora_firebase_common_1.isColorSetting)(deviceConfig)) { this.warn('Unable to add ColorSetting trait'); return; } deviceConfig.noraSpecific.turnOnWhenColorChanges = turnOnWhenBrightnessChanges; switch (colorType) { case 'hsv': deviceConfig.attributes = { commandOnlyColorSetting: (_b = config.commandonlycolor) !== null && _b !== void 0 ? _b : false, colorModel: 'hsv', }; deviceConfig.state.color = { spectrumHsv: { hue: 0, saturation: 0, value: 1, }, }; break; case 'rgb': deviceConfig.attributes = { commandOnlyColorSetting: (_c = config.commandonlycolor) !== null && _c !== void 0 ? _c : false, colorModel: 'rgb', }; deviceConfig.state.color = { spectrumRgb: 0, }; break; case 'temperature': { const tempMin = (0, util_1.getNumberOrDefault)(config.temperaturemin, 2700); const tempMax = (0, util_1.getNumberOrDefault)(config.temperaturemax, 5500); deviceConfig.attributes = { commandOnlyColorSetting: (_d = config.commandonlycolor) !== null && _d !== void 0 ? _d : false, colorTemperatureRange: { temperatureMinK: tempMin, temperatureMaxK: tempMax, }, }; deviceConfig.state.color = { temperatureK: tempMin, }; break; } } } (0, util_1.registerNoraDevice)(this, RED, config, { deviceConfig, updateStatus: ({ state, update }) => { var _a, _b; const statuses = [ state.on ? 'on' : 'off' ]; if (isBrightnessState(deviceConfig, state)) { statuses.push(`${state.brightness}`); } if (isHsvColor(deviceConfig, state) && 'spectrumHsv' in state.color) { const hue = state.color.spectrumHsv.hue; const saturation = ((_a = state.color.spectrumHsv.saturation) !== null && _a !== void 0 ? _a : 0) * 100; const value = ((_b = state.color.spectrumHsv.value) !== null && _b !== void 0 ? _b : 0) * 100; statuses.push((0, util_1.R) `H:${hue}° S:${saturation}% V:${value}%`); } if (isRgbColor(deviceConfig, state) && 'spectrumRgb' in state.color) { const rgbColor = `#${state.color.spectrumRgb.toString(16).padStart(6, '0')}`; statuses.push(`${rgbColor}`); } if (isTemperatureColor(deviceConfig, state) && 'temperatureK' in state.color) { statuses.push(`${state.color.temperatureK}K`); } update(statuses.join(' ')); }, mapStateToOutput: state => { if (!brightnessControl && !colorControl) { const value = state.on; return { payload: (0, util_1.getValue)(RED, this, value ? onValue : offValue, value ? onType : offType), }; } else { if (statepayload || colorControl) { return { payload: Object.assign({}, state), }; } else { return { payload: state.on && isBrightnessState(deviceConfig, state) ? state.brightness : 0, }; } } }, handleNodeInput: async ({ msg, updateState }) => { if (!brightnessControl && !colorControl) { const myOnValue = (0, util_1.getValue)(RED, this, onValue, onType); const myOffValue = (0, util_1.getValue)(RED, this, offValue, offType); if (RED.util.compareObjects(myOnValue, msg.payload)) { await updateState({ on: true }); } else if (RED.util.compareObjects(myOffValue, msg.payload)) { await updateState({ on: false }); } else { await updateState(msg === null || msg === void 0 ? void 0 : msg.payload); } return; } if (await updateState(msg === null || msg === void 0 ? void 0 : msg.payload)) { return; } const brightness = Math.max(0, Math.min(100, Math.round(msg.payload))); if (!isFinite(brightness)) { this.error('Payload must be a number in range 0-100'); return; } if (brightness === 0) { if (brightnessOverride !== 0) { await updateState({ on: false, brightness: brightnessOverride, }); } else { await updateState({ on: false, }); } } else { await updateState({ on: true, brightness: brightness, }); } }, }); function isBrightnessState(device, state) { return (0, nora_firebase_common_1.isBrightness)(device) && 'brightness' in state; } function isHsvColor(device, state) { return (0, nora_firebase_common_1.isColorSetting)(device) && 'colorModel' in device.attributes && device.attributes.colorModel === 'hsv' && (state === null || state === void 0 ? void 0 : state.color); } function isRgbColor(device, state) { return (0, nora_firebase_common_1.isColorSetting)(device) && 'colorModel' in device.attributes && device.attributes.colorModel === 'rgb' && (state === null || state === void 0 ? void 0 : state.color); } function isTemperatureColor(device, state) { return (0, nora_firebase_common_1.isColorSetting)(device) && 'colorTemperatureRange' in device.attributes && (state === null || state === void 0 ? void 0 : state.color); } }); };