gkreitz / homeassistant-grohe_sense

Grohe Sense integration for Home Assistant
MIT License
42 stars 25 forks source link

Refresh token expires often #23

Open peternorlander opened 2 years ago

peternorlander commented 2 years ago

Hi!

I really like this integration but I wanted to ask. I don't know if it is just me but how often do you guys need to create a new token? Mine usually only last for maybe two weeks then I need to get a new one.

Regards Peter

Floppe commented 2 years ago

Hi. Have no idea how often, but it's so often that it's annoying. I've stopped using this integration until proper OAuth is implemented.

I've also noticed the same thing with Grohes own Android App. Have to sign in again after some weeks. So could be the problems are coming from their own services.

// Joachim

peternorlander commented 2 years ago

I spent a little time trying to understand how Grohe's OAuth is working using postman but can't really get it to work. Even when I copied the urls chrome is using with the same payloads I get errors that I'm using an outdated browser. Anyone has any idea how to make OAuth working?

seranoo commented 2 years ago

Hi!

I have the same issues, like every two weeks i need to generate a new token. And more or less the same issue with the Apple app, often need to delete/reinstall the app to get it working again.

It is such a pity, the combination with Grohe Sense and my home alarm is essentially why I started the whole HA "trip". I will have to find some other hardware that can accomplish a similar functionality.

Regards, Magnus

erikkt commented 2 years ago

I've had to modify mine. I have installed an Wemos ESP relay board inside the Grohe Sense Guard. It was an easy hack. Now I have at least a reliable way of shutting the water off.

seranoo commented 2 years ago

Do you run the Wemos ESP relay board in parallell with the Grohe Sense Guard functionality? I.e. can the Grohe Sense Guard automatically shut off the water in case of an unexpected high flow, etc?

And of course, do you have any photos of the hack? Would love to have some pointers.

erikkt commented 2 years ago

Yes, the Grohe functionality is unaffected (when it actually works...) There are solder points where the power cable attaches and where the solenoid valve attaches. If you with a wemos relay short circuit the negative from power to the negative on the solenoid, the solenoid will activate. You have 12v power already present there, so you need a wemos mini with relay card, and a small 12v->5v converter. Then you only need three wires between the wemos setup and the Sense guard, 12v + and -, and negative from relay to solenoid. Using ESPHome the code is super simple, and very reliable. And best of all, it is cloud independent.

seranoo commented 2 years ago

Wow!!! Thanks erikkt! Then that is exactly what I will do!

In my opinion the "local Grohe functionality" appears working well. I've got kids that once in a while forgets to completely turn off the tap, and the the Grohe Sense Guard always turns off the water. At least in my experience. I think is is just the cloud connection that really sucks.

Again, thanks for this!

bendikrb commented 2 years ago

I threw together some code to retrieve the token. Hopefully this could help integrating authentication in the HA integration!

""" """
import html
import json
import re
import sys
from urllib.parse import urlparse

import requests

BASE_URL = 'https://idp2-apigw.cloud.grohe.com/v3/iot/'
LOGIN_URL = f"{BASE_URL}oidc/login"

def main():
    args = [arg for arg in sys.argv[1:]]
    if len(args) < 2:
        raise ValueError("Need exactly 2 arguments: username, password")
    login(args[0], args[1])

def login(username, password):
    oauth_session = requests.Session()

    redirect = oauth_session.get(LOGIN_URL, allow_redirects=False)
    redirect.raise_for_status()

    login_url = redirect.headers['Location']
    login_form = oauth_session.get(login_url)
    login_form.raise_for_status()

    regex = r'action="([^ >"]*)"'
    matches = re.findall(regex, login_form.content.decode('utf-8'), re.MULTILINE)
    login_url = html.unescape(matches[0])

    response = oauth_session.post(
        login_url,
        allow_redirects=False,
        data={
            "username": username,
            "password": password,
        })
    response.raise_for_status()

    token_url = urlparse(response.next.url)._replace(scheme="https").geturl()
    token_result = oauth_session.get(token_url)
    token_result.raise_for_status()
    token = token_result.json()
    print("== Successfully retrieved access token ==")
    print(json.dumps(token, indent=4))

if __name__ == '__main__':
    main()
aperezva commented 2 years ago

Thanks How can execute it?. Would I to create file?.

BR and good job

aperezva commented 2 years ago

Another question, do you know why data from HA differs taht data APP shows?

Thanks

bendikrb commented 2 years ago

Thanks How can execute it?. Would I to create file?.

Yes, and then you can run it in cli, like; python3 filename.py exampleuser mypasswd

aperezva commented 2 years ago

Thanks to difficult to me, I don´t have pythonknowledge and show me a error

Last login: Mon Jul 18 15:38:54 on ttys000 cd '/Users/xxxxxxx/Downloads/' && '/usr/local/bin/python3' '/Users/xxxx/Downloads/api grohe.py' && echo Exit status: $? && exit 1 xxxxxxx@iMac ~ % cd '/Users/txxxxxxx/Downloads/' && '/usr/local/bin/python3' '/Users/xxxxxxxx/Downloads/api grohe.py' && echo Exit status: $? && exit 1 Traceback (most recent call last): File "/Users/xxxxxxxx/Downloads/api grohe.py", line 8, in import requests ModuleNotFoundError: No module named 'requests' tonyperezvalls@iMac-de-Tony Downloads %

