Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get refresh_token automatically based on the username and password #31

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

rusucosmin
Copy link

Currently, after the refresh_token expires, the app automatically generates a new one. However, when you restart Home Assistant, it actually picks up the old one (the one that's stored in your config file). As such, the integration typically fails after some time, at the next Home Assistant restart.

I've changed the config to be able to specify your username and password, and it will get the first refresh_token automatically. Tested with my local HA and everything seems to be working smoothly.

@peternorlander
Copy link

Wow! Fantastic stuff! I checked it out and it works great! It has been running now for 10min. I will let you know in a couple of days if it appears to be fetching new token as excepted but it looks good. Thank you @rusucosmin ! 🥇

@peternorlander
Copy link

I see "my guard notification" has a notification that I can't see in the app. In the app it is all green and if I view firmware version it doesn't say anything about a new version here. I'm using firmware version: 01.51.Z22.0400.0101

This is what I get inside HA:
image

@rusucosmin
Copy link
Author

rusucosmin commented May 28, 2023 via email

@peternorlander
Copy link

peternorlander commented May 28, 2023 via email

@rusucosmin
Copy link
Author

rusucosmin commented May 29, 2023 via email

@peternorlander
Copy link

peternorlander commented May 29, 2023 via email

@peternorlander
Copy link

Hi again Cosmin,

An update from me. None of the values has updated for four days now. So I triggered a manual restart of HA and then it failed to generate a new token for me and thus was unable to setup the integration. Any idea what I have done wrong?

Logs:
2023-06-01 22:01:13.126 ERROR (MainThread) [custom_components.grohe_sense] Grohe sense refresh token is invalid (or expired), please update your configuration with a new refresh token
2023-06-01 22:01:13.687 ERROR (MainThread) [homeassistant.setup] Error during setup of component grohe_sense
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/setup.py", line 256, in _async_setup_component
result = await task
File "/config/custom_components/grohe_sense/init.py", line 76, in async_setup
await initialize_shared_objects(hass, await get_token(hass, config.get(DOMAIN).get(CONF_USERNAME), config.get(DOMAIN).get(CONF_PASSWORD)))
File "/config/custom_components/grohe_sense/init.py", line 89, in initialize_shared_objects
locations = await auth_session.get(BASE_URL + f'locations')
File "/config/custom_components/grohe_sense/init.py", line 144, in get
return await self._http_request(url, auth_token=self, **kwargs)
File "/config/custom_components/grohe_sense/init.py", line 157, in _http_request
token = await auth_token.token()
File "/config/custom_components/grohe_sense/init.py", line 133, in token
refresh_response = await self._http_request(BASE_URL + 'oidc/refresh', 'post', headers=headers, json=data)
File "/config/custom_components/grohe_sense/init.py", line 170, in _http_request
raise OauthException(response.status, await response.text())
custom_components.grohe_sense.OauthException: (401, '{"code":401,"message":"Unauthorized - The token was not validated by the Identity Provider"}')

@peternorlander
Copy link

Hi again,

I restarted my HA again today and then the token was generated successfully again, no errors.

I can also confirm that I only get updates upon restarting HA. Water usage has only been updated those few times that I have restarted.

Regarding the discussion about the notifications from "my guard". It has now been updated to instead say "Maximum volume reached Maximum volume reached" and that was actually the last notification I got, but that warning was triggered the day before. So that notification has been cleared in the app and should no longer be visible.
image

@peternorlander
Copy link

Hi Cosmin,

Finally got some time now during the summer? Nice! I know the feeling! :)

I installed your fixes but now it won't run for me? Can I extend the logs somehow because this is all I get:
image

And all Grohe entities has been removed
image

Any idea?

@rusucosmin
Copy link
Author

HI! Just fixed this. We'll have to wait a few days for the refresh token to expiry before we can see if this works or not

@peternorlander
Copy link

Hi Cosmin,

I pulled your fix and now the integration loads correctly again. But I still see the same behavior as I had last:

  1. The usage value seems to be stale, hasn't updated now for 2 hours even though it is changed in the app.
  2. It shows me an old value for the notification. The "Maximum volume reached" warning I got here two days ago and it has been cleared in the app so it shouldn't be showing here.

image

@peternorlander
Copy link

Hi Cosmin,

I still have the same values, same values as yesterday. But just to be straight with you, I had the same issues with the other main branch as well. So I can just confirm that I don't need to get a token manually anymore, but it doesn't make the integration much more useful for me.

Do you have the same behavior? Or how often does it update for you?

Regards
Peter

@rusucosmin
Copy link
Author

rusucosmin commented Jul 21, 2023 via email

@peternorlander
Copy link

peternorlander commented Aug 23, 2023

Hi Cosmin,

I updated HA from 2023.7 to 2023.8 the evening of the 17:th and after that it suddenly started to work! Now the water values has been updated very well! Check below for history! :) So right now the only thing that seems off is that notification for the main unit. I don't have any notifications that are unread (I used the app to scroll back all notifications until the date of setup and verified) but it still gives me notifications.

But I would like to vote that this PR gets merged with the master branch!

image

image

Regards
Peter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants