SecKatie / ha-wyzeapi

Home Assistant Integration for Wyze devices.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Have to re-configure with every restart

ancjsd opened this issue · comments

I'm being required to re-configure--i.e. provide all authentication information---every time I restart Home Assistant.

Running Home Assistant in VMWare on Windows.
Core 2024.7.1
Supervisor 2024.06.2
Operating System 12.4

Log details:

Logger: custom_components.wyzeapi
Source: custom_components/wyzeapi/init.py:114
integration: Wyze Home Assistant Integration (documentation, issues)
First occurred: 9:17:48 AM (1 occurrences)
Last logged: 9:17:48 AM

Wyzeapi: Could not login. Please re-login through integration configuration

same here. rpi5.

Same.
running on Home Assistant Blue /ODroid-N2+
Core 2024.7.1
Supervisor 2024.06.2
Operating System 12.4
Frontend 20240705.0
and only happening since upgrade core to 2024.7.1

Same here.
HAOS installed in Proxmox(NUC).

Is this new since HA 2024.7.x?

Is this new since HA 2024.7.x?

Not sure if coincidence but I will try to rollback when I get home.

Rollback from core 2024.7.1 to core 2024.7.0
Now all working.

I am also having this issue and unlike others i am farily new to home assistant so im not sure how to downgrade from core 7.1 down to 7.0

Me too same problem

Anyone using the latest update 7.2 if this fix the issue? Changelog doesn't include the wyze but maybe it's working. I am still out of town and I am not going to hit update as I need to restart my hallway tablet controller manually everytime after the update.

So the interesting thing is there is nothing that changed from a Wyze perspective going to 2024.7. Something on the HA side must have changed that’s affecting this but I haven’t been able to dive into it very much yet.

I rolled back to 2024.6.4 and still have this issue. Running in Kubernetes with /config mapped to a PV. In my case, although it may be a different underlying issue, my system keeps losing authentication within an hour or so. Unfortunately, that makes the plugin completely useless for me at this point until the root cause is identified.

@SecKatie do you have a guess what could have changed in HA that would all of a sudden cause a refresh token is error response from Wyze? My main instance is still on 2024.6.0 and is running fine, but on 2024.7dev0 it starts showing up. From what I can tell the correct refresh token is stored and passed so I don’t get it.

I also have this issue on the latest HAOS. Following for updates... if needed, I'll follow-up with debug info.

