Files
ewelink-api/mixins/powerState/setDevicePowerStateMixin.js
Martin M 098011b285 Release v2.0.0 (#44)
* Added arpTableSolver (#18)

* Added arpTableSolver

* fix package import

* linting class

* changed arp library

* refactor arp class

* using arpping fork

* refactor arpTableSolver class

* Added Zero Conf functionality (LAN mode) (#46)

* added crypto-js

* zeroconf helper functions

* zeroconf update payload

* new method to save devices cache file

* class renamed

* refactor Zeroconf class

* return cached device if exists

* moved method to get local ip address

* fix mac addresses without leading zeroes

* refactor Zeroconf class

* using new zeroconf functionality

* zeroconf working with single and multichannel devices

* save device mixin enhancement

* working on zeroconf test cases

* catch errors on filesystem methods

* zeroconf: added extra test cases

* better error handling

* zeroconf: 100% code coverage

* removed deprecated login method

* updates on credentials file

* version bump

* Docs for v2.0 (#52)

* added v1 docs

* added zeroconf docs

* updated readme

* docs updated

* removed zeroconf article warning

* updated vscode config

Co-authored-by: Luis Llamas <luisllamas@hotmail.com>
2020-01-11 01:39:29 -03:00

87 lines
2.1 KiB
JavaScript

const { _get } = require('../../lib/helpers');
const { getDeviceChannelCount } = require('../../lib/ewelink-helper');
const {
ChangeState,
ChangeStateZeroconf,
} = require('../../classes/PowerState');
const setDevicePowerState = {
/**
* 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 deviceApiKey = _get(device, 'apikey', false);
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, msg: 'Device channel does not exist' };
}
if (error || (!status && !switches)) {
if (error && parseInt(error) === 401) {
return device;
}
return { error, msg: 'Device does not exist' };
}
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 actionParams = {
apiUrl: this.getApiWebSocket(),
at: this.at,
apiKey: this.apiKey,
deviceId,
params,
state: stateToSwitch,
};
if (this.apiKey !== deviceApiKey) {
actionParams.apiKey = deviceApiKey;
}
return ChangeState.set(actionParams);
},
};
module.exports = setDevicePowerState;