-
Notifications
You must be signed in to change notification settings - Fork 2
/
pytu.js
46 lines (33 loc) · 928 Bytes
/
pytu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const TuyAPI = require('tuyapi');
const device = new TuyAPI({
id: '0620074468c63ac12602',
key: '10b58369f1ec852f'});
let stateHasChanged = false;
// Find device on network
device.find().then(() => {
// Connect to device
device.connect();
});
// Add event listeners
device.on('connected', () => {
console.log('Connected to device!');
});
device.on('disconnected', () => {
console.log('Disconnected from device.');
});
device.on('error', error => {
console.log('Error!', error);
});
device.on('data', data => {
console.log('Data from device:', data);
// Set default property to opposite
if (!stateHasChanged) {
//device.set({set: !(data.dps['1'])});
device.set({dps: 5, set: '6666ff0000ffff'});
// Otherwise we'll be stuck in an endless
// loop of toggling the state.
stateHasChanged = true;
}
});
// Disconnect after 10 seconds
setTimeout(() => { device.disconnect(); }, 3000);