Closed ek5932 closed 1 year ago
Hi, Thanks for your work on this, does this need to be added to to all the requests or just in the handshake? I'm not entirely sure how requests.session works.
I think just the handshake should be sufficient as for subsequent it's OK to share the cookies but I'm no expert either - this is my first venture into Python :)
In my testing my device worked fine with just the single change.
Duplicate to #62
If you call Handshake() twice the second call will fail due to the contents of the response changing. This resulted in my HA module failing to access my switch due to the following error:
File "/config/custom_components/tapo_p100_control/switch.py", line 83, in update self._p100.handshake() File "/usr/local/lib/python3.9/site-packages/PyP100/PyP100.py", line 124, in handshake encryptedKey = r.json()["result"]["key"] KeyError: 'result'
I'm not sure why HA is making the call twice but there is a simple fix (I tried to raise a PR but I don't seem to have access to push to my own branch). If you change the handshake logic in PyP100.py @ line 122 to:
r = requests.session().post(URL, json=Payload, timeout=2)
This forces a new HTTP session for each request meaning that any subsequent calls always come back with the correct payload.