utilities/tp-link-to-influxdb#9: Override timeouts set by PyP100



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: v0.25
Created: 22-Dec-23 11:40



Description

The Almottier fork of PyP100 contains some hardcoded timeouts for the old authentication mechanism. These are problematic for some of my plugs (those that are in slightly marginal wifi coverage are particularly impacted by this).

Once #7 is merged, I'd like to make the docker container use the Almottier fork of PyP100 by default, but I can't if that's going to result in polls regularly failing.

Although there's an upstream issue open, I want to see whether we can override the timeouts in the meantime.



Toggle State Changes

Activity


assigned to @btasker

The timeout is passed into requests.session.post():

# Execute call
        resp = self.session.post(url, json=payload, timeout=0.5)
        resp.raise_for_status()
        data = resp.json()

So there are two possible ways to go at this

  • Creating inheriting classes to get to this call and change it
  • Patch requests to ignore/change the timeout

The call that we need is a couple of classes deep, so if we went the inheriting route we'd need to create

  • PyP100.Device._initialize() - an exact copy other than referencing our amended auth class
  • PyP100.auth_protocol.OldProtocol._request_raw()

Both those functions contain a reasonable number of LOC, leaving us at risk of breakage if something changes in the upstream library. The risk of that is exceptionally high because the current install approach involves passing pip a reference to the master branch of the package's git repo.

We'd be taking on a hell of a maintenance burden.

I think it might be better to take aim at requests instead

With credit to stackoverflow for pointing me in the right direction:

'''
Interim fix added to address overly conservative timeouts used in an
upstream PyP100 module.

Monkey-patch `requests.session` so that it ignores timeouts set lower
than our desired threshold

Added in [utilities/tp-link-to-influxdb#9](/issue/utilities/tp-link-to-influxdb/9.html)
''' 
import requests
def request_set_timeout_bound(slf, *args, **kwargs):
    timeout = kwargs.pop('timeout', 5)
    if timeout < 3:
        timeout = 3
    return slf.request_orig(*args, **kwargs, timeout=timeout)

setattr(requests.sessions.Session, 'request_orig', requests.sessions.Session.request)
requests.sessions.Session.request = request_set_timeout_bound

This checks what timeout has been supplied:

  • If none supplied, defaults it to 5s
  • If supplied but less than 3, bump up to 3

Polling now works reliably

$ ./app/collect.py 
Plug: big-fridge using 173.886W, today: 0.831 kWh
Plug: slow-cooker using 0.0W, today: Not Supplied
Wrote 3 points to local

Hopefully, though, it won't be long before we can revert this.

Upstream PR 5 raised and merged.

mentioned in issue #7

Almost inevitably, just after merging this, I checked my inbox and saw that the maintainer of the upstream module had replied to say they're happy to accept a PR to increase the timeout.

Raised upstream https://github.com/almottier/TapoP100/pull/15. Will look at reverting this once that's been merged.

Re-opening for the revert.

I've upgraded to the latest upstream commit

pip3 install --force-reinstall git+https://github.com/almottier/TapoP100.git@main

Then, after reverting the commit

git revert 479a1dace56c136f6b961d5e9e5234cfd1606277

Ran the script

$ ./app/collect.py 
Plug: big-fridge using 3.414W, today: 1.192 kWh
Plug: slow-cooker using 0.0W, today: Not Supplied
Wrote 3 points to local

PR 6 raised and merged.

Closing this as done and reverted

verified

mentioned in commit github-mirror/tplink_to_influxdb@479a1dace56c136f6b961d5e9e5234cfd1606277

Commit: github-mirror/tplink_to_influxdb@479a1dace56c136f6b961d5e9e5234cfd1606277 
Author: B Tasker                            
                            
Date: 2023-12-22T12:15:18.000+00:00 

Message

fix: monkeypatch requests to raise overly short timeouts (utilities/tp-link-to-influxdb#9)

This'll raise any timeout below 3 seconds to 3, and set a default timeout of 5 seconds for anything provided without a timeout

+22 -1 (23 lines changed)
verified

mentioned in commit github-mirror/tplink_to_influxdb@2afa795dbcda4126f3a8ba924aaa42baf1ae7266

Commit: github-mirror/tplink_to_influxdb@2afa795dbcda4126f3a8ba924aaa42baf1ae7266 
Author: B Tasker                            
                            
Date: 2023-12-22T17:10:53.000+00:00 

Message

Revert "fix: monkeypatch requests to raise overly short timeouts (utilities/tp-link-to-influxdb#9)"

This reverts commit 479a1dace56c136f6b961d5e9e5234cfd1606277.

Upstream fix has been merged, so this is no longer required

+1 -22 (23 lines changed)