mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-24 06:28:30 +01:00
Release v3.0.0 (#85)
* updated dependencies * code linting * added new app id & app secret * cleanup requests payloads * remove unused function * update test cases * enabled firmware tests * refactor getDevice to use right api endpoint * error messages improvements * error messages improvements * error messages improvements * error messages improvements * error messages improvements * payload cleanup * refactor setDevicePowerState to use right api endpoint * update test exepectation * removed deprecated class * updated tests to reflect new error codes * error messages improvements * refactoring project structure: devices methods refactoring project sturcture * refactoring project structure: firmware methods * refactoring project structure: temperature/humidity * refactoring project structure: credentials methods * refactoring project structure: power usage methods * refactoring project structure: power state methods * refactoring project structure: websocket methods * removed deprecated login method from docs * refactoring project structure: power usage methods * refactoring project structure: zeroconf classes * refactoring project structure: websocket classes * refactoring project structure: zeroconf classes * refactor and cleanup * refactoring project structure: firmware methods * moved parsers to own directory * update tests with methods renames * export missing temperature/humidity methods * removed unused package * refactor and cleanup * fix test expectation * refactoring project structure: moved data files * refactoring project structure: moved data files * refactoring project structure: moved helpers files * refactoring project structure: moved helpers files * refactoring project structure: moved payload files * refactor and cleanup * refactor getDevicePowerState * setDevicePowerState returns channel * convert error 400 to 404 for clarity * updated test cases * remove console.log * cache path for zeroconf cache files * installed nock * using nock to simulate server requests during testing * moved credentials file to config folder * update request url when using nock * refactor nock helper file * move cooldown delay to setupTests file * updating testing instructions * restored delete code block * fix wrong error code * accept phone number to login to ewelink * added test cases for initialize main class * improvements on class initialization parameters * allow login using phone number * rename test file * updated test case * fixed regression bug * Release v3.0.0 - use node-fetch (#87) * replaced deprecated request library with node-fetch * refactor: moved makeRequest to own mixin file * refactor to use node-fetch * fixes * update config * created helper method * constant rename * ignore files from final package * version bump
This commit is contained in:
86
src/mixins/setDevicePowerState.js
Normal file
86
src/mixins/setDevicePowerState.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const { APP_ID } = require('../data/constants');
|
||||
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 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 };
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user