How to use tuyapi - 10 common examples

To help you get started, we’ve selected a few tuyapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
// Import packages
const dgram = require('dgram');
const net = require('net');
const timeout = require('p-timeout');
const retry = require('retry');
const debug = require('debug')('TuyAPI-ext');

// Helpers
const Cipher = require('tuyapi/lib/cipher');
const Parser = require('tuyapi/lib/message-parser')

TuyaDevice.prototype.getDevice = function () {
    return this.device;
}

TuyaDevice.prototype.get = function (options) {
    // Set empty object as default
    options = options ? options : {};

    const payload = {
        gwId: this.device.id,
        devId: this.device.id
    };

    debug('Payload: ', payload);

    // Create byte buffer
    const buffer = Parser.encode({
        data: payload,
        commandByte: '0a'
    });
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
//             // eslint-disable-next-line max-len
//             err.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
//             reject(err);
//         });
//     });
// };

TuyaDevice.prototype.getStatus = function (callback) {
    var tuya = this;
    tuya.get().then(status => {
        debug('Current Status: ' + status);
        callback.call(this, status);
    });
}

TuyaDevice.prototype.setStatus = function (options, callback) {
    var tuya = this;
    tuya.set(options).then(result => {
        tuya.get().then(status => {
            debug('New status: ' + status);
            if (callback != undefined) {
                callback.call(this, status);
            } else {
                debug(status);
            }
            return;
        });
    });
}

TuyaDevice.prototype.toggle = function (callback) {
    var tuya = this;
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
const TuyaDevice = require('tuyapi');
const TuyaColor = require('./tuya-color');

// Import packages
const dgram = require('dgram');
const net = require('net');
const timeout = require('p-timeout');
const retry = require('retry');
const debug = require('debug')('TuyAPI-ext');

// Helpers
const Cipher = require('tuyapi/lib/cipher');
const Parser = require('tuyapi/lib/message-parser')

TuyaDevice.prototype.getDevice = function () {
    return this.device;
}

TuyaDevice.prototype.get = function (options) {
    // Set empty object as default
    options = options ? options : {};

    const payload = {
        gwId: this.device.id,
        devId: this.device.id
    };

    debug('Payload: ', payload);

    // Create byte buffer
    const buffer = Parser.encode({
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
//             resolve(data);
//         });

//         // Handle errors
//         client.on('error', err => {
//             debug('Error event from socket.');
//             client.destroy(); // kill client after server's response

//             // eslint-disable-next-line max-len
//             err.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
//             reject(err);
//         });
//     });
// };

TuyaDevice.prototype.getStatus = function (callback) {
    var tuya = this;
    tuya.get().then(status => {
        debug('Current Status: ' + status);
        callback.call(this, status);
    });
}

TuyaDevice.prototype.setStatus = function (options, callback) {
    var tuya = this;
    tuya.set(options).then(result => {
        tuya.get().then(status => {
            debug('New status: ' + status);
            if (callback != undefined) {
                callback.call(this, status);
            } else {
                debug(status);
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
debug('Payload:', payload);

    // Encrypt data
    const data = this.device.cipher.encrypt({
        data: JSON.stringify(payload)
    });

    // Create MD5 signature
    const md5 = this.device.cipher.md5('data=' + data +
        '||lpv=' + this.device.version +
        '||' + this.device.key);

    // Create byte buffer from hex data
    const thisData = Buffer.from(this.device.version + md5 + data);
    const buffer = Parser.encode({
        data: thisData,
        commandByte: '07'
    });

    // Send request to change status
    return new Promise((resolve, reject) => {
        this._send(this.device.ip, buffer).then(() => {
            resolve(true);
        }).catch(err => {
            reject(err);
        });
    });
};
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
});
    });
}

TuyaDevice.prototype.toggle = function (callback) {
    var tuya = this;
    //tuya.resolveId().then(() => {
        tuya.get().then(status => {
            tuya.setStatus({
                set: !status
            }, callback);
        });
    //});
}

TuyaDevice.prototype.onoff = function (newStatus, callback) {
    newStatus = newStatus.toLowerCase();
    debug("onoff: " + newStatus);
    if (newStatus == "on") {
        this.on(callback);
    }
    if (newStatus == "off") {
        this.off(callback);
    }
    if (newStatus == "toggle") {
        this.toggle(callback);
    }
}

TuyaDevice.prototype.setColor = function (hexColor, callback) {
    debug("Set color to", hexColor);
    var tuya = this;
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
}

TuyaDevice.prototype.setColor = function (hexColor, callback) {
    debug("Set color to", hexColor);
    var tuya = this;
    var color = new TuyaColor(tuya);
    var dps = color.setColor(hexColor);

    //tuya.resolveId().then(() => {
        tuya.get().then(status => {
            tuya.setStatus(dps, callback);
        });
    //});
}

TuyaDevice.prototype.on = function (callback) {
    var tuya = this;
    //tuya.resolveId().then(() => {
        tuya.get().then(status => {
            tuya.setStatus({
                set: true
            }, callback);
        });
    //});
}

TuyaDevice.prototype.off = function (callback) {
    debug("off: ");
    var tuya = this;
    //tuya.resolveId().then(() => {
        tuya.get().then(status => {
            tuya.setStatus({
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
TuyaDevice.prototype.onoff = function (newStatus, callback) {
    newStatus = newStatus.toLowerCase();
    debug("onoff: " + newStatus);
    if (newStatus == "on") {
        this.on(callback);
    }
    if (newStatus == "off") {
        this.off(callback);
    }
    if (newStatus == "toggle") {
        this.toggle(callback);
    }
}

TuyaDevice.prototype.setColor = function (hexColor, callback) {
    debug("Set color to", hexColor);
    var tuya = this;
    var color = new TuyaColor(tuya);
    var dps = color.setColor(hexColor);

    //tuya.resolveId().then(() => {
        tuya.get().then(status => {
            tuya.setStatus(dps, callback);
        });
    //});
}

TuyaDevice.prototype.on = function (callback) {
    var tuya = this;
    //tuya.resolveId().then(() => {
        tuya.get().then(status => {
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
} else if (options.dps) {
                resolve(dps[options.dps]);
            } else {
                if (dps != undefined && dps["1"] != undefined) {
                    resolve(dps['1']);
                } else {
                    resolve(dps);
                }
            }
        }).catch(err => {
            reject(err);
        });
    });
};

TuyaDevice.prototype.set = function (options) {
    let dps = {};
    var count = Object.keys(options).length;

    if (options.dps != undefined || options.set != undefined) {
        if (options.dps === undefined) {
            dps = {
                1: options.set
            };
        } else {
            dps = {
                [options.dps.toString()]: options.set
            };
        }
    } else {
        dps = options;
    }
github TheAgentK / tuya-mqtt / tuyaapi-extended.js View on Github external
TuyaDevice.prototype.setStatus = function (options, callback) {
    var tuya = this;
    tuya.set(options).then(result => {
        tuya.get().then(status => {
            debug('New status: ' + status);
            if (callback != undefined) {
                callback.call(this, status);
            } else {
                debug(status);
            }
            return;
        });
    });
}

TuyaDevice.prototype.toggle = function (callback) {
    var tuya = this;
    //tuya.resolveId().then(() => {
        tuya.get().then(status => {
            tuya.setStatus({
                set: !status
            }, callback);
        });
    //});
}

TuyaDevice.prototype.onoff = function (newStatus, callback) {
    newStatus = newStatus.toLowerCase();
    debug("onoff: " + newStatus);
    if (newStatus == "on") {
        this.on(callback);
    }

tuyapi

An easy-to-use API for devices that use Tuya's cloud services

MIT
Latest version published 2 years ago

Package Health Score

66 / 100
Full package analysis