Release v1.7.0 (#20)

* Add typescript typings for main.js (#19)

* improved error message on temp/humidity devices

* Update index.d.ts

Co-Authored-By: ducpmm <49466191+ducpmm@users.noreply.github.com>
This commit is contained in:
Martin M
2019-10-18 20:49:18 -03:00
committed by GitHub
parent bff733adc5
commit 2da94b6971
5 changed files with 260 additions and 7 deletions

223
index.d.ts vendored Normal file
View File

@@ -0,0 +1,223 @@
// Type definitions for ewelink-api
// Definitions by: Alexander Méhes https://github.com/BMXsanko
declare module 'ewelink-api' {
export default eWelink;
class eWelink {
constructor({ }: { email: string; password: string; } | { at: string; } | { at: string; apiKey: string; });
/**
* Login into eWeLink API and get auth credentials.
*/
login(): Promise<LoginInfo>
/**
* Opens a socket connection to eWeLink and listen for real-time events.
*/
openWebSocket(callback: (data: {}) => void): Promise<any>
/**
* Returns a list of devices associated to logged account.
*/
getDevices(): Promise<Device[]>
/**
* Return information for specified device.
*/
getDevice(deviceId: string): Promise<Device>
/**
* Query for specified device power status.
*/
getDevicePowerState(deviceId: string, channel?: number): Promise<DeviceState>
/**
* Change specified device power state.
*/
setDevicePowerState(deviceId: string, state?: string, channel?: number): Promise<DeviceState>
/**
* Switch specified device current power state.
*/
toggleDevice(deviceId: string, channel?: number): Promise<DeviceState>
/**
* Returns current month power usage on device who supports electricity records, like Sonoff POW.
*/
getDevicePowerUsage(deviceId: string): Promise<PowerUsage>
/**
* Return current temperature and humidity for specified device.
*/
getDeviceCurrentTH(deviceId: string): Promise<TemperatureHumidity>
/**
* Return current temperature for specified device.
*/
getDeviceCurrentTemperature(deviceId: string): Promise<TemperatureHumidity>
/**
* Return current temperature for specified device.
*/
getDeviceCurrentHumidity(deviceId: string): Promise<TemperatureHumidity>
/**
* Return total channels for specified device.
*/
getDeviceChannelCount(deviceId: string): Promise<SwitchCount>
/**
* Return firmware version for specified device.
*/
getFirmwareVersion(deviceId: string): Promise<FirmwareVersion>
}
export interface Device {
_id: string;
name: string;
type: string;
deviceid: string;
apikey: string;
extra: Extra2;
__v: number;
onlineTime: string;
ip: string;
location: string;
offlineTime: string;
deviceStatus: string;
tags: Tags;
settings: Settings;
devGroups: any[];
groups: any[];
params: Params;
online: boolean;
createdAt: string;
group: string;
sharedTo: any[];
devicekey: string;
deviceUrl: string;
brandName: string;
showBrand: boolean;
brandLogoUrl: string;
productModel: string;
devConfig: DevConfig;
uiid: number;
}
export interface DevConfig {
}
export interface Params {
pulseWidth: number;
pulse: string;
init: number;
sledOnline: string;
version: number;
timers: any[];
controlType: string;
partnerApikey: string;
bindInfos: BindInfos;
rssi: number;
staMac: string;
startup: string;
fwVersion: string;
switch: string;
}
export interface BindInfos {
gaction: string[];
}
export interface Settings {
alarmNotify: number;
opsHistory: number;
opsNotify: number;
}
export interface Tags {
m_4434_sany: string;
}
export interface Extra2 {
_id: string;
extra: Extra;
}
export interface Extra {
description: string;
brandId: string;
apmac: string;
mac: string;
ui: string;
modelInfo: string;
model: string;
manufacturer: string;
uiid: number;
staMac: string;
chipid: string;
}
export interface DeviceState {
status?: string;
state?: string;
error?: number;
msg?: string;
}
export interface SwitchCount {
status?: string;
switchesAmount?: number;
error?: number;
msg?: string;
}
export interface TemperatureHumidity {
status?: string;
temperature?: number;
humidity?: number;
error?: number;
msg?: string;
}
export interface PowerUsage {
status?: string;
monthly?: number;
daily?: Daily[];
error?: number;
msg?: string;
}
export interface FirmwareVersion {
status?: string;
fwVersion?: string;
error?: number;
msg?: string;
}
export interface LoginInfo {
at: string;
rt: string;
user: User;
region: string;
}
export interface User {
_id: string;
email: string;
appId: string;
lang: string;
online: boolean;
onlineTime: string;
ip: string;
location: string;
offlineTime: string;
userStatus: string;
appInfos: AppInfo[];
isAccepEmailAd: boolean;
bindInfos: LoginBindInfos;
createdAt: string;
apikey: string;
}
export interface LoginBindInfos {
gaction: string[];
}
export interface AppInfo {
appVersion: string;
os: string;
}
export interface Daily {
day: number;
usage: number;
}
}

View File

@@ -13,11 +13,12 @@ const getTHMixin = {
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' };
if (error) {
return device;
}
if (!temperature || !humidity) {
return { error: 500, msg: "Can't read sensor data from device" };
}
const data = { status: 'ok', temperature, humidity };

View File

@@ -4,6 +4,7 @@
"description": "eWeLink API for Node.js",
"author": "Martín M.",
"license": "MIT",
"typings": "./index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/skydiver/ewelink-api.git"

View File

@@ -5,5 +5,6 @@
"deviceIdWithPower": "<your device id>",
"deviceIdWithoutPower": "<your device id>",
"deviceIdWithTempAndHum": "<your device id>",
"deviceIdWithoutTempAndHum": "<your device id>",
"fourChannelsDevice": "<your device id>"
}

View File

@@ -5,6 +5,7 @@ const ewelink = require('../main');
const {
email,
password,
deviceIdWithoutTempAndHum,
deviceIdWithTempAndHum: thDevice,
} = require('./_setup/credentials.json');
@@ -98,7 +99,9 @@ describe('current temperature and humidity: invalid device', () => {
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.msg).toBe(
"TypeError: Cannot read property 'apikey' of null"
);
expect(temperature.error).toBe(500);
});
@@ -106,7 +109,31 @@ describe('current temperature and humidity: invalid device', () => {
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.msg).toBe(
"TypeError: Cannot read property 'apikey' of null"
);
expect(humidity.error).toBe(500);
});
});
describe('current temperature and humidity: device without sensor', () => {
test('get device current temperature should fail', async () => {
const conn = new ewelink({ email, password });
const temperature = await conn.getDeviceCurrentTemperature(
deviceIdWithoutTempAndHum
);
expect(typeof temperature).toBe('object');
expect(temperature.msg).toBe("Can't read sensor data from device");
expect(temperature.error).toBe(500);
});
test('get device current humidity should fail', async () => {
const conn = new ewelink({ email, password });
const humidity = await conn.getDeviceCurrentHumidity(
deviceIdWithoutTempAndHum
);
expect(typeof humidity).toBe('object');
expect(humidity.msg).toBe("Can't read sensor data from device");
expect(humidity.error).toBe(500);
});
});