jasonacox / tinytuya

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

Break socket recv() out into its own function #153

Closed uzlonewolf closed 2 years ago

uzlonewolf commented 2 years ago

This PR:

  1. Moves self.socket.recv() into its own function
  2. Makes the recv() only read the header and payload of a single message
  3. Parses the message into the TuyaMessage() named tuple
  4. Moves the retcode into its own set of variables as it is not actually part of the header
  5. Spits out log.debug() messages if the header, footer, or CRC are wrong
  6. Misc other debugging message tweaks

Closes #152

jasonacox commented 2 years ago

Nice! Thanks @uzlonewolf - This is a very clean / elegant approach and I'm trying to remember why we/I didn't address this already. I remember having it in my TODO list to address the multi-palyload condition.

On the second bit, I understand and appreciate the exception approach. I can see that as a breaking change for some users. I want to run through some tests to see how serious that would be before decide on version increment, etc.

Thank you!

uzlonewolf commented 2 years ago

It shouldn't be a breaking change as it's only called from within the try/except block in _send_receive() - the except Exception as err: line will catch it and retry if there are any retries remaining.

Though now that I think about it, that retry may be a breaking change - previously, decode errors were caught a bit further down and resulted in a json error being returned instead of a retry. To restore this behavior we would just need to throw a specific exception and have a catch for it.