fishbigger / TapoP100

A module for controlling the TP-Link Tapo P100 Plugs
MIT License
567 stars 139 forks source link

KeyError: 'result' #64

Closed ek5932 closed 1 year ago

ek5932 commented 2 years ago

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.

fishbigger commented 2 years 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.

ek5932 commented 2 years ago

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.

realzoulou commented 2 years ago

Duplicate to #62