SecKatie / ha-wyzeapi

Home Assistant Integration for Wyze devices.
733 stars 112 forks source link

AccessTokenError occurs after running for a couple of days #40

Closed hjdarnel closed 4 years ago

hjdarnel commented 4 years ago

Describe the bug After a few days of uptime, the plugin starts to throw an error and my switches don't respond to input. Here's the log:

Logger: custom_components.wyzeapi.wyzeapi.wyzeapi_request_manager
Source: custom_components/wyzeapi/wyzeapi/wyzeapi_request_manager.py:42
First occurred: March 29, 2020, 12:00:00 PM (13595 occurrences)
Last logged: March 29, 2020, 3:45:38 PM

AccessTokenError

To Reproduce Steps to reproduce the behavior: Configure configuration.yml with username and password, restart HA and wait a few days (?)

Expected behavior the wyzeapi plugin should continue to work, as it does right after a restart.

System configuration System: HassIO on a Pi 3b HA Version: Home Assistant 0.107.5 WyzeApi Version: v0.4.2

configuration.yaml

wyzeapi:
  username: !secret wyze_username
  password: !secret wyze_password

with secrets

wyze_username: myemail@gmail.com
wyze_password: mycorrectpassword

home-assistant.log I've added this debug option, I'll update with complete logs once I recreate the error with debug logs turned on

hjdarnel commented 4 years ago

This is still occurring regularly (every 2 days without a restart). Without any better logging there's no way to tell what's going on. All I get is the AccessTokenError in the Supervisor Logs tab

SecKatie commented 4 years ago

Hey @hjdarnel,

I will work on getting additional logging around the affected area of code. Hopefully we can figure out what is going on.

I should have more time coming up soon due to the end of my school semester and I will be able to focus back on this project!

Thanks for submitting an issue! I hope that I will have a new beta for you to try out in the next week or two.

hjdarnel commented 4 years ago

Thanks @JoshuaMulliken! I totally understand. From my limited knowledge of oauth it’s not refreshing the token properly. 🤷‍♂️

Anyway ping here when you get some logs put in and I can pull it and start investigating!

ArndBrugman commented 4 years ago

Although not experincing this as I restart my HA twice a day I have found some code to refresh a token that might work when token looses access: https://github.com/noelportugal/wyze-node/blob/master/index.js

I hope this still works and could be implemented too. Sadly I am no Python dev so cannot assist with this.

hjdarnel commented 4 years ago

Yeah for now I might just restart every night or something to fix this.

On Sat, Apr 25, 2020 at 6:26 PM Arnd Brugman notifications@github.com wrote:

Although not experincing this as I restart my HA twice a day I have found some code to refresh a token that might work when token looses access: https://github.com/noelportugal/wyze-node/blob/master/index.js

I hope this still works and could be implemented too. Sadly I am no Python dev so cannot assist with this.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/JoshuaMulliken/ha-wyzeapi/issues/40#issuecomment-619454201, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4BDY55SZD62T7IQQWS2ITRONWTDANCNFSM4LWXLENA .

ArndBrugman commented 4 years ago

@JoshuaMulliken I've just tried this in PostMan and it returns a new access_token and refresh_token so it still works on the API of Wyze. Let me know if I can help implement it, although I'm not Python savvy, I might be able to point out where to add it.

SecKatie commented 4 years ago

Awesome thank you for the research. Right now the code try’s to login again in order to get a new access token. It seems to work with the lights but obviously switches are an issue. I will hopefully be able to get a beta with more debug logging soon. Exams are next week so once they are done I will have more free time to work on this.

SecKatie commented 4 years ago

Interestingly it doesn't have this issue with the lights. I think there is something funny going on with the switches.

hjdarnel commented 4 years ago

I will say I only use the API for switches 😃

SecKatie commented 4 years ago

@hjdarnel can you download the latest from master. I have more logging in there. I plan on implementing refresh token support later but at least this will give us more insight

hjdarnel commented 4 years ago

I've installed it, we'll need to give it some time for the access token to expire before it logs much 👍

hjdarnel commented 4 years ago

Getting an error off master @JoshuaMulliken

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/setup.py", line 137, in _async_setup_component
    component = integration.get_component()
  File "/usr/src/homeassistant/homeassistant/loader.py", line 268, in get_component
    cache[self.domain] = importlib.import_module(self.pkg_path)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/config/custom_components/wyzeapi/__init__.py", line 7, in <module>
    from .wyzeapi.wyzeapi import WyzeApi
  File "/config/custom_components/wyzeapi/wyzeapi/wyzeapi.py", line 37
    url = "https://api.wyzecam.com/app/user/login"
                                                 ^
