codetheweb / tuyapi

🌧 An easy-to-use API for devices that use Tuya's cloud services. Documentation: https://codetheweb.github.io/tuyapi.
MIT License
2.08k stars 342 forks source link

Real time event push / notification #74

Closed NorthernMan54 closed 6 years ago

NorthernMan54 commented 6 years ago

I was looking at the interaction model of this versus the app “ce smart” and the app updates in real time when a change occurs at the device, ie push the button to turn on. Has anyone looked into this interaction? Does the device send these as events as udp packets? Or do you need to maintain a socket connection? Or is this a cloud only feature via mqtt?

PS Great job on this, I grabbed some “CE Smart” WiFi Dimmers from Costco Canada and was able to get them working in Homebridge pretty quickly. Also expect a pull request shortly for your homebridge plugin to add support for dimmers, and status polling if real time events is not feasible.

codetheweb commented 6 years ago

From what I remember, the app gets push updates from the MQTT channel. But I think devices also send updates over the local network with UDP if you keep a socket open. Adding some kind of status emitter event to tuyapi would be pretty useful.

Thanks for contributing to the Homebridge plugin.

NorthernMan54 commented 6 years ago

Okay, have done some more digging and if you hold open the TCP connection on port 6668 the device will send real time events for any local changes.

After a quick hack of the code to hold the socket open, this is the traffic I received when pressing buttons on the dimmer. The '1' is on/off and the '3' is the dimmer side of my wifi dimmer.

Give me a couple of days to add a status event emitter to tuyapi, and the listener on the homebridge side.

 TuyAPI decrypted { devId: '07200106bcddc2ef159d',
  dps: { '1': false },
  t: 1535423039,
  s: 66 } +0ms
  TuyAPI Received data back: +1s
  TuyAPI 000055aa00000000000000080000008b00000000332e316664613838343963353738356238666566484559426676383957426270304a4f432f6c35794e4c577146715566455235342f47724d45444c42334e2b5747776e515247663955744b4e7852764a714c7830624c64596d586658376a504262564f54496a386273534271743078687974417355594a65564a324141343d9b4a33df0000aa55 +0ms
  TuyAPI decrypted { devId: '07200106bcddc2ef159d',
  dps: { '1': true },
  t: 1535423040,
  s: 67 } +1ms
  TuyAPI Received data back: +5s
  TuyAPI 000055aa00000000000000080000008b00000000332e316538323864333863643534653038323866484559426676383957426270304a4f432f6c35794e4c577146715566455235342f47724d45444c42334e304951615544584c616c532b534d727a4a737666323057456731756b46355968722f3251424e732b385a42525155596c35544b6545484930476f7174592f44453da44754170000aa55 +0ms
  TuyAPI decrypted { devId: '07200106bcddc2ef159d',
  dps: { '3': 97 },
  t: 1535423045,
  s: 68 } +1ms
NorthernMan54 commented 6 years ago

I have this running now as a poc with a couple of devices and it works really slick. You can turn on and off or bright/dim the wall switch and it updates in real time in HomeKit. As this pattern causes a switch to an asynchronous interaction model with the device, i need to rethink the get function as it is now redundant.

codetheweb commented 6 years ago

👍 sounds like great work, thanks.

I actually never tested this (and currently can't because I'm away from home), but in its current state doesn't the Homebridge plugin update within a few seconds?

I'm fairly sure Homebridge automatically polls plugins.

Apollon77 commented 6 years ago

see #88

codetheweb commented 6 years ago

Closing as #88 is now merged.