To further complicate this, I rolled all the way back to HA 2024.4 and Wyze 0.5.21 and I get the same error when its time for a token refresh (we didn't refresh on startup back then so it survives a restart), so something else is going on. But my other setup has been running fine this whole time. I suspect that if I re-configure that one from scratch it will start acting up too.

(I've had HA for 2 days, bear with me)

File "/usr/local/lib/python3.12/site-packages/wyzeapy/utils.py", line 85, in check_for_errors_standard
raise UnknownApiError(response_json)
wyzeapy.exceptions.UnknownApiError: {'code': '2002', 'ts': 1720761289753, 'msg': 'refresh token is error.', 'data': {}, 'traceId': 'f43ef546f27e722b37837217bf0c2340'}

This is from the log error I'm seeing, it says it's occurred 15,000 times in the last 12 hours. Does that mean 'data' is null rather than containing the refresh token?

No the refresh token is sent and this is the response. I’m not sure anything is normally in the data field.

For anybody else having this happen, when you re-login does it start to fail after ~23 hours?

https://support.wyze.com/hc/en-us/articles/16129834216731-Creating-an-API-Key

`HTTP/2 200 OK

Content-Type: application/json

 

{

 "ts":"",

 "code":"",

 "msg":"",

 "data":

 {

 "access_token":"",

 "refresh_token":""

 }

}

Notes:
The access token generated is valid for 2 days only.
The refresh token generated is valid for 30 days.
Please do not repeatedly use the login endpoint (the first endpoint) to generate new access tokens as this endpoint has a rate limit protection. If you need a new access token after using the login endpoint, you can use the refresh token endpoint (the second endpoint) to get this. You'll want to use a valid refresh token generated from the login endpoint for this.`

Oh right, that would have the new access and refresh token normally. But it’s not sent because of the 2002 error.

Mine starts failing an hour and 45 minutes after reauthenticating.

commented

Mine is the same, the errors show a message that the refresh key is error. I tried pulling the wyze-sdk python package an auth and then refresh with the same error. Also just used python requests to call the endpoints according to their specs with the same issue. I thought maybe it was my account as I wasnt seeing others, yet, reporting the issue. I created a new account today after getting frustrated with it and it has the same issue. This seems to be an issue with the refresh tokens on Wyze's end and I think the JWTs are being revoked on the wyze side in short order as well. I tried to put a ticket in with wyze support, but I wasnt expecting much from that end.

Note to all:

Rollback from core 2024.7.1 to core 2024.7.0 Now all working.

4 days now using core 2024.7.0, Wyze still no issue and it's working same as before.

commented

I rolled back to 2024.7.0 and still have the issue. I think there may be a couple of things going on here, or maybe just coincidental that it started working for some after rolling back.

When i decode the access token, the expiration is about 1.6 hours after refresh for the ones I get issued, not 48 hours. Also the refresh tokens are only valid for 28 days not 30. Mine will generally work around that long in HA now. The issue for me, and I suspect others too, is that my refresh tokens appear to be broken, or their API does not like mine for some reason. I created a brand new account and have the same issue as well, so I suspect it may be more wide spread.

All that to say, this appears like it could be an issue with Wyze, not this integration specifically.

commented

I did some more testing with my account. If I downgrade this integration to 0.1.23 it works over restarts. The only reason I can guess for this behavior based on the code changes is that the wyzeapy version went from 0.5.20 to 0.5.21, however 0.5.20 tag no longer exists int he wyzeapy repo to look at what changed there. It seems like the change there is that it almost always, or always, attempts to do a token refresh on load where 0.5.20 (and probably earlier) only refreshed if the access token expired I'm guessing.

Still the root of this issue seems to be broken refresh tokens on Wyze end. I waited the 1 hour and 40ish minutes for my access token to expire and still had to log back in with 0.1.23

I suspect this is related to the fix put in wyzepy 0.5.22

https://github.com/SecKatie/wyzeapy/releases/tag/0.5.22

Hopefully a version bump to the latest (0.5.23) will resolve.

Nevermind, latest already has 0.5.23.

It’s not related to what was changed in 0.5.22 directly, but because we started refreshing on startup vs waiting for a token expired error it makes the integration fail on a HA reboot.

I think what @bmatticus said is the issue. The access token is expiring too soon and the refresh token is invalid. This unfortunately is a Wyze issue, not an issue we can fix here. I have a support ticket open with Wyze but they don’t seem to have any idea what I’m talking about most of the time.

Same problem here. I tried everything also. Resetting passwords. Down grading. It only lasts an hour or so then starts complaining about the refresh token. How about a temporary work around where you handle that error by having it delete the refresh token and restart?

commented

So the quick workaround would be to store the api key and key id in the config, not currently there. I forked and created a branch that does that, an update to the token service. I also added a second attempt if the first attempt fails ommitting the token, which forces a new login. This would still allow the refresh tokens to work eventually, may be an update to wyzeapy to retry without the token if refresh fails would be more elegant though. This works for me locally at least. I could do a PR if the maintainers want to pull in the update.

https://github.com/bmatticus/ha-wyzeapi/tree/issue-612

If anyone else wants they can manually update their init.py and token_manager.py on their instance of HA as a workaround.

commented

I'd be willing to dig in on the wyzeapy side as well, looks like error code 2002 for refresh token issue. Could catch that one to trigger the user auth in the login method potentially. Move that else to a bool for attempt user auth maybe, flip it false when token is there and if error code is 2002 flip it back true to attempt.

commented

Suspected that the expiration of the access token may be an issue after my initial update, so really wyzeapy has to be updated for this workaround to really work. It will work across restarts and you could just reload every 2 hours if you wanted. I made some quick hacks to the wyzeapy to pass back an access token exception that already existed to the login method. If this is caught it will attempt to do another user login. Seems to work locally:

https://github.com/bmatticus/wyzeapy/tree/issue-612

Mine seems to not be having issues running on Core 2024.7.0. I did have to downgrade the core and only the core but once i did, the authentication expiring issue has stopped

commented

Wonder if there needs to be a new issue opened for refresh tokens specifically? I'm not sure what the rollback is fixing for the people it is fixing (rolling back HA Core only). I have no issues with the refresh token hacks I made locally and 2024.7.2 and downgrading never made a difference for me. I'm assuming that the people rolling back can actually control devices, like light bulbs, a couple hours after they reload? I know mine would act fine and let me toggle things when i downgraded the integration, but it wasnt actually controlling things and there were error like these in the logs.

2024-07-14 18:15:38.306 ERROR (MainThread) [wyzeapy.services.update_manager] Unknown error happened during updating device info
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/wyzeapy/services/update_manager.py", line 42, in update
    self.device = await self.service.update(self.device)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/wyzeapy/services/camera_service.py", line 41, in update
    camera.device_params = await self.get_updated_params(camera.mac)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/wyzeapy/services/base_service.py", line 131, in get_updated_params
    await self.get_object_list()
  File "/usr/local/lib/python3.12/site-packages/wyzeapy/services/base_service.py", line 105, in get_object_list
    await self._auth_lib.refresh_if_should()
  File "/usr/local/lib/python3.12/site-packages/wyzeapy/wyze_auth_lib.py", line 214, in refresh_if_should
    await self.refresh()
  File "/usr/local/lib/python3.12/site-packages/wyzeapy/wyze_auth_lib.py", line 237, in refresh
    check_for_errors_standard(self, response_json)
  File "/usr/local/lib/python3.12/site-packages/wyzeapy/utils.py", line 85, in check_for_errors_standard
    elif response_json['code'] == ResponseCodes.DEVICE_OFFLINE.value:
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wyzeapy.exceptions.UnknownApiError: {'code': '2002', 'ts': 1720995338267, 'msg': 'refresh token is error.', 'data': {}, 'traceId': 'f832b22a17da8b73663536a0d8c6ff5d'}

Looks like i couldnt use your code directly in homebridge because of how they integrated it. This is what i did jfarmer08/homebridge-wyze-smart-home#258 (comment)

Been working fine with it refreshing the refresh token every hour. My code didn't activate at all except once when installed a plug in. When HomeKit rebooted for a plugin install Wyze said the token was bad when it came back up. Must be something very messed up on their end. I keep getting emails about the V3 app being released maybe they are breaking things.

[15/07/2024, 00:23:15] Homebridge v1.8.3 (HAP v0.12.2) (Homebridge E07F) is running on port 51184.
[15/07/2024, 00:24:23] [Homebridge UI] [admin] Accessory layout changes saved.
[15/07/2024, 00:28:21] [WYZE] Wyze API Error (2001): 'access token is error'
[15/07/2024, 00:28:21] [WYZE] Wyze API Error (2002): 'refresh token is error.'
[15/07/2024, 00:28:21] [WYZE] Error getting devices: Error: Oh no 2002 failure refresh token is bad deleting persistent token
[15/07/2024, 00:36:36] [Homebridge UI] Running Command: /opt/homebridge/lib/node_modules/homebridge-config-ui-x/plugin-upgrade-install.sh homebridge-ups 1.2.11 /var/lib/homebridge/node_modules
[15/07/2024, 00:38:37] [Homebridge UI] Changes to config.json saved.
[15/07/2024, 00:38:37] [Homebridge UI] Running Command: npm uninstall --save homebridge-ups
[15/07/2024, 00:38:58] [Homebridge UI] Running Command: npm install --save homebridge-linux-cyberpower-ups@1.0.8
[15/07/2024, 00:39:54] [Homebridge UI] Changes to config.json saved.
[15/07/2024, 00:39:54] [Homebridge UI] Running Command: npm uninstall --save homebridge-linux-cyberpower-ups
[15/07/2024, 00:40:00] [Homebridge UI] Homebridge restart request received
[15/07/2024, 00:40:00] [Homebridge UI] UI / Bridge settings have not changed; only restarting Homebridge process
[15/07/2024, 00:40:00] [Homebridge UI] Sending SIGTERM to Homebridge
[15/07/2024, 00:40:00] Got SIGTERM, shutting down Homebridge...
[15/07/2024, 00:40:05] [HB Supervisor] Homebridge Process Ended. Code: 143, Signal: null
[15/07/2024, 00:40:10] [HB Supervisor] Restarting Homebridge...
[15/07/2024, 00:40:10] [HB Supervisor] Starting Homebridge with extra flags: -I -P /var/lib/homebridge/node_modules --strict-plugin-resolution
[15/07/2024, 00:40:10] [HB Supervisor] Started Homebridge v1.8.3 with PID: 31362
[15/07/2024, 00:40:12] Loaded config.json with 0 accessories and 3 platforms.
[15/07/2024, 00:40:12] Loaded 17 cached accessories from cachedAccessories.
[15/07/2024, 00:40:12] ---
[15/07/2024, 00:40:13] Loaded plugin: homebridge-dyson-pure-cool@2.7.10
[15/07/2024, 00:40:13] Registering platform 'homebridge-dyson-pure-cool.DysonPureCoolPlatform'
[15/07/2024, 00:40:13] ---
[15/07/2024, 00:40:13] Loaded plugin: homebridge-wyze-smart-home@0.5.46
[15/07/2024, 00:40:13] Registering platform 'homebridge-wyze-smart-home.WyzeSmartHome'
[15/07/2024, 00:40:13] ---
[15/07/2024, 00:40:13] Loading 3 platforms...

Update: Still randomly had my refresh token denied last night even with it refreshing every hour. At least now that its automatically deleting the token after I receive error 2002 it still worked great this morning. If I didn't do this, it would just be in a loop this morning until i delete the file out of the persistent folder.

[15/07/2024, 01:15:58] [MikesDesktop] [NetworkDevice] Awake cycle started
[15/07/2024, 01:15:58] [MikesDesktop] [NetworkDevice] Attempting to wake up device
[15/07/2024, 01:15:58] [MikesDesktop] [NetworkDevice] Device went from status "Offline" to "WakingUp"
[15/07/2024, 01:15:59] [MikesDesktop] [NetworkDevice] Waiting for wake grace time (45000ms)
[15/07/2024, 01:16:44] [MikesDesktop] [NetworkDevice] Device went from status "WakingUp" to "Online"
[15/07/2024, 01:36:36] [Homebridge UI] Running scheduled instance backup...
[15/07/2024, 01:36:36] [Homebridge UI] Creating temporary backup archive at /tmp/homebridge-backup-1zpADV/homebridge-backup-0EABF378DBAC.tar.gz
[15/07/2024, 02:10:15] [WYZE] Wyze API Error (2001): 'access token is error'
[15/07/2024, 02:10:15] [WYZE] Wyze API Error (2002): 'refresh token is error.'
[15/07/2024, 02:10:15] [WYZE] Error getting devices: Error: Oh no 2002 failure refresh token is bad deleting persistent token
[15/07/2024, 02:21:36] [Homebridge UI] Creating temporary backup archive at /tmp/homebridge-backup-BuyRLV/homebridge-backup-0EABF378DBAC.tar.gz
[15/07/2024, 02:21:36] [Homebridge UI] Backup complete, removing /tmp/homebridge-backup-BuyRLV
[15/07/2024, 03:51:56] [WYZE] Wyze API Error (2001): 'access token is error'
[15/07/2024, 03:51:56] [WYZE] Wyze API Error (2002): 'refresh token is error.'
[15/07/2024, 03:51:56] [WYZE] Error getting devices: Error: Oh no 2002 failure refresh token is bad deleting persistent token
[15/07/2024, 05:33:41] [WYZE] Wyze API Error (2001): 'access token is error'
[15/07/2024, 05:33:41] [WYZE] Wyze API Error (2002): 'refresh token is error.'
[15/07/2024, 05:33:41] [WYZE] Error getting devices: Error: Oh no 2002 failure refresh token is bad deleting persistent token

Same for me... required to "reauthenticate" and enter all information on each HA restart.

I got this response from Wyze IMG_5664

Note to all:

Rollback from core 2024.7.1 to core 2024.7.0 Now all working.

4 days now using core 2024.7.0, Wyze still no issue and it's working same as before.

6 days still working. Never hit the reload.

I assume when you say "rollback" you mean restore from a backup right? If that's the case then that makes sense. It really has nothing to do with the HA version, its because when you restore from a backup, you restore a working access and refresh token. And for whatever reason, it only seems to be the new tokens created when you login with username/pass/key that are generating bad refresh tokens.

I basically just tried this so I think it should work for anybody that wants to try:

  1. Restore to a HA version where Wyze was working.
  2. Go to config/.storage/core.config_entries file and search for Wyze config entry
  3. Copy both access token and refresh token exactly as they are and save them somewhere for later
  4. Go ahead and upgrade HA to latest version or whatever you want to use. When it finishes upgrading and is fully started, you will probably get the same error as you have been.
  5. Go back to the same file above and replace the access and refresh tokens with the ones you saved from the working instance.
  6. Hold your breath
  7. Restart HA
commented

Interesting enough, i went into my incremental backups from 6/30 and the refresh token does work. Also the access token from that time was for 2 days. When I refresh using that refresh token i get a new working refresh token and the new access token is good for 2 days.

The only other difference I see in the new jwts is that native_oauth2_resource is in aud as well as oauth2-resource.

same here! such a shame Wyze is a really good brand!

Note to all:

Rollback from core 2024.7.1 to core 2024.7.0 Now all working.

4 days now using core 2024.7.0, Wyze still no issue and it's working same as before.

6 days still working. Never hit the reload.

More than a week now and mine still working. Skipping all the updates except the latest version of core 2024.7.3 and still working. No modifications or whatsoever, just rollback to the last working core 2024.7.0

just rollback to the last working core 2024.7.0

one of the privileged ones :P

I only just got HA and has nothing to rollback to.

commented

When is coming out an update for this bug?m

As we said in the comments several times, this is a Wyze issue, not something we can easily remedy. They are aware of it. The instructions to restore a working token above do work if you have a backup to restore from.

commented

As we said in the comments several times, this is a Wyze issue, not something we can easily remedy. They are aware of it. The instructions to restore a working token above do work if you have a backup to restore from.

Can you send instructions please how to use a token from a old backup?

#612 (comment)

I will add one caveat to that, the backup you restore from probably has to be from within the last 30 days because that’s how long the refresh token is good for. I’m not positive on that but I think that’s what I saw on the decoded token.

commented

#612 (comment)

I will add one caveat to that, the backup you restore from probably has to be from within the last 30 days because that’s how long the refresh token is good for. I’m not positive on that but I think that’s what I saw on the decoded token.

Why there is no other way to integrate with Wyze?
Maybe you can integrate the same way as the tinyCam PRO app (when you try to use a Wyze camera it redirect you to Wyze login page and you have to login to your Wyze account and than it's working)

commented
Screen_Recording_20240722_225843_tinyCam.PRO.mp4

#612 (comment)
I will add one caveat to that, the backup you restore from probably has to be from within the last 30 days because that’s how long the refresh token is good for. I’m not positive on that but I think that’s what I saw on the decoded token.

Why there is no other way to integrate with Wyze? Maybe you can integrate the same way as the tinyCam PRO app (when you try to use a Wyze camera it redirect you to Wyze login page and you have to login to your Wyze account and than it's working)

Because this is the way Wyze has asked 3rd party integrations to connect. We used to use a simple username/password but Wyze now requires the API key.

https://support.wyze.com/hc/en-us/articles/16129834216731-Creating-an-API-Key

commented

Because this is the way Wyze has asked 3rd party integrations to connect. We used to use a simple username/password but Wyze now requires the API key.

https://support.wyze.com/hc/en-us/articles/16129834216731-Creating-an-API-Key

So the only solution is waiting for the fix from Wyze....😔

commented

Somehow I set CPU passthrough in my VM setting, then Wyze integration works fine now. Not sure why.

On Mon, Jul 22, 2024 at 8:07 PM Shwerzb @.> wrote: Because this is the way Wyze has asked 3rd party integrations to connect. We used to use a simple username/password but Wyze now requires the API key. https://support.wyze.com/hc/en-us/articles/16129834216731-Creating-an-API-Key So the only solution is waiting for the fix from Wyze....🙂‍↕️ — Reply to this email directly, view it on GitHub <#612 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGKKCCX4AFC4FEBCCLOGD6LZNXCGFAVCNFSM6AAAAABKTKGSUWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBUGE3DGMBVHE . You are receiving this because you are subscribed to this thread.Message ID: @.>

Even after you restart home assistant?

I am still needing to reauthenticate on restart, but I generated a new API key about 12 hours ago and it is still working as of now. Previously it was breaking at 1.6 hours.

commented

I just generated a new API and it's working!!

Can confirm that the tokens appear to have been fixed! I'll let it run for a while but the access token is back to expiring after 2 days like it used to be, and the refresh token issued now seems to be working properly again. 🤞