mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-21 21:33:11 +01:00
* 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
88 lines
2.1 KiB
JavaScript
88 lines
2.1 KiB
JavaScript
const { _get, timestamp, nonce } = 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
|
|
*
|
|
* @param deviceId
|
|
* @param state
|
|
* @param channel
|
|
*
|
|
* @returns {Promise<{state: *, status: string}|{msg: string, error: *}>}
|
|
*/
|
|
async setDevicePowerState(deviceId, state, channel = 1) {
|
|
const device = await this.getDevice(deviceId);
|
|
const error = _get(device, 'error', false);
|
|
const uiid = _get(device, 'extra.extra.uiid', false);
|
|
|
|
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: 404, msg: errors.ch404 };
|
|
}
|
|
|
|
if (error || (!status && !switches)) {
|
|
return { error, msg: errors[error] };
|
|
}
|
|
|
|
let stateToSwitch = state;
|
|
const params = {};
|
|
|
|
if (switches) {
|
|
status = switches[channel - 1].switch;
|
|
}
|
|
|
|
if (state === 'toggle') {
|
|
stateToSwitch = status === 'on' ? 'off' : 'on';
|
|
}
|
|
|
|
if (switches) {
|
|
params.switches = switches;
|
|
params.switches[channel - 1].switch = stateToSwitch;
|
|
} else {
|
|
params.switch = stateToSwitch;
|
|
}
|
|
|
|
if (this.devicesCache) {
|
|
return ChangeStateZeroconf.set({
|
|
url: this.getZeroconfUrl(device),
|
|
device,
|
|
params,
|
|
switches,
|
|
state: stateToSwitch,
|
|
});
|
|
}
|
|
|
|
const { APP_ID } = this;
|
|
|
|
const response = await this.makeRequest({
|
|
method: 'post',
|
|
uri: '/user/device/status',
|
|
body: {
|
|
deviceid: deviceId,
|
|
params,
|
|
appid: APP_ID,
|
|
nonce,
|
|
ts: timestamp,
|
|
version: 8,
|
|
},
|
|
});
|
|
|
|
const responseError = _get(response, 'error', false);
|
|
|
|
if (responseError) {
|
|
return { error: responseError, msg: errors[responseError] };
|
|
}
|
|
|
|
return { status: 'ok', state, channel };
|
|
},
|
|
};
|