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
216 lines
6.7 KiB
JavaScript
216 lines
6.7 KiB
JavaScript
const ewelink = require('../main');
|
|
const errors = require('../src/data/errors');
|
|
const { getAllChannelsState } = require('../src/helpers/device-control');
|
|
|
|
const {
|
|
email,
|
|
password,
|
|
sharedAccount,
|
|
singleChannelDeviceId,
|
|
fourChannelsDevice,
|
|
} = require('./_setup/config/credentials.js');
|
|
|
|
describe('device control using WebSockets: get power state', () => {
|
|
let conn;
|
|
|
|
beforeAll(() => {
|
|
conn = new ewelink({ email, password });
|
|
});
|
|
|
|
test('get power state on single channel device', async () => {
|
|
jest.setTimeout(30000);
|
|
const device = await conn.getDevice(singleChannelDeviceId);
|
|
const { switch: originalState } = device.params;
|
|
const powerState = await conn.getWSDevicePowerState(singleChannelDeviceId, {
|
|
shared: sharedAccount,
|
|
});
|
|
expect(typeof powerState).toBe('object');
|
|
expect(powerState.status).toBe('ok');
|
|
expect(powerState.state).toBe(originalState);
|
|
expect(powerState.channel).toBe(1);
|
|
});
|
|
|
|
test('get power state for specific channel on multi-channel device', async () => {
|
|
jest.setTimeout(30000);
|
|
const channel = 3;
|
|
const device = await conn.getDevice(fourChannelsDevice);
|
|
const { switches } = device.params;
|
|
const originalState = switches[channel - 1].switch;
|
|
const powerState = await conn.getWSDevicePowerState(fourChannelsDevice, {
|
|
channel,
|
|
shared: sharedAccount,
|
|
});
|
|
expect(typeof powerState).toBe('object');
|
|
expect(powerState.status).toBe('ok');
|
|
expect(powerState.state).toBe(originalState);
|
|
expect(powerState.channel).toBe(channel);
|
|
});
|
|
|
|
test('get power state for all channels on multi-channel device', async () => {
|
|
jest.setTimeout(30000);
|
|
const channel = 3;
|
|
const device = await conn.getDevice(fourChannelsDevice);
|
|
const originalState = getAllChannelsState(device.params);
|
|
const powerState = await conn.getWSDevicePowerState(fourChannelsDevice, {
|
|
allChannels: true,
|
|
shared: sharedAccount,
|
|
});
|
|
expect(typeof powerState).toBe('object');
|
|
expect(powerState.status).toBe('ok');
|
|
expect(powerState.state).toStrictEqual(originalState);
|
|
});
|
|
});
|
|
|
|
describe('device control using WebSockets: set power state', () => {
|
|
let conn;
|
|
|
|
beforeAll(() => {
|
|
conn = new ewelink({ email, password });
|
|
});
|
|
|
|
test('toggle power state on single channel device', async () => {
|
|
jest.setTimeout(30000);
|
|
const device = await conn.getDevice(singleChannelDeviceId);
|
|
const { switch: originalState } = device.params;
|
|
const newState = originalState === 'on' ? 'off' : 'on';
|
|
const powerState = await conn.setWSDevicePowerState(
|
|
singleChannelDeviceId,
|
|
'toggle',
|
|
{
|
|
shared: sharedAccount,
|
|
}
|
|
);
|
|
expect(typeof powerState).toBe('object');
|
|
expect(powerState.status).toBe('ok');
|
|
expect(powerState.channel).toBe(1);
|
|
expect(powerState.state).toBe(newState);
|
|
const deviceVerify = await conn.getDevice(singleChannelDeviceId);
|
|
const { switch: currentStateVerify } = deviceVerify.params;
|
|
expect(newState).toBe(currentStateVerify);
|
|
});
|
|
|
|
test('toggle power state on multi-channel device', async () => {
|
|
jest.setTimeout(30000);
|
|
const channel = 3;
|
|
const device = await conn.getDevice(fourChannelsDevice);
|
|
const { switches } = device.params;
|
|
const originalState = switches[channel - 1].switch;
|
|
const newState = originalState === 'on' ? 'off' : 'on';
|
|
const powerState = await conn.setWSDevicePowerState(
|
|
fourChannelsDevice,
|
|
'toggle',
|
|
{
|
|
channel,
|
|
shared: sharedAccount,
|
|
}
|
|
);
|
|
expect(typeof powerState).toBe('object');
|
|
expect(powerState.status).toBe('ok');
|
|
expect(powerState.channel).toBe(channel);
|
|
expect(powerState.state).toBe(newState);
|
|
const deviceVerify = await conn.getDevice(fourChannelsDevice);
|
|
const { switches: switchesVerify } = deviceVerify.params;
|
|
const currentStateVerify = switchesVerify[channel - 1].switch;
|
|
expect(newState).toBe(currentStateVerify);
|
|
});
|
|
|
|
test('turn off single channel device', async () => {
|
|
jest.setTimeout(30000);
|
|
const powerState = await conn.setWSDevicePowerState(
|
|
singleChannelDeviceId,
|
|
'off',
|
|
{
|
|
shared: sharedAccount,
|
|
}
|
|
);
|
|
expect(typeof powerState).toBe('object');
|
|
expect(powerState.status).toBe('ok');
|
|
expect(powerState.channel).toBe(1);
|
|
expect(powerState.state).toBe('off');
|
|
});
|
|
|
|
test('turn off multi-channel device', async () => {
|
|
jest.setTimeout(30000);
|
|
const channel = 3;
|
|
const powerState = await conn.setWSDevicePowerState(
|
|
fourChannelsDevice,
|
|
'off',
|
|
{
|
|
channel,
|
|
shared: sharedAccount,
|
|
}
|
|
);
|
|
expect(typeof powerState).toBe('object');
|
|
expect(powerState.status).toBe('ok');
|
|
expect(powerState.channel).toBe(channel);
|
|
expect(powerState.state).toBe('off');
|
|
});
|
|
});
|
|
|
|
describe('device control using WebSockets: errors and exceptions', () => {
|
|
let conn;
|
|
|
|
beforeAll(() => {
|
|
conn = new ewelink({ email, password });
|
|
});
|
|
|
|
test('get power state using invalid credentials should throw an exception', async () => {
|
|
try {
|
|
const connection = new ewelink({
|
|
email: 'invalid',
|
|
password: 'credentials',
|
|
});
|
|
await connection.getWSDevicePowerState(singleChannelDeviceId);
|
|
} catch (error) {
|
|
expect(typeof error).toBe('object');
|
|
expect(error.toString()).toBe(`Error: ${errors[406]}`);
|
|
}
|
|
});
|
|
|
|
test('get power state using invalid device should throw an exception', async () => {
|
|
jest.setTimeout(30000);
|
|
try {
|
|
await conn.getWSDevicePowerState('INVALID DEVICE', {
|
|
shared: sharedAccount,
|
|
});
|
|
} catch (error) {
|
|
expect(typeof error).toBe('object');
|
|
expect(error.toString()).toBe(`Error: ${errors[403]}`);
|
|
}
|
|
});
|
|
|
|
test('set power state using invalid credentials should throw an exception', async () => {
|
|
try {
|
|
const connection = new ewelink({
|
|
email: 'invalid',
|
|
password: 'credentials',
|
|
});
|
|
await connection.setWSDevicePowerState(singleChannelDeviceId, 'toggle');
|
|
} catch (error) {
|
|
expect(typeof error).toBe('object');
|
|
expect(error.toString()).toBe(`Error: ${errors[406]}`);
|
|
}
|
|
});
|
|
|
|
test('turn off invalid device', async () => {
|
|
jest.setTimeout(30000);
|
|
try {
|
|
await conn.setWSDevicePowerState('INVALID DEVICE', 'off', {
|
|
shared: sharedAccount,
|
|
});
|
|
} catch (error) {
|
|
expect(typeof error).toBe('object');
|
|
expect(error.toString()).toBe(`Error: ${errors[403]}`);
|
|
}
|
|
});
|
|
|
|
test('using invalid power state should throw an exception', async () => {
|
|
try {
|
|
await conn.setWSDevicePowerState(singleChannelDeviceId, 'INVALID STATE');
|
|
} catch (error) {
|
|
expect(typeof error).toBe('object');
|
|
expect(error.toString()).toBe(`Error: ${errors.invalidPowerState}`);
|
|
}
|
|
});
|
|
});
|