Skip to content

Commit b87d092

Browse files
authored
Release v3.1.0 (#93)
* set APP_ID and APP_SECRET from main class * add APP_ID and APP_SECRET as class constructor parameters * updated test case * updated test case * added new test case * docs updated * Release v3.1.0 - "setWSDevicePowerState" (#96) * new mixing to control devices using websocket * switch status on single channel devices * working on deviceControl mixin * better error handling * working on fix for shared devices * refactor/cleanup * added helper function * added docs for new method * return device new status * added test cases * properly close websocket connection and clean used properties * added test cases * error detection enhancements * added test cases * error detection enhancements * added new test file to jest setup * method renamed * fix for closing websocket connection * new getWSDevicePowerState method * added test cases * re-arrange tests * added new test cases * extract helpers methods * added test case * close WebSocket connection on auth error * updated docs * updated dependencies * fix for "forbidden" error * updated dependencies
1 parent c11b3a8 commit b87d092

28 files changed

Lines changed: 2346 additions & 2078 deletions

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* [getDevices](available-methods/getdevices.md)
1414
* [getDevicePowerState](available-methods/getdevicepowerstate.md)
1515
* [setDevicePowerState](available-methods/setdevicepowerstate.md)
16+
* [getWSDevicePowerState](available-methods/getwsdevicepowerstate.md)
17+
* [setWSDevicePowerState](available-methods/setwsdevicepowerstate.md)
1618
* [toggleDevice](available-methods/toggledevice.md)
1719
* [getDevicePowerUsage](available-methods/getdevicepowerusage.md)
1820
* [getDeviceCurrentTH](available-methods/getdevicecurrentth.md)

docs/available-methods/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Here is the list of available methods.
88
* [getDevices](getdevices.md)
99
* [getDevicePowerState](getdevicepowerstate.md)
1010
* [setDevicePowerState](setdevicepowerstate.md)
11+
* [getWSDevicePowerState](getwsdevicepowerstate.md)
12+
* [setWSDevicePowerState](setwsdevicepowerstate.md)
1113
* [toggleDevice](toggledevice.md)
1214
* [getDevicePowerUsage](getdevicepowerusage.md)
1315
* [getDeviceCurrentTH](getdevicecurrentth.md)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# getWSDevicePowerState
2+
3+
Query for specified device power status using WebSockets.
4+
5+
6+
### Usage
7+
```js
8+
// get device power status
9+
const status = await connection.getWSDevicePowerState('<your device id>');
10+
console.log(status);
11+
```
12+
13+
```js
14+
// get device power status using a secondary account
15+
const status = await connection.getWSDevicePowerState('<your device id>', {
16+
shared: true,
17+
});
18+
console.log(status);
19+
```
20+
21+
```js
22+
// get channel 3 power status on multi-channel device
23+
const status = await connection.getWSDevicePowerState('<your device id>', {
24+
channel: 3,
25+
});
26+
console.log(status);
27+
```
28+
29+
```js
30+
// get all channels power status on multi-channel device
31+
const status = await connection.getWSDevicePowerState('<your device id>', {
32+
allChannels: true,
33+
});
34+
console.log(status);
35+
```
36+
37+
38+
<sup>* _Remember to instantiate class before use_</sup>
39+
40+
41+
### Response example
42+
```js
43+
{
44+
status: 'ok',
45+
state: 'off',
46+
channel: 1
47+
}
48+
```
49+
50+
```js
51+
{
52+
status: 'ok',
53+
state: [
54+
{ channel: 1, state: 'off' },
55+
{ channel: 2, state: 'off' },
56+
{ channel: 3, state: 'off' },
57+
{ channel: 4, state: 'off' }
58+
]
59+
}
60+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# setWSDevicePowerState
2+
3+
Change specified device power state using WebSockets.
4+
5+
Possible states: `on`, `off`, `toggle`.
6+
7+
### Usage
8+
```js
9+
const status = await connection.setWSDevicePowerState('<your device id>', 'on');
10+
console.log(status);
11+
```
12+
13+
```js
14+
// multi-channel devices like Sonoff 4CH
15+
// example will toggle power state on channel 3
16+
const status = await connection.setWSDevicePowerState('<your device id>', 'toggle', {
17+
channel: 3,
18+
});
19+
console.log(status);
20+
```
21+
22+
```js
23+
// to control a shared device using a second account, add "shared" setting
24+
const status = await connection.setWSDevicePowerState('<your device id>', 'off', {
25+
shared: true
26+
});
27+
console.log(status);
28+
```
29+
30+
```js
31+
// turn on channel 2 on a shared multi-channel device
32+
const status = await connection.setWSDevicePowerState('<your device id>', 'on', {
33+
channel: 2,
34+
shared: true
35+
});
36+
console.log(status);
37+
```
38+
39+
40+
<sup>* _Remember to instantiate class before use_</sup>
41+
42+
43+
### Response example
44+
```js
45+
{
46+
status: 'ok',
47+
state: 'on',
48+
channel: 1
49+
}
50+
```

docs/class-instantiation.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Class Instantiation
22

3-
> Default region of this library is `us`. If your are in a different one, **you must** specify region parameter or error 400/401 will be returned.
3+
* Default region of this library is `us`. If your are in a different one, **you must** specify region parameter or error 400/401 will be returned.
44

5-
**_Using email and password_**
5+
* If you don't know your region, use [getRegion](available-methods/getregion) method
6+
7+
* To get your access token and api key, use [getCredentials](available-methods/getcredentials) method
8+
9+
## Using email and password
610
```
711
const connection = new ewelink({
812
email: '<your ewelink email>',
@@ -11,7 +15,7 @@
1115
});
1216
```
1317

14-
**_Using phone number and password_**
18+
## Using phone number and password
1519
```
1620
const connection = new ewelink({
1721
phoneNumber: '<your phone number>',
@@ -20,7 +24,7 @@
2024
});
2125
```
2226

23-
**_Using access token and api key_**
27+
## Using access token and api key
2428
```
2529
const connection = new ewelink({
2630
at: '<valid access token>',
@@ -29,8 +33,17 @@
2933
});
3034
```
3135

32-
**_Using devices and arp table cache files_**
33-
Check [ZeroConf](zeroconf.md) docs for detailed information.
36+
## Custom APP_ID and APP_SECRET
37+
This library uses an APP ID and APP Secret provided by Sonoff team.
38+
If you want to specify another pair of settings, just pass in the class constructor:
39+
```
40+
const connection = new ewelink({
41+
email: '<your ewelink email>',
42+
password: '<your ewelink password>',
43+
APP_ID: 'CUSTOM APP ID',
44+
APP_SECRET: 'CUSTOM APP SECRET',
45+
});
46+
```
3447

35-
> * If you don't know your region, use [getRegion](available-methods/getregion) method
36-
> * To get your access token and api key, use [getCredentials](available-methods/getcredentials) method
48+
## Using devices and arp table cache files
49+
Check [ZeroConf](zeroconf.md) docs for detailed information.

main.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
const {
2+
APP_ID: DEFAULT_APP_ID,
3+
APP_SECRET: DEFAULT_APP_SECRET,
4+
} = require('./src/data/constants');
5+
16
const mixins = require('./src/mixins');
27
const errors = require('./src/data/errors');
38

49
class eWeLink {
5-
constructor({
6-
region = 'us',
7-
email = null,
8-
phoneNumber = null,
9-
password = null,
10-
at = null,
11-
apiKey = null,
12-
devicesCache = null,
13-
arpTable = null,
14-
}) {
10+
constructor(parameters = {}) {
11+
const {
12+
region = 'us',
13+
email = null,
14+
phoneNumber = null,
15+
password = null,
16+
at = null,
17+
apiKey = null,
18+
devicesCache = null,
19+
arpTable = null,
20+
APP_ID = DEFAULT_APP_ID,
21+
APP_SECRET = DEFAULT_APP_SECRET,
22+
} = parameters;
23+
1524
const check = this.checkLoginParameters({
1625
region,
1726
email,
@@ -35,6 +44,9 @@ class eWeLink {
3544
this.apiKey = apiKey;
3645
this.devicesCache = devicesCache;
3746
this.arpTable = arpTable;
47+
48+
this.APP_ID = APP_ID;
49+
this.APP_SECRET = APP_SECRET;
3850
}
3951

4052
// eslint-disable-next-line class-methods-use-this

0 commit comments

Comments
 (0)