skydiver / ewelink-api

eWeLink API for JavaScript
https://www.npmjs.com/package/ewelink-api
MIT License
264 stars 108 forks source link

getDevicePowerUsage bad request error #133

Open miroberes opened 3 years ago

miroberes commented 3 years ago

Hi,

i could not get the daily power usage for my Pow_R2 from the API at first, but then got it working.

ewelink-api version 3.1.1

Current Behavior In DevicePowerUsageRaw.get(...) API returns {error: 400, reason: 'Bad Request', apikey: '...', sequence: '...', actionName: 'userOnline'}

Expected Behavior DevicePowerUsageRaw.get(...) returns valid response.

Returned value example: {status: 'ok', monthly: 62.77, daily: [{day: 28, usage: 0.86}, {day: 27, usage: 0.11}, ...]}

Possible reason: In DevicePowerUsageRaw the appid attribute in payloadLogin is undefined, as in DevicePowerUsageRaw this.APP_ID is undefined. const payloadLogin = wssLoginPayload({ at, apiKey, appid: this.APP_ID }); https://github.com/skydiver/ewelink-api/blob/5a07f6b6152d603c71266012177cc4b47d533bf9/src/classes/DevicePowerUsageRaw.js#L18

Possible Solution In getDevicePowerUsageRaw pass this.APP_ID to DevicePowerUsageRaw.get(...) as appid attribute in actionParams

Possible Implementation In getDevicePowerUsageRaw add appid: this.APP_ID, to actionParams:

const actionParams = {
  apiUrl: this.getApiWebSocket(),
  at: this.at,
  apiKey: this.apiKey,
  deviceId,
  appid: this.APP_ID,
};

https://github.com/skydiver/ewelink-api/blob/5a07f6b6152d603c71266012177cc4b47d533bf9/src/mixins/getDevicePowerUsageRaw.js#L17

In DevicePowerUsageRaw use the appid parameter

static async get({ apiUrl, at, apiKey, deviceId, appid }) { const payloadLogin = wssLoginPayload({ at, apiKey, appid });

https://github.com/skydiver/ewelink-api/blob/5a07f6b6152d603c71266012177cc4b47d533bf9/src/classes/DevicePowerUsageRaw.js#L17

alexjimenezes commented 3 years ago

Had the same issue in an EARU power device since the update to 3.5.0. Your solution solved it as well. Thanks!