From 2da94b69719ab34a7238c1c52f599c6673efaa44 Mon Sep 17 00:00:00 2001 From: Martin M Date: Fri, 18 Oct 2019 20:49:18 -0300 Subject: [PATCH] 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> --- index.d.ts | 223 ++++++++++++++++++++++++++++++ mixins/temphumd/getTHMixin.js | 11 +- package.json | 1 + test/_setup/credentials.json | 1 + test/temperature-humidity.spec.js | 31 ++++- 5 files changed, 260 insertions(+), 7 deletions(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..24c5550 --- /dev/null +++ b/index.d.ts @@ -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 + /** + * Opens a socket connection to eWeLink and listen for real-time events. + */ + openWebSocket(callback: (data: {}) => void): Promise + /** + * Returns a list of devices associated to logged account. + */ + getDevices(): Promise + /** + * Return information for specified device. + */ + getDevice(deviceId: string): Promise + /** + * Query for specified device power status. + */ + getDevicePowerState(deviceId: string, channel?: number): Promise + /** + * Change specified device power state. + */ + setDevicePowerState(deviceId: string, state?: string, channel?: number): Promise + /** + * Switch specified device current power state. + */ + toggleDevice(deviceId: string, channel?: number): Promise + /** + * Returns current month power usage on device who supports electricity records, like Sonoff POW. + */ + getDevicePowerUsage(deviceId: string): Promise + /** + * Return current temperature and humidity for specified device. + */ + getDeviceCurrentTH(deviceId: string): Promise + /** + * Return current temperature for specified device. + */ + getDeviceCurrentTemperature(deviceId: string): Promise + /** + * Return current temperature for specified device. + */ + getDeviceCurrentHumidity(deviceId: string): Promise + /** + * Return total channels for specified device. + */ + getDeviceChannelCount(deviceId: string): Promise + /** + * Return firmware version for specified device. + */ + getFirmwareVersion(deviceId: string): Promise + } + + 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; + } +} diff --git a/mixins/temphumd/getTHMixin.js b/mixins/temphumd/getTHMixin.js index 3b9f84d..42a1904 100644 --- a/mixins/temphumd/getTHMixin.js +++ b/mixins/temphumd/getTHMixin.js @@ -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 }; diff --git a/package.json b/package.json index 5507608..fa7a48b 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/_setup/credentials.json b/test/_setup/credentials.json index 92ecd7c..9369819 100644 --- a/test/_setup/credentials.json +++ b/test/_setup/credentials.json @@ -5,5 +5,6 @@ "deviceIdWithPower": "", "deviceIdWithoutPower": "", "deviceIdWithTempAndHum": "", + "deviceIdWithoutTempAndHum": "", "fourChannelsDevice": "" } \ No newline at end of file diff --git a/test/temperature-humidity.spec.js b/test/temperature-humidity.spec.js index 546f16c..48a0300 100644 --- a/test/temperature-humidity.spec.js +++ b/test/temperature-humidity.spec.js @@ -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); }); });