nocmt / ewelink-api-next

CoolKit v2 API Official Library
MIT License
19 stars 4 forks source link

Error: 400 on lan switches request #11

Closed KonnosPetrovas closed 11 months ago

KonnosPetrovas commented 11 months ago

After creating the lan client with the api key, when I try to control my tx t2 sonoff switch I get this response and nothing happens on the switch:

Request result:  {
  status: 200,
  responseTime: 126,
  seq: 4,
  sequence: '1695127587222',
  error: 400
}

Am I inputing anything invalid in the switches method? This Is my code:

const eWeLink = require("ewelink-api-next").default;
(async () => {
  const lanClient = new eWeLink.Lan({
    selfApikey: "<my apikey>",
    logObj: eWeLink.createLogger("lan"),
  });

  lanClient.discovery((server) => {
    console.log("server:", server);
  }); // Start Discovery Service

  const iv = "0000000000000000";
  const secretKey = "81dc9bdb52d04dc20036dbd8313ed055";
  try {
    const res = await lanClient.zeroconf.switches({
      ip: "192.168.2.13",
      port: 8081,
      deviceId: "<the device id from the app>",
      data: {
        switches: [
          { switch: "off", outlet: 0 },
          { switch: "off", outlet: 1 },
          { switch: "off", outlet: 2 },
          { switch: "off", outlet: 3 },
        ],
      },
      encrypt: true,
      iv: iv,
      secretKey: secretKey,
    });
    console.info("Request result: ", res);
  } catch (error) {
    console.info(error.message);
  }
})();

The iv and secretKey I got from the lan test of the repo and I cannot find a way to get them from the device. Am I supposed to use the device key in the place of the secretkey? Also if I set encrypt to false I get this response:

Request result:  {
  status: 200,
  responseTime: 41,
  seq: 28,
  sequence: 'Invalid sequence',
  error: 401
}

What could be the cause of these errors?

KonnosPetrovas commented 11 months ago

I found out that you have to use a generated MD5 hash from the device key as the secret key for the encryption and now it works