Release v1.5.1 (#14)

* updated dependencies

* disabled temp/humidity test

* refactor big main class into smaller mixins

* updated jest config

* version bump
This commit is contained in:
Martin M
2019-09-14 22:12:56 -03:00
committed by GitHub
parent 5b4361a23d
commit 1cd87a8d90
15 changed files with 1275 additions and 2572 deletions

318
main.js
View File

@@ -2,16 +2,10 @@ const rp = require('request-promise');
const { _get } = require('./lib/helpers');
const {
makeAuthorizationSign,
getDeviceChannelCount,
} = require('./lib/ewelink-helper');
const { makeAuthorizationSign } = require('./lib/ewelink-helper');
const payloads = require('./lib/payloads');
const { ChangeState } = require('./classes/PowerState');
const { DeviceRaw, CurrentMonth } = require('./classes/PowerUsage');
class eWeLink {
constructor({ region = 'us', email, password, at, apiKey }) {
if (!at && (!email && !password)) {
@@ -127,270 +121,52 @@ class eWeLink {
await this.login();
}
}
/**
* Get specific device information
*
* @returns {Promise<{msg: string, error: *}>}
*/
async getDevices() {
return this.makeRequest({
uri: '/user/device',
qs: { lang: 'en', getTags: 1 },
});
}
/**
* Get information about all associated devices to account
*
* @param deviceId
*
* @returns {Promise<{msg: string, error: *}>}
*/
async getDevice(deviceId) {
return this.makeRequest({
uri: `/user/device/${deviceId}`,
qs: { lang: 'en', getTags: 1 },
});
}
/**
* Get current power state for a specific device
*
* @param deviceId
* @param channel
*
* @returns {Promise<{state: *, status: string}|{msg: string, error: *}>}
*/
async getDevicePowerState(deviceId, channel = 1) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const uiid = _get(device, 'extra.extra.uiid', false);
let state = _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 || (!state && !switches)) {
if (error && parseInt(error) === 401) {
return device;
}
return { error, msg: 'Device does not exist' };
}
if (switches) {
state = switches[channel - 1].switch;
}
return { status: 'ok', state };
}
/**
* 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, 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;
}
return ChangeState.set({
apiUrl: this.getApiWebSocket(),
at: this.at,
apiKey: this.apiKey,
deviceId,
params,
state: stateToSwitch,
});
}
/**
* Toggle power state for a specific device
*
* @param deviceId
* @param channel
*
* @returns {Promise<{state: *, status: string}|{msg: string, error: *}>}
*/
async toggleDevice(deviceId, channel = 1) {
return this.setDevicePowerState(deviceId, 'toggle', channel);
}
/**
* Get device raw power usage
*
* @param deviceId
*
* @returns {Promise<{error: string}|{response: {hundredDaysKwhData: *}, status: string}>}
*/
async getDeviceRawPowerUsage(deviceId) {
await this.logIfNeeded();
return DeviceRaw.get({
apiUrl: this.getApiWebSocket(),
at: this.at,
apiKey: this.apiKey,
deviceId,
});
}
/**
* Get device power usage for current month
*
* @param deviceId
*
* @returns {Promise<{error: string}|{daily: *, monthly: *}>}
*/
async getDevicePowerUsage(deviceId) {
const response = await this.getDeviceRawPowerUsage(deviceId);
const error = _get(response, 'error', false);
const hundredDaysKwhData = _get(response, 'data.hundredDaysKwhData', false);
if (error) {
return response;
}
return {
status: 'ok',
...CurrentMonth.parse({ hundredDaysKwhData }),
};
}
/**
* 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 };
}
/**
* 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 };
}
/**
* Get device channel count
*
* @param deviceId
*
* @returns {Promise<{switchesAmount: *, status: string}
*/
async getDeviceChannelCount(deviceId) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const uiid = _get(device, 'extra.extra.uiid', false);
const switchesAmount = getDeviceChannelCount(uiid);
if (error) {
if (error && parseInt(error) === 401) {
return device;
}
return { error, msg: 'Device does not exist' };
}
return { status: 'ok', switchesAmount };
}
/**
* Get device firmware version
*
* @param deviceId
*
* @returns {Promise<{fwVersion: *, status: string}|{msg: string, error: *}>}
*/
async getFirmwareVersion(deviceId) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const fwVersion = _get(device, 'params.fwVersion', false);
if (error || !fwVersion) {
if (error && parseInt(error) === 401) {
return device;
}
return { error, msg: 'Device does not exist' };
}
return { status: 'ok', fwVersion };
}
}
/* LOAD MIXINS: power state */
const getDevicePowerStateMixin = require('./mixins/powerState/getDevicePowerStateMixin');
const setDevicePowerState = require('./mixins/powerState/setDevicePowerStateMixin');
const toggleDeviceMixin = require('./mixins/powerState/toggleDeviceMixin');
/* LOAD MIXINS: power usage */
const getDevicePowerUsageMixin = require('./mixins/powerUsage/getDevicePowerUsageMixin');
const getDeviceRawPowerUsageMixin = require('./mixins/powerUsage/getDeviceRawPowerUsageMixin');
/* LOAD MIXINS: temperature & humidity */
const getDeviceCurrentTemperatureMixin = require('./mixins/temperature/getDeviceCurrentTemperatureMixin');
const getDeviceCurrentHumidityMixin = require('./mixins/humidity/getDeviceCurrentHumidityMixin');
/* LOAD MIXINS: devices */
const getDevicesMixin = require('./mixins/devices/getDevicesMixin');
const getDeviceMixin = require('./mixins/devices/getDeviceMixin');
const getDeviceChannelCountMixin = require('./mixins/devices/getDeviceChannelCountMixin');
const getFirmwareVersionMixin = require('./mixins/devices/getFirmwareVersionMixin');
Object.assign(
eWeLink.prototype,
getDevicePowerStateMixin,
setDevicePowerState,
toggleDeviceMixin
);
Object.assign(
eWeLink.prototype,
getDevicePowerUsageMixin,
getDeviceRawPowerUsageMixin
);
Object.assign(
eWeLink.prototype,
getDeviceCurrentTemperatureMixin,
getDeviceCurrentHumidityMixin
);
Object.assign(
eWeLink.prototype,
getDevicesMixin,
getDeviceMixin,
getDeviceChannelCountMixin,
getFirmwareVersionMixin
);
module.exports = eWeLink;

