Skip to content

Commit b92f73c

Browse files
committed
Better error reporting, updated README
1 parent 96337f3 commit b92f73c

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

README.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,23 @@ Currently only supports smart plugs, but it should be fairly trivial to add othe
1111

1212
const TuyaDevice = require('tuyapi');
1313

14-
var tuya = new TuyaDevice({
15-
type: 'outlet',
16-
ip: 'xxx.yyy.0.zzz',
14+
let tuya = new TuyaDevice({
1715
id: 'xxxxxxxxxxxxxxxxxxxx',
1816
key: 'xxxxxxxxxxxxxxxx'});
1917

20-
tuya.getStatus(function(error, status) {
21-
if (error) { return console.log(error); }
22-
console.log('Status: ' + status);
18+
tuya.get().then(status => {
19+
console.log('Status: ' + status);
2320

24-
tuya.setStatus(!status, function(error, result) {
25-
if (error) { return console.log(error); }
26-
console.log('Result of setting status to ' + !status + ': ' + result);
21+
tuya.set({set: !status}).then(result => {
22+
console.log('Result of setting status to ' + !status + ': ' + result);
2723

28-
tuya.getStatus(function(error, status) {
29-
if (error) { return console.log(error); }
30-
console.log('New status: ' + status);
24+
tuya.get().then(status => {
25+
console.log('New status: ' + status);
26+
return;
27+
});
3128
});
3229
});
33-
});
30+
3431

3532
This should report the current status, set the device to the opposite of what it currently is, then report the changed status.
3633

@@ -44,10 +41,9 @@ See the [docs](docs/API.md).
4441

4542
## TODO
4643

47-
3. Better documentation.
48-
7. Add automated tests
49-
8. Document details of protocol
50-
9. Add error message for no IP
44+
1. Add automated tests
45+
2. Document details of protocol
46+
3. Retry when ECONNRESET is thrown
5147

5248
## Contributors
5349

docs/API.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ const tuya = new TuyaDevice([
3838

3939
### resolveIds
4040

41-
Resolves IDs stored in class to IPs.
41+
Resolves IDs stored in class to IPs. If you didn't pass IPs to the constructor,
42+
you must call this before doing anything else.
4243

4344
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>** true if IPs were found and devices are ready to be used
4445

4546
### get
4647

47-
Gets the device's current status. Defaults to returning only the value of the first result,
48+
Gets a device's current status. Defaults to returning only the value of the first result,
4849
but by setting {schema: true} you can get everything.
4950

5051
**Parameters**
@@ -74,7 +75,7 @@ Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
7475

7576
### set
7677

77-
Sets the device's status.
78+
Sets a property on a device.
7879

7980
**Parameters**
8081

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ function TuyaDevice(options) {
3838

3939
// Standardize devices array
4040
for (let i = 0; i < this.devices.length; i++) {
41+
if (this.devices[i].id === undefined) {
42+
throw new Error('ID is missing from device.');
43+
}
44+
if (this.devices[i].key === undefined) {
45+
throw new Error('Encryption key is missing from device with ID ' + this.devices[i].id + '.');
46+
}
4147
if (this.devices[i].type === undefined) {
4248
this.devices[i].type = 'outlet';
4349
}
@@ -103,7 +109,7 @@ TuyaDevice.prototype.resolveIds = function () {
103109
};
104110

105111
/**
106-
* Gets the device's current status. Defaults to returning only the value of the first result,
112+
* Gets a device's current status. Defaults to returning only the value of the first result,
107113
* but by setting {schema: true} you can get everything.
108114
* @param {Object} [options] - optional options for getting data
109115
* @param {String} [options.id] - ID of device
@@ -148,7 +154,7 @@ TuyaDevice.prototype.get = function (options) {
148154
const thisData = Buffer.from(JSON.stringify(requests[currentDevice.type].status.command));
149155
const buffer = this._constructBuffer(currentDevice.type, thisData, 'status');
150156

151-
return new Promise(resolve => {
157+
return new Promise((resolve, reject) => {
152158
this._send(currentDevice.ip, buffer).then(data => {
153159
// Extract returned JSON
154160
data = this._extractJSON(data);
@@ -158,12 +164,14 @@ TuyaDevice.prototype.get = function (options) {
158164
} else {
159165
resolve(data.dps['1']);
160166
}
167+
}).catch(error => {
168+
reject(error);
161169
});
162170
});
163171
};
164172

165173
/**
166-
* Sets the device's status.
174+
* Sets a property on a device.
167175
* @param {Object} options - options for setting properties
168176
* @param {String} [options.id] - ID of device
169177
* @param {Boolean} options.set - `true` for on, `false` for off
@@ -244,7 +252,7 @@ TuyaDevice.prototype.set = function (options) {
244252
};
245253

246254
/**
247-
* Sends a query to the device.
255+
* Sends a query to a device.
248256
* @private
249257
* @param {String} ip - IP of device
250258
* @param {Buffer} buffer - buffer of data
@@ -263,6 +271,7 @@ TuyaDevice.prototype._send = function (ip, buffer) {
263271
resolve(data);
264272
});
265273
client.on('error', error => {
274+
error.message = "Error communicating with device. Make sure nothing else is trying to control it or connected to it."
266275
reject(error);
267276
});
268277
});
@@ -274,7 +283,7 @@ TuyaDevice.prototype._send = function (ip, buffer) {
274283
* @private
275284
* @param {String} type - type of device
276285
* @param {String} data - data to put in buffer
277-
* @param {String} command - command (status, on, off, etc.)
286+
* @param {String} command - command (status || set)
278287
* @returns {Buffer} buffer - buffer of data
279288
*/
280289
TuyaDevice.prototype._constructBuffer = function (type, data, command) {
@@ -289,7 +298,7 @@ TuyaDevice.prototype._constructBuffer = function (type, data, command) {
289298
/**
290299
* Extracts JSON from a raw buffer and returns it as an object.
291300
* @private
292-
* @param {Buffer} buffer of data
301+
* @param {Buffer} data - buffer of data
293302
* @returns {Object} extracted object
294303
*/
295304
TuyaDevice.prototype._extractJSON = function (data) {

0 commit comments

Comments
 (0)