Skip to content

Commit e193cae

Browse files
authored
Refactor and Clean Up (#143)
* Convert TuyaCipher to ES6 class * Add hooks for testing * Convert MessageParser to ES6 class * Only package necessary files * Clean up constructor checks * Refactor get/set into ES6 async/await syntax * Refactor resolveId() -> find() * Simplify _send() * Rename `__ping` * Massively simplify get/set with/without events Will now *always* emit event on data, `get()` will *always* return data. Nice and simple. :) * Simplify _send() and add comments * Simplify `connect()` * Update docs * Clean up debug() statements * Fix retry in send() * Fix `find()` * Update README * Remove persistent option, `.connect()` in `.send()`, and add option to return all devices in `.find()` * Fix `.find()` when returning all devices * Fix `.find()` returing multiples * Fix `.find()` returning multiples
1 parent 2cf7330 commit e193cae

File tree

7 files changed

+1623
-1163
lines changed

7 files changed

+1623
-1163
lines changed

README.md

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,55 @@ A library for communicating with devices that use the [Tuya](http://tuya.com) cl
1313

1414
## Basic Usage
1515

16+
See the [setup instructions](docs/SETUP.md) for how to find the needed parameters.
17+
18+
These examples should report the current status, set the default property to the opposite of what it currently is, then report the changed status.
19+
They will need to be adapted if your device does not have a boolean property at index 1 (i.e. it doesn't have an on/off property).
20+
1621
### Asynchronous (event based, recommended)
1722
```javascript
23+
const TuyAPI = require('tuyapi');
24+
1825
const device = new TuyAPI({
1926
id: 'xxxxxxxxxxxxxxxxxxxx',
20-
key: 'xxxxxxxxxxxxxxxx',
21-
ip: 'xxx.xxx.xxx.xxx',
22-
persistentConnection: true});
27+
key: 'xxxxxxxxxxxxxxxx'});
2328

24-
device.on('connected',() => {
25-
console.log('Connected to device.');
29+
let stateHasChanged = false;
30+
31+
// Find device on network
32+
device.find().then(() => {
33+
// Connect to device
34+
device.connect();
35+
});
36+
37+
// Add event listeners
38+
device.on('connected', () => {
39+
console.log('Connected to device!');
2640
});
2741

28-
device.on('disconnected',() => {
42+
device.on('disconnected', () => {
2943
console.log('Disconnected from device.');
3044
});
3145

46+
device.on('error', error => {
47+
console.log('Error!', error);
48+
});
49+
3250
device.on('data', data => {
3351
console.log('Data from device:', data);
3452

35-
const status = data.dps['1'];
53+
console.log(`Boolean status of default property: ${data.dps['1']}.`);
3654

37-
console.log('Current status:', status);
55+
// Set default property to opposite
56+
if (!stateHasChanged) {
57+
device.set({set: !(data.dps['1'])});
3858

39-
device.set({set: !status}).then(result => {
40-
console.log('Result of setting status:', result);
41-
});
59+
// Otherwise we'll be stuck in an endless
60+
// loop of toggling the state.
61+
stateHasChanged = true;
62+
}
4263
});
4364

44-
device.on('error',(err) => {
45-
console.log('Error: ' + err);
46-
});
47-
48-
device.connect();
49-
5065
// Disconnect after 10 seconds
5166
setTimeout(() => { device.disconnect(); }, 10000);
5267
```
@@ -57,34 +72,32 @@ const TuyAPI = require('tuyapi');
5772

5873
const device = new TuyAPI({
5974
id: 'xxxxxxxxxxxxxxxxxxxx',
60-
key: 'xxxxxxxxxxxxxxxx',
61-
ip: 'xxx.xxx.xxx.xxx'});
75+
key: 'xxxxxxxxxxxxxxxx'});
6276

63-
device.get().then(status => {
64-
console.log('Status:', status);
77+
(async () => {
78+
await device.find();
6579

66-
device.set({set: !status}).then(result => {
67-
console.log('Result of setting status to ' + !status + ': ' + result);
80+
let status = await device.get();
6881

69-
device.get().then(status => {
70-
console.log('New status:', status);
71-
return;
72-
});
73-
});
74-
});
75-
```
82+
console.log(`Current status: ${status}.`);
7683

77-
This should report the current status, set the device to the opposite of what it currently is, then report the changed status. The above examples will work with smart plugs; they may need some tweaking for other types of devices.
84+
await device.set({set: !status});
7885

79-
See the [setup instructions](docs/SETUP.md) for how to find the needed parameters.
86+
status = await device.get();
87+
88+
console.log(`New status: ${status}.`);
89+
90+
device.disconnect();
91+
})();
92+
```
8093

8194

8295
## 📝 Notes
8396
- Only one TCP connection can be in use with a device at once. If using this, do not have the app on your phone open.
8497
- Some devices ship with older firmware that may not work with `tuyapi`. If you're experiencing issues, please try updating the device's firmware in the official app.
8598

8699

87-
## 📓 Docs
100+
## 📓 Documentation
88101

89102
See the [docs](https://codetheweb.github.io/tuyapi/index.html).
90103

@@ -103,6 +116,13 @@ See the [docs](https://codetheweb.github.io/tuyapi/index.html).
103116
- [NorthernMan54](https://github.com/NorthernMan54)
104117
- [Apollon77](https://github.com/Apollon77)
105118
- [dresende](https://github.com/dresende)
119+
- [kaveet](https://github.com/kaveet)
120+
- [johnyorke](https://github.com/johnyorke)
121+
- [jpillora](https://github.com/jpillora)
122+
- [neojski](https://github.com/neojski)
123+
- [unparagoned](https://github.com/unparagoned)
124+
125+
(If you're not on the above list, open a PR.)
106126

107127
## Related
108128

0 commit comments

Comments
 (0)