home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.61k stars 30.77k forks source link

Unable to add Withings integration #120182

Closed gitfmc closed 4 months ago

gitfmc commented 4 months ago

The problem

Hi, I am unable to add the withings integration to home assistant. I have followed the instructions exactly. I am using a duckdns url (tested and working), I've configured My Home Assistant (https://my.home-assistant.io) to point to the correct url (without any space or backlash at the end). I've tested it in different browsers (edge and chrome). I've whitelisted all ips from withings on my router/firewall (found here: https://developer.withings.com/developer-guide/v3/data-api/keep-user-data-up-to-date/). I'm logged in on both withings developer website and home assistant using the external url. Every time I try to add the integration I get an error message: {"errors":[{"message":"redirect_uri_mismatch: The redirect URI provided is missing or does not match partner callback url"}]} Unfortunately there are no logs as the integration is not added. I've had this working in the past. Is this integration dead?

What version of Home Assistant Core has the issue?

core-2024.6.4

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Withings

Link to integration documentation on our website

https://www.home-assistant.io/integrations/withings/

Diagnostics information

integration not added - no diagnostics

Example YAML snippet

no yaml configuration

Anything in the logs that might be useful for us?

integration not added - no logs

Additional information

No response

home-assistant[bot] commented 4 months ago

Hey there @joostlek, mind taking a look at this issue as it has been labeled with an integration (withings) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `withings` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign withings` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


withings documentation withings source (message by IssueLinks)

joostlek commented 4 months ago

Do you have my disabled in your instance? If you don't know what I am talking about probably not.

What callback URL did you set at withings?

gitfmc commented 4 months ago

I do not have my disabled. As per the instructions, using https://my.home-assistant.io/redirect/oauth

joostlek commented 4 months ago

Be sure to try again with a new client in the Withings dashboard, since sometimes that can also help with issues. You can remove the old client id and secret from integrations -> 3 dots -> application credentials

gitfmc commented 4 months ago

Yes, I see the credential but can't find a way to delete it? There is only a button at the bottom right corner that says + add application credential

joostlek commented 4 months ago

There should be a value on the screen where the Withings client id and secret are stored from your integration, otherwise just start the setup over

gitfmc commented 4 months ago

I've deleted the integration some time ago as I had problems with the sleep mat and had it returned. I think that credential is from that time. But it seems the ability to remove credentials from homeassistant was removed - i just found this recent post: https://community.home-assistant.io/t/why-cant-you-delete-application-credentials-any-more/739283 I never manage to add the integration so i can't start over. when I add it i am taken to that error webpage and the integration is never added. Is there any other way to delete the old credential?

joostlek commented 4 months ago

Oh this is interesting, there is indeed no way to delete them anymore, let me raise this at some frontend people, maybe they know why this is the case now. The other way would be to update .storage/application_credentials but do that on your own risk

gitfmc commented 4 months ago

Thanks, will try that now and revert back

gitfmc commented 4 months ago

ok, success. I've managed to delete the credentials, restarted home assistant, added the integration again and this time it asked me for the credentials and i got redirected to the withings developer site to authorize the app. all is good so far. Unfortunately after adding, only the deep sleep entity shows up (and shows the correct value, just checked it on the withings app) but the "in bed" sensor doesn't even show up on the list of entities unavailable. I've tried laying down in bed for a few minutes but it doesn't show up. What could the problem be?

joostlek commented 4 months ago

Lay in bed, it will be added on the fly

gitfmc commented 4 months ago

I've laid in bed for about 2 minutes and it didn't show up. Show i stay in bed longer?

joostlek commented 4 months ago

Oh in that case your webhook did not work, you should consult your logs and check what's going on

gitfmc commented 4 months ago

Just laid in bed for 15 minutes and still not showing up.

Indeed, i get an error on the logs related to the webhook:

Logger: homeassistant
Source: components/withings/__init__.py:193
First occurred: 20:56:41 (3 occurrences)
Last logged: 21:09:15

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/withings/__init__.py", line 263, in register_webhook
    await async_subscribe_webhooks(self.withings_data.client, webhook_url)
  File "/usr/src/homeassistant/homeassistant/components/withings/__init__.py", line 193, in async_subscribe_webhooks
    await client.subscribe_notification(webhook_url, notification)
  File "/usr/local/lib/python3.12/site-packages/aiowithings/withings.py", line 351, in subscribe_notification
    await self._request(
  File "/usr/local/lib/python3.12/site-packages/aiowithings/withings.py", line 135, in _request
    raise WithingsInvalidParamsError(error)
aiowithings.exceptions.WithingsInvalidParamsError: The callback URL is either absent or incorrect

any ideas?

joostlek commented 4 months ago

Reload the integration, check the logs again. Apparently withings doesn't like the URL. They aren't really vocal about why it's wrong so this.

gitfmc commented 4 months ago

I've reloaded the integration, went to lay in bed and checked the logs again - the exact same error shows up.

Is there anything else I can try? Any other callback URL i can try?

joostlek commented 4 months ago

This has nothing to do with the oauth anymore, this is withings telling you they can't reach your webhook

gitfmc commented 4 months ago

I've seen this post: https://stackoverflow.com/questions/30192322/notification-operation-always-returning-statusc-code-293-the-callback-url-is-e and i've tried using http://my.home-assistant.io/redirect/oauth instead of https but that doesn't work - and no error shows up on the logs either. That also made me think, I'm using the Nginx Proxy Manager add-on - can that be the culprit? The proxy host i have for home assistant is pretty standard, same as others (and i can reach the public url fine). I have cache assets, Block common exploits and websockets support all enabled. On the ssl tab I also have enabled Force SSL, HTTP/2 Support and HSTS Enabled. no variables set in the Advanced tab. Can any of those settings be interfering? If not, is there nothing else I can do? is it an issue from withings side or is there anything else i can try?

joostlek commented 4 months ago

It's not a thing with the my.homeassistant, as that's for oauth. The oauth connection is now made and is only important with reauthentucating if that ever needs to happen.

This now probably a configuration issue from your side. About 9k users also use this integration, so if it was a global issue this would be a bigger issue on GitHub.

gitfmc commented 4 months ago

Ok, thanks for your assistance.

I've found this post here on the forum: https://community.home-assistant.io/t/struggling-to-get-withings-integrations-fully-operational/405553/18 and i've tried inputting the callback url as: https://{my_homeassistant_url}.duckdns.org/api/webhook/{my_webhook_id_from_core.config_entries} and when testing on the withings developer website it goes green. but now i get a new error on the logs:

Logger: homeassistant
Source: components/withings/__init__.py:193
First occurred: 22:05:06 (3 occurrences)
Last logged: 22:17:03

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/aiowithings/withings.py", line 107, in _request
    response = await self.session.request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 608, in _request
    await resp.start(conn)
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client_reqrep.py", line 976, in start
    message, payload = await protocol.read()  # type: ignore[union-attr]
                       ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/streams.py", line 640, in read
    await self._waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/aiowithings/withings.py", line 106, in _request
    async with asyncio.timeout(self.request_timeout):
  File "/usr/local/lib/python3.12/asyncio/timeouts.py", line 115, in __aexit__
    raise TimeoutError from exc_val
TimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/withings/__init__.py", line 263, in register_webhook
    await async_subscribe_webhooks(self.withings_data.client, webhook_url)
  File "/usr/src/homeassistant/homeassistant/components/withings/__init__.py", line 193, in async_subscribe_webhooks
    await client.subscribe_notification(webhook_url, notification)
  File "/usr/local/lib/python3.12/site-packages/aiowithings/withings.py", line 351, in subscribe_notification
    await self._request(
  File "/usr/local/lib/python3.12/site-packages/aiowithings/withings.py", line 115, in _request
    raise WithingsConnectionError(msg) from exception
aiowithings.exceptions.WithingsConnectionError: Timeout occurred while connecting to Withings

Can you help me understand it? does it mean that the callback url is now reachable but not receiving a response in useful time?

On the page i posted early (https://stackoverflow.com/questions/30192322/notification-operation-always-returning-statusc-code-293-the-callback-url-is-e), a user also says that:

"Turns out that the Withings API needs a fast response (under 1 second?) and my local flask server being tunneled through ngrok did not reply fast enough. I solved this by enabling threading in the flask server. app.run(port=8080, debug=True, threaded=True)"

I'm using a raspberry pi 3B dedicated solely to home assistant. Can this be the issue in your opinion?

I appreciate all your help, thank you.

joostlek commented 4 months ago

Honestly, I have no clue. I myself use HA cloud because I can't portforward at home. So I haven't encountered this whole problem.

gitfmc commented 4 months ago

Alright, fair enough.

I really appreciate all your help with this - thank you so much!

joostlek commented 4 months ago

Closing as it's not an integration problem by the looks of it