jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
951 stars 172 forks source link

Questions about tuya automation with tinytuya. #429

Closed 3735943886 closed 10 months ago

3735943886 commented 10 months ago

I'll control tuya devices with tinytuya. The code will be structured into Monitor and Control sections. There are two points of interest:

  1. What happens if the persistent connection is lost? Does TinyTuya automatically recover its connection when available, or is there a need to add some code for reconnection?
  2. How to send control commands during monitoring? For instance, if one of the switch devices is connected with ‘persist on’ and is in a loop with Device.receive(), and an external sensor triggers the on/off control of this switch, how should this be handled? Should another connection be made to control this switch? Or can the already created tinytuya.Device object for monitoring be used to send control from outside of the loop while waiting for Device.receive()?

Thank you.

3735943886 commented 10 months ago
  1. Several tests were done, including a forced reboot of the AP. Following the reboot, the Device.receive() function returned an error Check device key or version. Tinytuya does not seem to automatically recover its connection, indicating that reconnection codes may need to be added when an error message is received from Device.receive().

  2. Commands were sent from outside of the loop using an already created tinytuya.Device object to a waiting device in a loop via Device.receive(). The Device.receive() function inside the loop was able to receive the dps signal without any issues. However, the efficacy of this approach remains uncertain.

Any comments or guidances are welcome. Thank you.

uzlonewolf commented 10 months ago

Hi @3735943886 !

  1. TinyTuya does automatically reconnect. Devices can sometimes take a while to re-connect to the WiFi, so if you get that error you can just wait a bit and retry the send/receive. Occasionally some devices will "forget" their local key and need to be power cycled before you can connect to them again.

  2. Yes, it's perfectly fine to use one Device object with 2 threads like that. Just make sure to set nowait=True in the sending calls to keep the sending thread from trying to receive as well.

3735943886 commented 10 months ago

Hi @3735943886 !

  1. TinyTuya does automatically reconnect. Devices can sometimes take a while to re-connect to the WiFi, so if you get that error you can just wait a bit and retry the send/receive. Occasionally some devices will "forget" their local key and need to be power cycled before you can connect to them again.
  2. Yes, it's perfectly fine to use one Device object with 2 threads like that. Just make sure to set nowait=True in the sending calls to keep the sending thread from trying to receive as well.

Thank you for your kind answer.