View File

@@ -0,0 +1,30 @@
const { _get } = require('../../lib/helpers');
const { getDeviceChannelCount } = require('../../lib/ewelink-helper');
const getDeviceChannelCountMixin = {
/**
* Get device channel count
*
* @param deviceId
*
* @returns {Promise<{switchesAmount: *, status: string}|{msg: string, error: *}>}
*/
async getDeviceChannelCount(deviceId) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const uiid = _get(device, 'extra.extra.uiid', false);
const switchesAmount = getDeviceChannelCount(uiid);
if (error) {
if (error && parseInt(error) === 401) {
return device;
}
return { error, msg: 'Device does not exist' };
}
return { status: 'ok', switchesAmount };
},
};
module.exports = getDeviceChannelCountMixin;

View File

@@ -0,0 +1,17 @@
const getDeviceMixin = {
/**
* Get information about all associated devices to account
*
* @param deviceId
*
* @returns {Promise<{msg: string, error: *}>}
*/
async getDevice(deviceId) {
return this.makeRequest({
uri: `/user/device/${deviceId}`,
qs: { lang: 'en', getTags: 1 },
});
},
};
module.exports = getDeviceMixin;

View File

@@ -0,0 +1,15 @@
const getDevicesMixin = {
/**
* Get specific device information
*
* @returns {Promise<{msg: string, error: *}>}
*/
async getDevices() {
return this.makeRequest({
uri: '/user/device',
qs: { lang: 'en', getTags: 1 },
});
},
};
module.exports = getDevicesMixin;

View File

@@ -0,0 +1,27 @@
const { _get } = require('../../lib/helpers');
const getFirmwareVersionMixin = {
/**
* Get device firmware version
*
* @param deviceId
*
* @returns {Promise<{fwVersion: *, status: string}|{msg: string, error: *}>}
*/
async getFirmwareVersion(deviceId) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const fwVersion = _get(device, 'params.fwVersion', false);
if (error || !fwVersion) {
if (error && parseInt(error) === 401) {
return device;
}
return { error, msg: 'Device does not exist' };
}
return { status: 'ok', fwVersion };
},
};
module.exports = getFirmwareVersionMixin;

View File

@@ -0,0 +1,28 @@
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;

View File

