UNPKG

@ekristoffe/node-red-contrib-nature-remo

Version:
232 lines (213 loc) 10.1 kB
const NatureRemo = require("nature-remo"); module.exports = function (RED) { "use strict"; /***************************************************************/ function NatureCredentialsNode(config) { RED.nodes.createNode(this, config); this.name = config.name; this.token = config.token; } RED.nodes.registerType("nature-credentials", NatureCredentialsNode); /***************************************************************/ function NatureControl(config) { RED.nodes.createNode(this, config); let node = this; this.natureConfig = RED.nodes.getNode(config.natureCreds); const token = this.natureConfig.token; // Instantiate Nature Remo client with an API token const client = new NatureRemo.Cloud(token); let id = ""; let nickname = ""; let type = ""; // Get a list of the appliances client .getAppliances() .then(function (result) { result.forEach((appliance) => { if (appliance.nickname === config.nickname) { nickname = appliance.nickname; type = appliance.type; id = appliance.id; console.log({ id, nickname, type, }); } }); }) .catch((reason) => { // Log the rejection reason node.error(`Handle rejected promise (${reason}) here.`); }); this.on("input", function (msg) { // Get a list of the appliances client .getAppliances() .then(function (result) { result.forEach((appliance) => { if (appliance.nickname === config.nickname) { if (type.toUpperCase === "LIGHT") { // Toggle light switch if ( msg.payload === 0 || msg.payload === "0" || msg.payload === "off" ) { // Turn off light client .updateLight(appliance.id, "off") .then(function (result) { msg.payload = result; node.send(msg); }) .catch((reason) => { // Log the rejection reason node.error( `Handle rejected promise (${reason}) here.`, ); }); } else if ( msg.payload === 1 || msg.payload === "1" || msg.payload === "on" ) { // Turn on light client .updateLight(appliance.id, "on") .then(function (result) { msg.payload = result; node.send(msg); }) .catch((reason) => { // Log the rejection reason node.error( `Handle rejected promise (${reason}) here.`, ); }); } } else if (type.toUpperCase === "AC") { // Toggle air conditioner's switch if ( msg.payload === 0 || msg.payload === "0" || msg.payload === "off" ) { // Turn off aircon client .updateAirconSettings(appliance.id, { button: "power-off", }) .then(function (result) { msg.payload = result; node.send(msg); }) .catch((reason) => { // Log the rejection reason node.error( `Handle rejected promise (${reason}) here.`, ); }); } else if ( msg.payload === 1 || msg.payload === "1" || msg.payload === "on" ) { // Turn on aircon with specific operation mode client .updateAirconSettings(appliance.id, { operation_mode: appliance.settings.mode, }) .then(function (result) { msg.payload = result; node.send(msg); }) .catch((reason) => { // Log the rejection reason node.error( `Handle rejected promise (${reason}) here.`, ); }); } } } }); }) .catch((reason) => { // Log the rejection reason node.error(`Handle rejected promise (${reason}) here.`); }); }); } RED.nodes.registerType("nature-control", NatureControl); /***************************************************************/ function NatureGetAppliances(config) { RED.nodes.createNode(this, config); let node = this; this.on("input", function (msg) { this.natureConfig = RED.nodes.getNode(config.natureCreds); const token = this.natureConfig.token; // Instantiate Nature Remo client with an API token const client = new NatureRemo.Cloud(token); // Get a list of the appliances client .getAppliances() .then(function (result) { result.forEach((appliance) => { const { id, nickname, type } = appliance; console.log({ id, nickname, type, }); }); msg.payload = result; node.send(msg); }) .catch((reason) => { // Log the rejection reason node.error(`Handle rejected promise (${reason}) here.`); }); }); } RED.nodes.registerType("nature-get-appliances", NatureGetAppliances); /***************************************************************/ function NatureGetDevices(config) { RED.nodes.createNode(this, config); let node = this; this.on("input", function (msg) { this.natureConfig = RED.nodes.getNode(config.natureCreds); const token = this.natureConfig.token; // Instantiate Nature Remo client with an API token const client = new NatureRemo.Cloud(token); // Get a list of the devices client .getDevices() .then(function (result) { // Print a value of each sensors result.forEach((device) => { console.log({ name: device.name, version: device.firmware_version, temperature: device.newest_events.te.val, humidity: device.newest_events.hu.val, illumination: device.newest_events.il.val, offsets: { temperature: device.temperature_offset, humidity: device.humidity_offset, }, }); }); msg.payload = result; node.send(msg); }) .catch((reason) => { // Log the rejection reason node.error(`Handle rejected promise (${reason}) here.`); }); }); } RED.nodes.registerType("nature-get-devices", NatureGetDevices); /***************************************************************/ };