mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-24 06:28:30 +01:00
Release v1.6.1 (#16)
* refactor temperature/humidity methods * updating temperature/humidity tests * version bump * updated jest config * fix: when logged user is not device owner, can’t change power state * fix: when logged user is not device owner, can’t get power usage * reverted extra code changes * updated tests
This commit is contained in:
@@ -27,9 +27,10 @@ class DeviceRaw extends WebSocket {
|
||||
payloadUpdate,
|
||||
]);
|
||||
|
||||
const error = _get(response, 'error', false);
|
||||
if (error) {
|
||||
return response;
|
||||
const error = _get(response[1], 'error', false);
|
||||
|
||||
if (error === 403) {
|
||||
return { error, msg: response[1].reason };
|
||||
}
|
||||
|
||||
const hundredDaysKwhData = _get(
|
||||
|
||||
9
main.js
9
main.js
@@ -133,8 +133,7 @@ const getDevicePowerUsageMixin = require('./mixins/powerUsage/getDevicePowerUsag
|
||||
const getDeviceRawPowerUsageMixin = require('./mixins/powerUsage/getDeviceRawPowerUsageMixin');
|
||||
|
||||
/* LOAD MIXINS: temperature & humidity */
|
||||
const getDeviceCurrentTemperatureMixin = require('./mixins/temperature/getDeviceCurrentTemperatureMixin');
|
||||
const getDeviceCurrentHumidityMixin = require('./mixins/humidity/getDeviceCurrentHumidityMixin');
|
||||
const getTHMixin = require('./mixins/temphumd/getTHMixin');
|
||||
|
||||
/* LOAD MIXINS: devices */
|
||||
const getDevicesMixin = require('./mixins/devices/getDevicesMixin');
|
||||
@@ -158,11 +157,7 @@ Object.assign(
|
||||
getDeviceRawPowerUsageMixin
|
||||
);
|
||||
|
||||
Object.assign(
|
||||
eWeLink.prototype,
|
||||
getDeviceCurrentTemperatureMixin,
|
||||
getDeviceCurrentHumidityMixin
|
||||
);
|
||||
Object.assign(eWeLink.prototype, getTHMixin);
|
||||
|
||||
Object.assign(
|
||||
eWeLink.prototype,
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
const { _get } = require('../../lib/helpers');
|
||||
|
||||
const getDeviceCurrentHumidityMixin = {
|
||||
/**
|
||||
* Get device current humidity
|
||||
*
|
||||
* @param deviceId
|
||||
*
|
||||
* @returns {Promise<{humidity: *, status: string}|{msg: string, error: *}>}
|
||||
*/
|
||||
async getDeviceCurrentHumidity(deviceId) {
|
||||
const device = await this.getDevice(deviceId);
|
||||
const error = _get(device, 'error', false);
|
||||
const model = _get(device, 'extra.extra.model', '');
|
||||
const humidity = _get(device, 'params.currentHumidity', false);
|
||||
|
||||
if (error || model !== 'PSA-BHA-GL' || !humidity) {
|
||||
if (error && parseInt(error) === 401) {
|
||||
return device;
|
||||
}
|
||||
return { error, msg: 'Device does not exist' };
|
||||
}
|
||||
|
||||
return { status: 'ok', humidity };
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = getDeviceCurrentHumidityMixin;
|
||||
@@ -15,6 +15,7 @@ const setDevicePowerState = {
|
||||
*/
|
||||
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);
|
||||
|
||||
@@ -52,14 +53,20 @@ const setDevicePowerState = {
|
||||
params.switch = stateToSwitch;
|
||||
}
|
||||
|
||||
return ChangeState.set({
|
||||
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);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
const { _get } = require('../../lib/helpers');
|
||||
|
||||
const { DeviceRaw } = require('../../classes/PowerUsage');
|
||||
|
||||
const getDeviceRawPowerUsageMixin = {
|
||||
@@ -9,14 +11,21 @@ const getDeviceRawPowerUsageMixin = {
|
||||
* @returns {Promise<{error: string}|{response: {hundredDaysKwhData: *}, status: string}>}
|
||||
*/
|
||||
async getDeviceRawPowerUsage(deviceId) {
|
||||
await this.logIfNeeded();
|
||||
const device = await this.getDevice(deviceId);
|
||||
const deviceApiKey = _get(device, 'apikey', false);
|
||||
|
||||
return DeviceRaw.get({
|
||||
const actionParams = {
|
||||
apiUrl: this.getApiWebSocket(),
|
||||
at: this.at,
|
||||
apiKey: this.apiKey,
|
||||
deviceId,
|
||||
});
|
||||
};
|
||||
|
||||
if (this.apiKey !== deviceApiKey) {
|
||||
actionParams.apiKey = deviceApiKey;
|
||||
}
|
||||
|
||||
return DeviceRaw.get(actionParams);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
const { _get } = require('../../lib/helpers');
|
||||
|
||||
const getDeviceCurrentTemperatureMixin = {
|
||||
/**
|
||||
* Get device current temperature
|
||||
*
|
||||
* @param deviceId
|
||||
*
|
||||
* @returns {Promise<{temperature: *, status: string}|{msg: string, error: *}>}
|
||||
*/
|
||||
async getDeviceCurrentTemperature(deviceId) {
|
||||
const device = await this.getDevice(deviceId);
|
||||
const error = _get(device, 'error', false);
|
||||
const model = _get(device, 'extra.extra.model', '');
|
||||
const temperature = _get(device, 'params.currentTemperature', false);
|
||||
|
||||
if (error || model !== 'PSA-BHA-GL' || !temperature) {
|
||||
if (error && parseInt(error) === 401) {
|
||||
return device;
|
||||
}
|
||||
return { error, msg: 'Device does not exist' };
|
||||
}
|
||||
|
||||
return { status: 'ok', temperature };
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = getDeviceCurrentTemperatureMixin;
|
||||
55
mixins/temphumd/getTHMixin.js
Normal file
55
mixins/temphumd/getTHMixin.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const { _get } = require('../../lib/helpers');
|
||||
|
||||
const getTHMixin = {
|
||||
/**
|
||||
* Get device current temperature & humidity
|
||||
* @param deviceId
|
||||
* @param type
|
||||
* @returns {Promise<{temperature: *, humidity: *, status: string}|{msg: string, error: *}>}
|
||||
*/
|
||||
async getDeviceCurrentTH(deviceId, type = '') {
|
||||
const device = await this.getDevice(deviceId);
|
||||
const error = _get(device, 'error', false);
|
||||
const temperature = _get(device, 'params.currentTemperature', false);
|
||||
const humidity = _get(device, 'params.currentHumidity', false);
|
||||
|
||||
if (error || !temperature || !humidity) {
|
||||
if (error && parseInt(error) === 401) {
|
||||
return device;
|
||||
}
|
||||
return { error, msg: 'Device does not exist' };
|
||||
}
|
||||
|
||||
const data = { status: 'ok', temperature, humidity };
|
||||
|
||||
if (type === 'temp') {
|
||||
delete data.humidity;
|
||||
}
|
||||
|
||||
if (type === 'humd') {
|
||||
delete data.temperature;
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get device current temperature
|
||||
* @param deviceId
|
||||
* @returns {Promise<*|{temperature: *, status: string}|{msg: string, error: *}>}
|
||||
*/
|
||||
async getDeviceCurrentTemperature(deviceId) {
|
||||
return this.getDeviceCurrentTH(deviceId, 'temp');
|
||||
},
|
||||
|
||||
/**
|
||||
* Get device current humidity
|
||||
* @param deviceId
|
||||
* @returns {Promise<*|{temperature: *, status: string}|{msg: string, error: *}>}
|
||||
*/
|
||||
async getDeviceCurrentHumidity(deviceId) {
|
||||
return this.getDeviceCurrentTH(deviceId, 'humd');
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = getTHMixin;
|
||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ewelink-api",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ewelink-api",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.1",
|
||||
"description": "eWeLink API for Node.js",
|
||||
"author": "Martín M.",
|
||||
"license": "MIT",
|
||||
@@ -21,7 +21,7 @@
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"test": "jest --runInBand --verbose",
|
||||
"coverage": "jest --runInBand --verbose --coverage --coverageDirectory=output/coverage/jest",
|
||||
"coverage": "jest --runInBand --verbose --coverage --coverageDirectory=coverage/jest",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix"
|
||||
},
|
||||
|
||||
@@ -5,7 +5,6 @@ const ewelink = require('../main');
|
||||
const {
|
||||
singleChannelDeviceId,
|
||||
deviceIdWithPower,
|
||||
deviceIdWithTempAndHum,
|
||||
fourChannelsDevice,
|
||||
} = require('./_setup/credentials.json');
|
||||
|
||||
@@ -68,30 +67,10 @@ describe('invalid credentials', () => {
|
||||
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
|
||||
const powerUsage = await conn.getDevicePowerUsage(deviceIdWithPower);
|
||||
expect(typeof powerUsage).toBe('object');
|
||||
expect(powerUsage.msg).toBe('Authentication error');
|
||||
expect(powerUsage.error).toBe(401);
|
||||
expect(powerUsage.msg).toBe('Forbidden');
|
||||
expect(powerUsage.error).toBe(403);
|
||||
});
|
||||
|
||||
// test('get device current temperature should fail', async () => {
|
||||
// const conn = new ewelink({ email: 'invalid', password: 'credentials' });
|
||||
// const powerState = await conn.getDeviceCurrentTemperature(
|
||||
// deviceIdWithTempAndHum
|
||||
// );
|
||||
// expect(typeof powerState).toBe('object');
|
||||
// expect(powerUsage.msg).toBe('Authentication error');
|
||||
// expect(powerUsage.error).toBe(401);
|
||||
// });
|
||||
|
||||
// test('get device current humidity should fail', async () => {
|
||||
// const conn = new ewelink({ email: 'invalid', password: 'credentials' });
|
||||
// const powerState = await conn.getDeviceCurrentHumidity(
|
||||
// deviceIdWithTempAndHum
|
||||
// );
|
||||
// expect(typeof powerState).toBe('object');
|
||||
// expect(powerUsage.msg).toBe('Authentication error');
|
||||
// expect(powerUsage.error).toBe(401);
|
||||
// });
|
||||
|
||||
test('get channel count 1 should fail', async () => {
|
||||
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
|
||||
const switchesAmount = await conn.getDeviceChannelCount(
|
||||
|
||||
@@ -68,7 +68,8 @@ describe('valid credentials, invalid device', () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
const powerUsage = await conn.getDeviceRawPowerUsage('invalid deviceid');
|
||||
expect(typeof powerUsage).toBe('object');
|
||||
expect(powerUsage.error).toBe('No power usage data found.');
|
||||
expect(powerUsage.msg).toBe('Forbidden');
|
||||
expect(powerUsage.error).toBe(403);
|
||||
});
|
||||
|
||||
test('current month power usage on invalid device should fail', async () => {
|
||||
@@ -76,7 +77,8 @@ describe('valid credentials, invalid device', () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
const powerUsage = await conn.getDevicePowerUsage('invalid deviceid');
|
||||
expect(typeof powerUsage).toBe('object');
|
||||
expect(powerUsage.error).toBe('No power usage data found.');
|
||||
expect(powerUsage.msg).toBe('Forbidden');
|
||||
expect(powerUsage.error).toBe(403);
|
||||
});
|
||||
|
||||
test('raw power on device without electricity monitor should fail', async () => {
|
||||
@@ -87,24 +89,6 @@ describe('valid credentials, invalid device', () => {
|
||||
expect(powerUsage.error).toBe('No power usage data found.');
|
||||
});
|
||||
|
||||
test('get device current temperature should fail', async () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
const temperature = await conn.getDeviceCurrentTemperature(
|
||||
'invalid deviceid'
|
||||
);
|
||||
expect(typeof temperature).toBe('object');
|
||||
expect(temperature.msg).toBe('Device does not exist');
|
||||
expect(temperature.error).toBe(500);
|
||||
});
|
||||
|
||||
test('get device current humidity should fail', async () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
const humidity = await conn.getDeviceCurrentHumidity('invalid deviceid');
|
||||
expect(typeof humidity).toBe('object');
|
||||
expect(humidity.msg).toBe('Device does not exist');
|
||||
expect(humidity.error).toBe(500);
|
||||
});
|
||||
|
||||
test('get channel count should fail', async () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
const switchesAmount = await conn.getDeviceChannelCount('invalid deviceid');
|
||||
|
||||
@@ -1,45 +1,58 @@
|
||||
const delay = require('delay');
|
||||
|
||||
const ewelink = require('../main');
|
||||
|
||||
const {
|
||||
email,
|
||||
password,
|
||||
deviceIdWithTempAndHum,
|
||||
deviceIdWithTempAndHum: thDevice,
|
||||
} = require('./_setup/credentials.json');
|
||||
|
||||
describe.skip('current temerature and humidity: node script', () => {
|
||||
describe('current temperature and humidity: node script', () => {
|
||||
let conn;
|
||||
let device;
|
||||
|
||||
beforeAll(async () => {
|
||||
conn = new ewelink({ email, password });
|
||||
await conn.login();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await delay(1000);
|
||||
device = await conn.getDevice(thDevice);
|
||||
});
|
||||
|
||||
test('should return current temperature/humidity', async () => {
|
||||
const { currentTemperature, currentHumidity } = device.params;
|
||||
const result = await conn.getDeviceCurrentTH(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.status).toBe('ok');
|
||||
expect(result.temperature).toBe(currentTemperature);
|
||||
expect(result.humidity).toBe(currentHumidity);
|
||||
});
|
||||
|
||||
test('should return current temperature', async () => {
|
||||
const device = await conn.getDevice(deviceIdWithTempAndHum);
|
||||
const { currentTemperature } = device.params;
|
||||
const temperature = await conn.getDeviceCurrentTemperature(
|
||||
deviceIdWithTempAndHum
|
||||
);
|
||||
expect(typeof temperature).toBe('object');
|
||||
expect(temperature.status).toBe('ok');
|
||||
expect(temperature.state).toBe(currentTemperature);
|
||||
const result = await conn.getDeviceCurrentTemperature(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.status).toBe('ok');
|
||||
expect(result.temperature).toBe(currentTemperature);
|
||||
});
|
||||
|
||||
test('should return current humidity', async () => {
|
||||
const device = await conn.getDevice(deviceIdWithTempAndHum);
|
||||
const { currentHumidity } = device.params;
|
||||
const humidity = await conn.getDeviceCurrentHumidity(
|
||||
deviceIdWithTempAndHum
|
||||
);
|
||||
expect(typeof humidity).toBe('object');
|
||||
expect(humidity.status).toBe('ok');
|
||||
expect(humidity.state).toBe(currentHumidity);
|
||||
const result = await conn.getDeviceCurrentHumidity(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.status).toBe('ok');
|
||||
expect(result.humidity).toBe(currentHumidity);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skip('current temerature and humidity: serverless', () => {
|
||||
describe('current temperature and humidity: serverless', () => {
|
||||
let accessToken;
|
||||
let apiKey;
|
||||
let connSL;
|
||||
let device;
|
||||
|
||||
beforeAll(async () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
@@ -48,27 +61,70 @@ describe.skip('current temerature and humidity: serverless', () => {
|
||||
apiKey = login.user.apikey;
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await delay(1000);
|
||||
connSL = new ewelink({ at: accessToken, apiKey });
|
||||
device = await connSL.getDevice(thDevice);
|
||||
});
|
||||
|
||||
test('should return current temperature/humidity', async () => {
|
||||
const { currentTemperature, currentHumidity } = device.params;
|
||||
const result = await connSL.getDeviceCurrentTH(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.status).toBe('ok');
|
||||
expect(result.temperature).toBe(currentTemperature);
|
||||
expect(result.humidity).toBe(currentHumidity);
|
||||
});
|
||||
|
||||
test('should return current temperature', async () => {
|
||||
const conn = new ewelink({ at: accessToken, apiKey });
|
||||
const device = await conn.getDevice(deviceIdWithTempAndHum);
|
||||
const { currentTemperature } = device.params;
|
||||
const temperature = await conn.getDeviceCurrentTemperature(
|
||||
deviceIdWithTempAndHum
|
||||
);
|
||||
expect(typeof temperature).toBe('object');
|
||||
expect(temperature.status).toBe('ok');
|
||||
expect(temperature.state).toBe(currentTemperature);
|
||||
const result = await connSL.getDeviceCurrentTemperature(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.status).toBe('ok');
|
||||
expect(result.temperature).toBe(currentTemperature);
|
||||
});
|
||||
|
||||
test('should return current humidity', async () => {
|
||||
const conn = new ewelink({ at: accessToken, apiKey });
|
||||
const device = await conn.getDevice(deviceIdWithTempAndHum);
|
||||
const { currentHumidity } = device.params;
|
||||
const humidity = await conn.getDeviceCurrentHumidity(
|
||||
deviceIdWithTempAndHum
|
||||
);
|
||||
expect(typeof humidity).toBe('object');
|
||||
expect(humidity.status).toBe('ok');
|
||||
expect(humidity.state).toBe(currentHumidity);
|
||||
const result = await connSL.getDeviceCurrentHumidity(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.status).toBe('ok');
|
||||
expect(result.humidity).toBe(currentHumidity);
|
||||
});
|
||||
});
|
||||
|
||||
describe('current temperature and humidity: invalid device', () => {
|
||||
test('get device current temperature should fail', async () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
const temperature = await conn.getDeviceCurrentTemperature('invalid');
|
||||
expect(typeof temperature).toBe('object');
|
||||
expect(temperature.msg).toBe('Device does not exist');
|
||||
expect(temperature.error).toBe(500);
|
||||
});
|
||||
|
||||
test('get device current humidity should fail', async () => {
|
||||
const conn = new ewelink({ email, password });
|
||||
const humidity = await conn.getDeviceCurrentHumidity('invalid');
|
||||
expect(typeof humidity).toBe('object');
|
||||
expect(humidity.msg).toBe('Device does not exist');
|
||||
expect(humidity.error).toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('current temperature and humidity: invalid credentials', () => {
|
||||
test('get device current temperature should fail', async () => {
|
||||
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
|
||||
const result = await conn.getDeviceCurrentTemperature(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.msg).toBe('Authentication error');
|
||||
expect(result.error).toBe(401);
|
||||
});
|
||||
|
||||
test('get device current humidity should fail', async () => {
|
||||
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
|
||||
const result = await conn.getDeviceCurrentHumidity(thDevice);
|
||||
expect(typeof result).toBe('object');
|
||||
expect(result.msg).toBe('Authentication error');
|
||||
expect(result.error).toBe(401);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user