mirror of
https://github.com/skydiver/ewelink-api.git
synced 2026-01-03 11:35:01 +01:00
* helper method to login into ewelink if no auth credentials found * return websocket reponse as JSON * created function to get raw consumption data * created function to parse raw consumption data and return daily usage * renamed property * created function to get current month power usage * created function to get raw power usage * added new test cases * catch websocket connection errors * power usage enhancements * added new test case * removed unused code * updated credentials file * version bump * updated dependencies * tests reorganized
73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
const delay = require('delay');
|
|
|
|
const ewelink = require('../main');
|
|
|
|
const {
|
|
email,
|
|
password,
|
|
deviceIdWithPower,
|
|
} = require('./_setup/credentials.json');
|
|
const {
|
|
rawPowerUsageExpectations,
|
|
currentMonthPowerUsageExpectations,
|
|
} = require('./_setup/expectations');
|
|
|
|
describe('power usage: node script', () => {
|
|
let conn;
|
|
|
|
beforeAll(async () => {
|
|
conn = new ewelink({ email, password });
|
|
await conn.login();
|
|
});
|
|
|
|
test('should return raw power usage', async () => {
|
|
jest.setTimeout(30000);
|
|
const powerUsage = await conn.getDeviceRawPowerUsage(deviceIdWithPower);
|
|
expect(typeof powerUsage).toBe('object');
|
|
expect(powerUsage).toMatchObject(rawPowerUsageExpectations);
|
|
expect(powerUsage.data.hundredDaysKwhData.length).toBe(600);
|
|
});
|
|
|
|
test('should return current month power usage', async () => {
|
|
jest.setTimeout(30000);
|
|
await delay(1000);
|
|
const days = new Date().getDate();
|
|
const powerUsage = await conn.getDevicePowerUsage(deviceIdWithPower);
|
|
expect(typeof powerUsage).toBe('object');
|
|
expect(powerUsage).toMatchObject(currentMonthPowerUsageExpectations);
|
|
expect(powerUsage.daily.length).toBe(days);
|
|
});
|
|
});
|
|
|
|
describe('power usage: serverless', () => {
|
|
let accessToken;
|
|
let apiKey;
|
|
|
|
beforeAll(async () => {
|
|
const conn = new ewelink({ email, password });
|
|
const login = await conn.login();
|
|
accessToken = login.at;
|
|
apiKey = login.user.apikey;
|
|
});
|
|
|
|
test('should return raw power usage', async () => {
|
|
jest.setTimeout(30000);
|
|
const conn = new ewelink({ at: accessToken, apiKey });
|
|
const powerUsage = await conn.getDeviceRawPowerUsage(deviceIdWithPower);
|
|
expect(typeof powerUsage).toBe('object');
|
|
expect(powerUsage).toMatchObject(rawPowerUsageExpectations);
|
|
expect(powerUsage.data.hundredDaysKwhData.length).toBe(600);
|
|
});
|
|
|
|
test('should return current month power usage', async () => {
|
|
jest.setTimeout(30000);
|
|
await delay(1000);
|
|
const days = new Date().getDate();
|
|
const conn = new ewelink({ at: accessToken, apiKey });
|
|
const powerUsage = await conn.getDevicePowerUsage(deviceIdWithPower);
|
|
expect(typeof powerUsage).toBe('object');
|
|
expect(powerUsage).toMatchObject(currentMonthPowerUsageExpectations);
|
|
expect(powerUsage.daily.length).toBe(days);
|
|
});
|
|
});
|