almottier / TapoP100

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

Overly tight timeout in `OldProtocol` leads to Exception: Failed to initialize protocol #14

Closed bentasker closed 11 months ago

bentasker commented 11 months ago

Just looking at trying to get this working with some plugs running older firmware

Running

p110 = PyP110.P110(ip, user, passw, preferred_protocol="old")

Leads to a set of exceptions being thrown

Traceback (most recent call last):
  File "/home/ben/.local/lib/python3.10/site-packages/PyP100/PyP100.py", line 32, in _initialize
    protocol.Initialize()
  File "/home/ben/.local/lib/python3.10/site-packages/PyP100/auth_protocol.py", line 244, in Initialize
    result = self._request_raw("handshake", {"key": public_key})
  File "/home/ben/.local/lib/python3.10/site-packages/PyP100/auth_protocol.py", line 165, in _request_raw
    resp = self.session.post(url, json=payload, timeout=0.5)
  File "/home/ben/.local/lib/python3.10/site-packages/requests/sessions.py", line 637, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
  File "/home/ben/.local/lib/python3.10/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ben/.local/lib/python3.10/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "/home/ben/.local/lib/python3.10/site-packages/requests/adapters.py", line 507, in send
    raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='192.168.3.152', port=80): Max retries exceeded with url: /app (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7fd032a0d720>, 'Connection to 192.168.3.152 timed out. (connect timeout=0.5)'))
Traceback (most recent call last):
  File "/home/ben/Documents/src.old/tplink_smartplug_to_influxdb/./app/collect.py", line 249, in <module>
    main()
  File "/home/ben/Documents/src.old/tplink_smartplug_to_influxdb/./app/collect.py", line 75, in main
    do_work(config, influxes)
  File "/home/ben/Documents/src.old/tplink_smartplug_to_influxdb/./app/collect.py", line 115, in do_work
    now_usage_w, today_usage = poll_tapo(tapo['ip'], config["tapo"]["user"], config["tapo"]["passw"])
  File "/home/ben/Documents/src.old/tplink_smartplug_to_influxdb/./app/collect.py", line 193, in poll_tapo
    p110.handshake() #Creates the cookies required for further methods
  File "/home/ben/.local/lib/python3.10/site-packages/PyP100/PyP100.py", line 48, in handshake
    self._initialize()
  File "/home/ben/.local/lib/python3.10/site-packages/PyP100/PyP100.py", line 39, in _initialize
    raise Exception("Failed to initialize protocol")
Exception: Failed to initialize protocol

That half-second timeout is hardcoded here.

I've edited ~/.local/lib/python3.10/site-packages/PyP100/auth_protocol.py to bump the timeout up to a second and no longer get that exception.

Might be worth extending the timeout a little (or making it configurable)

almottier commented 11 months ago

Sure. You can open a PR extending the timeout, I will merge it.

bentasker commented 11 months ago

Thanks, raised https://github.com/almottier/TapoP100/pull/15