IndentationError: unindent does not match any outer indentation level
SecKatie commented 4 years ago

Yep just fixed that one. Sorry about that. Can you try again?

psicard commented 4 years ago

I'm not sure if this is related, but I just installed through HACS (not sure which branch that is) and got this: 2020-05-01 20:55:03 ERROR (MainThread) [homeassistant.setup] Error during setup of component wyzeapi Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/setup.py", line 171, in _async_setup_component hass, processed_config File "/config/custom_components/wyzeapi/init.py", line 39, in async_setup await wyzeapi_account.async_init() File "/config/custom_components/wyzeapi/wyzeapi/wyzeapi.py", line 23, in async_init self._access_token = await self.async_login(self._user_name, self._password, self._device_id) File "/config/custom_components/wyzeapi/wyzeapi/wyzeapi.py", line 47, in async_login data = await self.async_do_request(url, payload) File "/config/custom_components/wyzeapi/wyzeapi/wyzeapi.py", line 113, in async_do_request return await WyzeRequest(url, payload).async_get_response() File "/config/custom_components/wyzeapi/wyzeapi/wyze_request.py", line 45, in async_get_response raise WyzeApiError("Payload: " + str(self._payload) + "\nResponse Data: " + str(data))

SecKatie commented 4 years ago

@psicard It seems like it cannot login. It is failing on startup so that would be my thought. Check the credentials. Remember 2fa doesn’t work with this.

SecKatie commented 4 years ago

@hjdarnel any new logs?

hjdarnel commented 4 years ago

@JoshuaMulliken I actually haven't experienced the original issue since changing to pull from master instead of a numbered release. I wonder if the numbered releases were/are behind or something. I'll pull logs if it comes up though.

SecKatie commented 4 years ago

@hjdarnel That’s great! I am going to be switching to use the refresh token soon and it would be great to have a beta tester for that. Would you be able to do that when it comes along?

hjdarnel commented 4 years ago

Sure, let me know when you have a working branch up and I can have HACS pull it

psicard commented 4 years ago

Oops. Yeah, I have 2FA turned on...

On Mon, May 4, 2020 at 11:43 AM Joshua Mulliken notifications@github.com wrote:

@psicard https://github.com/psicard It seems like it cannot login. It is failing on startup so that would be my thought. Check the credentials. Remember 2fa doesn’t work with this.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/JoshuaMulliken/ha-wyzeapi/issues/40#issuecomment-623575580, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOQ2R4DENXQMZKDEBNWMT3RP3WE7ANCNFSM4LWXLENA .

SecKatie commented 4 years ago

@ArndBrugman Can you look at this pull request for me. Should this work to use the refresh token? https://github.com/JoshuaMulliken/ha-wyzeapi/pull/44

ArndBrugman commented 4 years ago

As mentioned I am no Python guy. It looks fine though. easiest way to validate is to rais an exeption right after:

try: return await WyzeRequest(url, payload).async_get_response() ---> HERE!!! (WELL Before the return of course ;) )

This will force to refresh the access token and retry for a second time. Note this is for validation/debugging only, not the actual release.

Also I see the function

def is_valid_login(self):
    if self._access_token == None:
        return False
    return True

which would mean if we loose access token and are unable to refresh it we should invalidate access_token. OR: even better do a re-login that will try to login and if it fails it should wipe the self_access_token.

Hope this helps and is clear enough.

By the way: the devices returned also return the sensors, which is a great addition.

And: I have some 15 bulbs and a few sensor kits. Which makes updates to HA slow, slower that 30 seconds.

Updating wyzeapi light took longer than the scheduled update interval 0:00:30 2:56:23 PM – Light (WARNING) - message first occurred at 5:23:40 AM and shows up 544 times

Updating wyzeapi switch took longer than the scheduled update interval 0:00:30 2:56:23 PM – Switch (WARNING) - message first occurred at 5:23:40 AM and shows up 510 times

It would be good to do batch updates on all devices and use a cache for the reads in between. On my Hue Web App I only update complete status once every 2.5 seconds. and just display from cached values to make sure i do not overload the bridge. we could persue a similar approach and setup in this component.

SecKatie commented 4 years ago

Ok @hjdarnel the latest master should be using the refresh token so if you pull an update from master you can help test it