@@ -0,0 +1,43 @@
const { _get } = require('../../lib/helpers');
const { getDeviceChannelCount } = require('../../lib/ewelink-helper');
const getDevicePowerStateMixin = {
/**
* Get current power state for a specific device
*
* @param deviceId
* @param channel
*
* @returns {Promise<{state: *, status: string}|{msg: string, error: *}>}
*/
async getDevicePowerState(deviceId, channel = 1) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const uiid = _get(device, 'extra.extra.uiid', false);
let state = _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 || (!state && !switches)) {
if (error && parseInt(error) === 401) {
return device;
}
return { error, msg: 'Device does not exist' };
}
if (switches) {
state = switches[channel - 1].switch;
}
return { status: 'ok', state };
},
};
module.exports = getDevicePowerStateMixin;

View File

@@ -0,0 +1,66 @@
const { _get } = require('../../lib/helpers');
const { getDeviceChannelCount } = require('../../lib/ewelink-helper');
const { ChangeState } = 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 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;
}
return ChangeState.set({
apiUrl: this.getApiWebSocket(),
at: this.at,
apiKey: this.apiKey,
deviceId,
params,
state: stateToSwitch,
});
},
};
module.exports = setDevicePowerState;

View File

@@ -0,0 +1,15 @@
const toggleDeviceMixin = {
/**
* Toggle power state for a specific device
*
* @param deviceId
* @param channel
*
* @returns {Promise<{state: *, status: string}|{msg: string, error: *}>}
*/
async toggleDevice(deviceId, channel = 1) {
return this.setDevicePowerState(deviceId, 'toggle', channel);
},
};
module.exports = toggleDeviceMixin;

View File

@@ -0,0 +1,30 @@
const { _get } = require('../../lib/helpers');
const { CurrentMonth } = require('../../classes/PowerUsage');
const getDevicePowerUsageMixin = {
/**
* Get device power usage for current month
*
* @param deviceId
*
* @returns {Promise<{error: string}|{daily: *, monthly: *}>}
*/
async getDevicePowerUsage(deviceId) {
const response = await this.getDeviceRawPowerUsage(deviceId);
const error = _get(response, 'error', false);
const hundredDaysKwhData = _get(response, 'data.hundredDaysKwhData', false);
if (error) {
return response;
}
return {
status: 'ok',
...CurrentMonth.parse({ hundredDaysKwhData }),
};
},
};
module.exports = getDevicePowerUsageMixin;

View File

@@ -0,0 +1,23 @@
const { DeviceRaw } = require('../../classes/PowerUsage');
const getDeviceRawPowerUsageMixin = {
/**
* Get device raw power usage
*
* @param deviceId
*
* @returns {Promise<{error: string}|{response: {hundredDaysKwhData: *}, status: string}>}
*/
async getDeviceRawPowerUsage(deviceId) {
await this.logIfNeeded();
return DeviceRaw.get({
apiUrl: this.getApiWebSocket(),
at: this.at,
apiKey: this.apiKey,
deviceId,
});
},
};
module.exports = getDeviceRawPowerUsageMixin;

View File

@@ -0,0 +1,28 @@
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;

3184
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "ewelink-api",
"version": "1.5.0",
"version": "1.5.1",
"description": "eWeLink API for Node.js",
"author": "Martín M.",
"license": "MIT",
@@ -21,6 +21,7 @@
"main": "main.js",
"scripts": {
"test": "jest --runInBand --verbose",
"coverage": "jest --runInBand --verbose --coverage --coverageDirectory=output/coverage/jest",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
@@ -28,6 +29,10 @@
"testPathIgnorePatterns": [
"/node_modules/",
".cache"
],
"coverageReporters": [
"text",
"html"
]
},
"dependencies": {
@@ -36,22 +41,22 @@
"random": "^2.1.1",
"request": "^2.88.0",
"request-promise": "^4.2.4",
"websocket": "^1.0.29",
"websocket": "^1.0.30",
"websocket-as-promised": "^0.10.1"
},
"devDependencies": {
"babel-eslint": "^10.0.2",
"eslint": "^6.1.0",
"eslint-config-airbnb": "^17.1.1",
"eslint-config-prettier": "^6.0.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.4.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.3.0",
"eslint-config-wesbos": "0.0.19",
"eslint-plugin-html": "^6.0.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^1.6.1",
"jest": "^24.8.0",
"eslint-plugin-react-hooks": "^2.0.1",
"jest": "^24.9.0",
"prettier": "^1.18.2"
}
}

View File

@@ -37,7 +37,7 @@ describe.skip('current temerature and humidity: node script', () => {
});
});
describe('current temerature and humidity: serverless', () => {
describe.skip('current temerature and humidity: serverless', () => {
let accessToken;
let apiKey;