Octoober / tuya-bulb-control

API wrapper for you smart bulbs developed by Tuya 💡
MIT License
31 stars 3 forks source link

Revalidate token #2

Closed prankard closed 3 years ago

prankard commented 3 years ago

Hey, thanks for working on this. It's great library.

I've been using it for a system tray app to control the light in my office. But I noticed that after about an hour of inactivity the token becomes invalid and all bulb control functions no longer work. You can test it with this code:

bulb = Bulb(
    client_id=CLIENT_ID,
    secret_key=SECRET_KEY,
    device_id=DEVICE_ID,
    region_key=REGION_KEY
)

bulb.turn_on()

time.sleep(2)
bulb._TuyaApi__access_token = "invalid"

# Error, will fail to get valid data on _get()
bulb.turn_off() 

I've added a workaround in _get that checks the token before returning, and if it returned a bad request, will acquire a new token and try again. The same with _post (although I think that one is not needed, it may be needed in the future). It will only try to get the token once, not indefinitely.

Thanks again!

Octoober commented 3 years ago

Thanks for your edits!