mihai-dinculescu / tapo

Unofficial Tapo API Client. Works with TP-Link Tapo smart devices. Tested with light bulbs (L510, L520, L530, L610, L630), light strips (L900, L920, L930), plugs (P100, P105, P110, P115, P300), hubs (H100), switches (S200B) and sensors (KE100, T100, T110, T300, T310, T315).
MIT License
356 stars 36 forks source link

tapo-py 0.3.1 code hangs on timeout #245

Closed mihai-dinculescu closed 2 weeks ago

mihai-dinculescu commented 2 months ago

Two users (https://github.com/mihai-dinculescu/tapo/commit/c91ee35c6ca01c282bfafd0cb99fd5384093bb6a#commitcomment-144217052 & https://github.com/mihai-dinculescu/tapo/issues/228#issuecomment-2209587753) have reported that after the migration to pyo3 0.21, the code hangs when timeouts occur.

I have been able to reproduce the problem, and it seems that the hangs are caused by the isahc's send_async future that never completes. Using the non-async send function works as intended.

However, I'd like to explore migrating away from isahc, as it no longer seems to be well maintained.

TTSKarlsson commented 1 month ago

Is there a workaround for now, like choosing a version that works? If so how do I use it?

avadiax commented 3 weeks ago

Hi, I noticed you have released 0.3.2. The timeout issue still continue to persist and I have continued to use 0.3.0. FYI.

mihai-dinculescu commented 3 weeks ago

@TTSKarlsson v0.3.0 is the latest good version that doesn't exhibit this issue. @avadiax, the fix for this proved more difficult than I thought, and I'm still exploring what's the best way to address the issue. Please bear with me 🙏

mihai-dinculescu commented 2 weeks ago

The fix is on the main branch. Could you please give it a try @TTSKarlsson, @avadiax, @gsaviane?

avadiax commented 2 weeks ago

The fix is on the main branch. Could you please give it a try @TTSKarlsson, @avadiax, @gsaviane?

Hi Mihai, unfortunately I install via pip, and pip will not install from the main branch since it does not detect setup.py or pyproject.toml in it. :( Not sure if I miss anything.

TTSKarlsson commented 2 weeks ago

I'm not sure how to run this.

My steps:

  1. clone repo
  2. cd tapo-py
  3. pip install poetry
  4. poetry install
  5. poetry shell -> now inside the venv
  6. export TAPO_USERNAME=_________
  7. export TAPO_PASSWORD=_________
  8. export IP_ADDRESS=the local ip for a tapo plug
  9. py ./examples/tapo_p100.py

Result:

Traceback (most recent call last):
  File "F:\rust\tapo\tapo-py\examples\tapo_p110.py", line 7, in <module>
    from tapo import ApiClient
ModuleNotFoundError: No module named 'tapo'
mihai-dinculescu commented 2 weeks ago

@TTSKarlsson After step 5, you need one more, maturin develop. @avadiax these steps should work for you as well.

CONTRIBUTING.md should be updated to better capture this.

TTSKarlsson commented 2 weeks ago

20240831_124627_1161x254

It does turn it on and then off, but then fails to catch an exception.

TTSKarlsson commented 2 weeks ago

This exception doesn't happen on tapo_generic_device_toggle example btw

mihai-dinculescu commented 2 weeks ago

That's a separate issue concerning the changes introduced in 0.3.2 in response to firmware updates. Is your P11X device on the latest firmware?

TTSKarlsson commented 2 weeks ago

fw_ver is '1.2.5 Build 240411 Rel.143808' , if that's the firmware.

I've got a few different plugs. I'm testing on a P100, and this response is the same using examples tapo_p100 and tapo_p110.

mihai-dinculescu commented 2 weeks ago

Is your P100 on the latest firmware? If so, can you please paste the response of get_device_info_json?

TTSKarlsson commented 2 weeks ago
{
  "avatar": "ceiling_lamp",
  "device_id": "8022C03D80DD64FC390D5CA114CA6C31209FE8B8",
  "device_on": false,
  "fw_id": "00000000000000000000000000000000",
  "fw_ver": "1.2.5 Build 240411 Rel.143808",
  "has_set_location_info": true,
  "hw_id": "4D44B233807D5BDA45BEC69DC18AAE09",
  "hw_ver": "2.0",
  "ip": "192.168.2.206",
  "lang": "en_US",
  "latitude": 0,
  "longitude": 0,
  "mac": "30-DE-4B-21-D6-4D",
  "model": "P100",
  "nickname": "Computer",
  "oem_id": "6A0E161B05B8FB93E53988B66CE51E30",
  "on_time": 0,
  "region": "Europe/Amsterdam",
  "rssi": -51,
  "signal_level": 2,
  "specs": "",
  "ssid": "Emsie",
  "time_diff": 60,
  "type": "SMART.TAPOPLUG"
}
TTSKarlsson commented 2 weeks ago

I don't know how to know if the firmware is the latest, it seems from the build name that it's from april this year

TTSKarlsson commented 2 weeks ago

I should add that I've had these for years and I don't use any apps or anything, which means they do manage to update themselves whenever they want to.

mihai-dinculescu commented 2 weeks ago

The fix for P100 is on main. Can you please try again?

TTSKarlsson commented 2 weeks ago

👍🏼 Yes now it works, and the test also returns the device info and device usage

mihai-dinculescu commented 2 weeks ago

What about the main issue described in this ticket? Did the hangs that you've experienced with v0.3.1 and v0.3.2 go away?

TTSKarlsson commented 2 weeks ago

Yes, this issue is gone in the main branch with P100 that I tested.

If it passes for you, you should probably release it as it's going to be a better experience than having to install an older version. My main use now is still 0.3.0 until this releases.

I'll open a new ticket if the new release wouldn't work for some reason.

mihai-dinculescu commented 2 weeks ago

The change is out now as part of 0.4.0. I just needed a little bit of time to get another improvement in.

avadiax commented 2 weeks ago

The change is out now as part of 0.4.0. I just needed a little bit of time to get another improvement in.

I can confirm 0.4.0 looks to be stable during the testing. Will report if issues pop up.

I take it that you replaced isahc with reqwest?

TTSKarlsson commented 2 weeks ago

0.4.0 works here too.

mihai-dinculescu commented 2 weeks ago

Great, thanks everyone for trying it out, and apologies for the delay in fixing it.

Indeed, isahc was part of the issue. From what I gather, it has its own internal async runtime that doesn’t seem to play well with pyo3. Replacing it with another library that supports an external runtime allowed me to configure tokio in a way that works.