Release v1.4.1 (#13)

* specific error when requested channel is bigger than total

* fix for multi-channel devices

* updated test expectations

* fixes with uiid api changes

* fix wrong error message

* added new test cases

* return expected error on wrong channel number

* added more devices

* version bump
This commit is contained in:
Martin M
2019-09-11 23:46:17 -03:00
committed by GitHub
parent 171faa617b
commit 024aaa8a2c
10 changed files with 190 additions and 52 deletions

33
main.js
View File

@@ -165,11 +165,18 @@ class eWeLink {
async getDevicePowerState(deviceId, channel = 1) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const uiid = _get(device, 'extra.extra.uiid', false);
let state = _get(device, 'params.switch', false);
const switches = _get(device, 'params.switches', false);
const switchesAmount = getDeviceChannelCount(device.uiid);
if (error || switchesAmount < channel || (!state && !switches)) {
const switchesAmount = getDeviceChannelCount(uiid);
if (switchesAmount > 0 && switchesAmount < channel) {
return { error, msg: 'Device channel does not exist' };
}
if (error || (!state && !switches)) {
if (error && parseInt(error) === 401) {
return device;
}
@@ -195,11 +202,18 @@ class eWeLink {
async setDevicePowerState(deviceId, state, channel = 1) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const status = _get(device, 'params.switch', false);
const switches = _get(device, 'params.switches', false);
const switchesAmount = getDeviceChannelCount(device.uiid);
const uiid = _get(device, 'extra.extra.uiid', false);
if (error || switchesAmount < channel || (!status && !switches)) {
let status = _get(device, 'params.switch', false);
const switches = _get(device, 'params.switches', false);
const switchesAmount = getDeviceChannelCount(uiid);
if (switchesAmount > 0 && switchesAmount < channel) {
return { error, msg: 'Device channel does not exist' };
}
if (error || (!status && !switches)) {
if (error && parseInt(error) === 401) {
return device;
}
@@ -207,13 +221,16 @@ class eWeLink {
}
let stateToSwitch = state;
const params = {};
if (switches) {
status = switches[channel - 1].switch;
}
if (state === 'toggle') {
stateToSwitch = status === 'on' ? 'off' : 'on';
}
const params = {};
if (switches) {
params.switches = switches;
params.switches[channel - 1].switch = stateToSwitch;