Skip to content

Commit 96337f3

Browse files
committed
Better documentation
1 parent 5687892 commit 96337f3

File tree

3 files changed

+103
-41
lines changed

3 files changed

+103
-41
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ This should report the current status, set the device to the opposite of what it
3636

3737
See the [setup instructions](docs/SETUP.md) for how to find the needed parameters.
3838

39-
## Docs
39+
## 📓 Docs
4040

4141
See the [docs](docs/API.md).
42+
4243
**IMPORTANT**: Only one TCP connection can be in use with a device at once. If testing this, do not have the app on your phone open.
4344

4445
## TODO
4546

4647
3. Better documentation.
47-
4. Support arbitrary control schemes for devices as self-reported.
48-
5. Use Promises for all functions?
4948
7. Add automated tests
5049
8. Document details of protocol
50+
9. Add error message for no IP
5151

5252
## Contributors
5353

docs/API.md

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Docs
88
- [resolveIds](#resolveids)
99
- [get](#get)
1010
- [set](#set)
11-
- [\_extractJSON](#_extractjson)
1211

1312
## TuyaDevice
1413

@@ -17,13 +16,25 @@ Represents a Tuya device.
1716
**Parameters**
1817

1918
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options for constructing a TuyaDevice
20-
- `options.type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** type of device (optional, default `'outlet'`)
21-
- `options.ip` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** IP of device
22-
- `options.port` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** port of device (optional, default `6668`)
23-
- `options.id` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** ID of device
24-
- `options.uid` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** UID of device (optional, default `''`)
25-
- `options.key` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** encryption key of device
26-
- `options.version` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** protocol version (optional, default `3.1`)
19+
- `options.type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** type of device (optional, default `'outlet'`)
20+
- `options.ip` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** IP of device
21+
- `options.port` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** port of device (optional, default `6668`)
22+
- `options.id` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** ID of device
23+
- `options.uid` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** UID of device (optional, default `''`)
24+
- `options.key` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** encryption key of device
25+
- `options.version` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** protocol version (optional, default `3.1`)
26+
27+
**Examples**
28+
29+
```javascript
30+
const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'})
31+
```
32+
33+
```javascript
34+
const tuya = new TuyaDevice([
35+
{id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'},
36+
{id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'}])
37+
```
2738

2839
### resolveIds
2940

@@ -33,32 +44,55 @@ Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
3344

3445
### get
3546

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

3850
**Parameters**
3951

40-
- `options`
41-
- `ID` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** optional, ID of device. Defaults to first device.
42-
- `callback` **function ([error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error), result)**
52+
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** optional options for getting data
53+
- `options.id` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** ID of device
54+
- `options.schema` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** true to return entire schema, not just the first result
55+
56+
**Examples**
57+
58+
```javascript
59+
// get status for device with one property
60+
tuya.get().then(status => console.log(status))
61+
```
62+
63+
```javascript
64+
// get status for specific device with one property
65+
tuya.get({id: 'xxxxxxxxxxxxxxxxxxxx'}).then(status => console.log(status))
66+
```
67+
68+
```javascript
69+
// get all available data from device
70+
tuya.get({schema: true}).then(data => console.log(data))
71+
```
72+
73+
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** returns boolean if no options are provided, otherwise returns object of results
4374

4475
### set
4576

4677
Sets the device's status.
4778

4879
**Parameters**
4980

50-
- `options`
51-
- `on` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` for on, `false` for off
52-
{id, set: true|false, dps:1}
53-
- `callback` **function ([error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error), result)** returns `true` if the command succeeded
54-
55-
### \_extractJSON
81+
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options for setting properties
82+
- `options.id` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** ID of device
83+
- `options.set` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` for on, `false` for off
84+
- `options.dps` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** dps index to change
5685

57-
Extracts JSON from a raw buffer and returns it as an object.
86+
**Examples**
5887

59-
**Parameters**
88+
```javascript
89+
// set default property on default device
90+
tuya.set({set: true}).then(() => console.log('device was changed'))
91+
```
6092

61-
- `data`
62-
- `buffer` **[Buffer](https://nodejs.org/api/buffer.html)** of data
93+
```javascript
94+
// set custom property on non-default device
95+
tuya.set({id: 'xxxxxxxxxxxxxxxxxxxx', 'dps': 2, set: true}).then(() => console.log('device was changed'))
96+
```
6397

64-
Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** extracted object
98+
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)>** returns `true` if the command succeeded

index.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ const requests = require('./requests.json');
1111

1212
/**
1313
* Represents a Tuya device.
14-
* @constructor
14+
* @class
1515
* @param {Object} options - options for constructing a TuyaDevice
16-
* @param {string} [options.type='outlet'] - type of device
17-
* @param {string} options.ip - IP of device
18-
* @param {number} [options.port=6668] - port of device
19-
* @param {string} options.id - ID of device
20-
* @param {string} [options.uid=''] - UID of device
21-
* @param {string} options.key - encryption key of device
22-
* @param {number} [options.version=3.1] - protocol version
16+
* @param {String} [options.type='outlet'] - type of device
17+
* @param {String} [options.ip] - IP of device
18+
* @param {Number} [options.port=6668] - port of device
19+
* @param {String} options.id - ID of device
20+
* @param {String} [options.uid=''] - UID of device
21+
* @param {String} options.key - encryption key of device
22+
* @param {Number} [options.version=3.1] - protocol version
23+
* @example
24+
* const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'})
25+
* @example
26+
* const tuya = new TuyaDevice([
27+
* {id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'},
28+
* {id: 'xxxxxxxxxxxxxxxxxxxx', key: 'xxxxxxxxxxxxxxxx'}])
2329
*/
2430
function TuyaDevice(options) {
2531
this.devices = [];
@@ -51,7 +57,8 @@ function TuyaDevice(options) {
5157
}
5258

5359
/**
54-
* Resolves IDs stored in class to IPs.
60+
* Resolves IDs stored in class to IPs. If you didn't pass IPs to the constructor,
61+
* you must call this before doing anything else.
5562
* @returns {Promise<Boolean>} - true if IPs were found and devices are ready to be used
5663
*/
5764
TuyaDevice.prototype.resolveIds = function () {
@@ -96,9 +103,21 @@ TuyaDevice.prototype.resolveIds = function () {
96103
};
97104

98105
/**
99-
* Gets the device's current status. Defaults to returning only the first 'dps', but by setting {schema: true} you can get everything.
100-
* @param {string} ID - optional, ID of device. Defaults to first device.
101-
* @param {function(error, result)} callback
106+
* Gets the device's current status. Defaults to returning only the value of the first result,
107+
* but by setting {schema: true} you can get everything.
108+
* @param {Object} [options] - optional options for getting data
109+
* @param {String} [options.id] - ID of device
110+
* @param {Boolean} [options.schema] - true to return entire schema, not just the first result
111+
* @example
112+
* // get status for device with one property
113+
* tuya.get().then(status => console.log(status))
114+
* @example
115+
* // get status for specific device with one property
116+
* tuya.get({id: 'xxxxxxxxxxxxxxxxxxxx'}).then(status => console.log(status))
117+
* @example
118+
* // get all available data from device
119+
* tuya.get({schema: true}).then(data => console.log(data))
120+
* @returns {Promise<Object>} - returns boolean if no options are provided, otherwise returns object of results
102121
*/
103122
TuyaDevice.prototype.get = function (options) {
104123
let currentDevice;
@@ -145,9 +164,17 @@ TuyaDevice.prototype.get = function (options) {
145164

146165
/**
147166
* Sets the device's status.
148-
* @param {boolean} on - `true` for on, `false` for off
149-
* {id, set: true|false, dps:1}
150-
* @param {function(error, result)} callback - returns `true` if the command succeeded
167+
* @param {Object} options - options for setting properties
168+
* @param {String} [options.id] - ID of device
169+
* @param {Boolean} options.set - `true` for on, `false` for off
170+
* @param {Number} [options.dps] - dps index to change
171+
* @example
172+
* // set default property on default device
173+
* tuya.set({set: true}).then(() => console.log('device was changed'))
174+
* @example
175+
* // set custom property on non-default device
176+
* tuya.set({id: 'xxxxxxxxxxxxxxxxxxxx', 'dps': 2, set: true}).then(() => console.log('device was changed'))
177+
* @returns {Promise<Boolean>} - returns `true` if the command succeeded
151178
*/
152179
TuyaDevice.prototype.set = function (options) {
153180
let currentDevice;
@@ -261,6 +288,7 @@ TuyaDevice.prototype._constructBuffer = function (type, data, command) {
261288

262289
/**
263290
* Extracts JSON from a raw buffer and returns it as an object.
291+
* @private
264292
* @param {Buffer} buffer of data
265293
* @returns {Object} extracted object
266294
*/

0 commit comments

Comments
 (0)