From ac80e06b0f0ad5365985bbd2a30b6743fe51b82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marti=CC=81n=20M?= Date: Wed, 21 Oct 2020 21:00:23 -0300 Subject: [PATCH] setDevicePowerState: use v2 API --- src/mixins/setDevicePowerState.js | 52 +++++++++++++------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/mixins/setDevicePowerState.js b/src/mixins/setDevicePowerState.js index 728f209..b8aab21 100644 --- a/src/mixins/setDevicePowerState.js +++ b/src/mixins/setDevicePowerState.js @@ -1,10 +1,6 @@ -const { _get, timestamp, nonce } = require('../helpers/utilities'); +const { _get } = require('../helpers/utilities'); const errors = require('../data/errors'); -const { getDeviceChannelCount } = require('../helpers/ewelink'); - -const ChangeStateZeroconf = require('../classes/ChangeStateZeroconf'); - module.exports = { /** * Change power state for a specific device @@ -17,20 +13,20 @@ module.exports = { */ async setDevicePowerState(deviceId, state, channel = 1) { const device = await this.getDevice(deviceId); + + /** Check for errors */ const error = _get(device, 'error', false); - const uiid = _get(device, 'extra.extra.uiid', false); + if (error) { + throw new Error(`[${error}] ${errors[error]}`); + } let status = _get(device, 'params.switch', false); const switches = _get(device, 'params.switches', false); - const switchesAmount = getDeviceChannelCount(uiid); + const switchesAmount = switches.length; if (switchesAmount > 0 && switchesAmount < channel) { - return { error: 404, msg: errors.ch404 }; - } - - if (error || (!status && !switches)) { - return { error, msg: errors[error] }; + throw new Error(`${errors.ch404}`); } let stateToSwitch = state; @@ -51,35 +47,31 @@ module.exports = { params.switch = stateToSwitch; } - if (this.devicesCache) { - return ChangeStateZeroconf.set({ - url: this.getZeroconfUrl(device), - device, - params, - switches, - state: stateToSwitch, - }); - } - - const { APP_ID } = this; + // DISABLED DURING v4.0.0 DEVELOPMENT + // if (this.devicesCache) { + // return ChangeStateZeroconf.set({ + // url: this.getZeroconfUrl(device), + // device, + // params, + // switches, + // state: stateToSwitch, + // }); + // } const response = await this.makeRequest({ method: 'post', - uri: '/user/device/status', + uri: '/v2/device/thing/status', body: { - deviceid: deviceId, + type: 1, + id: deviceId, params, - appid: APP_ID, - nonce, - ts: timestamp, - version: 8, }, }); const responseError = _get(response, 'error', false); if (responseError) { - return { error: responseError, msg: errors[responseError] }; + throw new Error(`[${error}] ${errors[error]}`); } return { status: 'ok', state, channel };