bendikrb commented 2 years ago

pip3 install requests .. or run this in a container (use one of the SSH addons for example), or even add it as a command line sensor

aperezva commented 2 years ago

Thanks,

Today is the third time I have to generate a new token. It seems I will till another integration will be ready. Impossible to manage it in this way.

Thanks a lot for your time

bendikrb commented 2 years ago

That doesn't sound right - are you sure you're using the refresh token?

aperezva commented 2 years ago

Yes, and it’s working for some time but after 2 hours HA show me an error in the token

Any suggestions?

aperezva commented 2 years ago

Thanks How can execute it?. Would I to create file?.

Yes, and then you can run it in cli, like; python3 filename.py exampleuser mypasswd

Hi again @bendikrb

After renewal token every two days, I need you help to implement this feature and get token in the easier way.

How can I setup the command line sensor?. It means that this sensor will provide me the updated token ?

Please, support me in this way if you have time.

thanks

pertakman commented 2 years ago

I'm struggling with having to restart HA due to complaints about the refresh token. It does not seem to matter whether I get a new refresh token or not, but I'd also like to get help with automating this somehow.

seranoo commented 2 years ago

Hi @erikkt!

Yesterday I had a chance to open up the Grohe Sense Guard and to short circuit the negative from power to the negative on the solenoid (just manually to test it out).

It worked great. However, since you have to keep short circuiting the negative from power to the negative on the solenoid it appears to no longer be possible to use the button on the front of the Grohe Sense Guard or to use the app. Am I correct?

Is this the way you have approached it? Looking forward to hearing back from you!

Kindest regards, Magnus

erikkt commented 2 years ago

@seranoo You are correct in the sense that if the water is turned off by short circuiting manually or by a relay, you cannot turn on water with the Grohe button or with the app. This setup will turn off water and override everything else. But in normal cases, the water is on, and in that case you can turn off water with relay, button and/or app.

seranoo commented 2 years ago

I can highly recommend the proposal by @erikkt until (somwhere in the future...) when Grohe sorts out their app/token. That is, add a separate piece of hardware to provide negative power to the solenoid when you want to turn off the water.

There is plenty of space inside of Grohe Sense, so anything will do really. Just push the two buttons on the sides and open the lid.

erikkt used a wemos mini with relay card, and a small 12v->5v converter. I instead used a Shelly 1 running on 12V. Either solution is super easy and now is actually possible to be sure that the water is turned off when you want it to. No more having to rely on if the token for the day is working.

pertakman commented 1 year ago

After having giving the proposal by @erikkt some consideration I decided to hack the Grohe sense guard as suggested. To avoid the frustration of not being able to turn the water back on in case Home Assistant crashes I decided to add a momentary button and some configuration settings to ensure that power cycling the Grohe sense guard turns the water back on again. I've attached my yaml file for those of you that find that helpful.

grohe-sense-guard.txt

Warmly, Per

Sleepy81 commented 1 year ago

Tried the python script, but get the following:

Traceback (most recent call last): File "C:\Users\XXXXX\grohe.py", line 53, in main() File "C:\Users\XXXXX\grohe.py", line 18, in main login(args[0], args[1]) File "C:\Users\XXXXX\grohe.py", line 44, in login token_url = urlparse(response.next.url)._replace(scheme="https").geturl() AttributeError: 'NoneType' object has no attribute 'url'

peternorlander commented 1 year ago

It just expired again, less than 48 hours after I created it last time.

On Wed, 13 Apr 2022, 12:32 Joachim Wickman, @.***> wrote:

Hi. Have no idea how often, but it's so often that it's annoying. I've stopped using this integration until proper OAuth is implemented.

I've also noticed the same thing with Grohes own Android App. Have to sign in again after some weeks. So could be the problems are coming from their own services.

// Joachim

— Reply to this email directly, view it on GitHub https://github.com/gkreitz/homeassistant-grohe_sense/issues/23#issuecomment-1097887254, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKPWGUMN62BOBVL4OG5O5TVE2PB5ANCNFSM5TELYF7Q . You are receiving this because you authored the thread.Message ID: @.***>

autopower commented 1 year ago

It looks like new refresh token is created everytime you access Grohe endpoint via mobile app.

jahiekk commented 2 weeks ago

I implemented automatic token refresh on the sensor code: https://github.com/jahiekk/homeassistant-grohe_sense

peternorlander commented 2 weeks ago

Hi Jahiekk,

Interesting! Thanks for sharing! I have been running on this branch for about a year and it works pretty good. Have you implemented any other changes aside from this branch? https://github.com/gkreitz/homeassistant-grohe_sense/pull/31

Den fre 30 aug. 2024 kl 06:45 skrev jahiekk @.***>:

I implemented automatic token refresh on the sensor code: https://github.com/jahiekk/homeassistant-grohe_sense

— Reply to this email directly, view it on GitHub https://github.com/gkreitz/homeassistant-grohe_sense/issues/23#issuecomment-2320032285, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKPWGR3XQ2MAJ5SCJEMH4LZT72GVAVCNFSM6AAAAABNLW2PBOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRQGAZTEMRYGU . You are receiving this because you authored the thread.Message ID: @.***>