home-assistant / android

:iphone: Home Assistant Companion for Android
https://companion.home-assistant.io/
Apache License 2.0
2.32k stars 641 forks source link

wifi (only) always triggers "unable to connect to home assistant" #3860

Closed jhaar closed 1 year ago

jhaar commented 1 year ago

Home Assistant Android app version(s):

Android version(s): latest from Play Store - 20230908.0 Device model(s): Samsung S21 and S23 Home Assistant version: 2023.9.1 Last working Home Assistant release (if known): 6-ish months ago?

Description of problem, include YAML if issue is related to notifications:

Several months ago the Android Home Assistant app became unreliable: it works over Cellular, but at home over wifi it produces this "unable to connect to home assistant" error and demands I set it up again (which doesn't work either). If I drop out of the app, disable Wifi, and re-enter the app, it starts working. And conversely, if I drop out, re-enable Wifi - it stops working.

I have just upgraded from my old Samsung S21 to a S23 and the problem remains - it affected both. I have also tried uninstalling the app, reinstalling - no change. And of course my homeassistant app works fine from my laptop over wifi - there is no problem. ie the "https://homeassistant.my.domain/" works at home over wifi via a web browser on the Android and laptop - but the HomeAssistant app on the Android does not.

BTW I do run my own DNS at home, so my homeassistant DNS name resolves to a 192.168 address on Wifi and it resolves to it's Internet address over cellular - that's the only difference I can think of between the two "network options". Both DNS names resolve to the same, working homeassistant web server app.

I have homeassistant running in a docker behind a reverse proxy - the DNS name (internal and external) point to the same URL "value" (on wifi resolve to the 192.168 IP and remote/cellular resolving to the Internet IP).

Any ideas what's going wrong? Ruins several phone-based triggers I have, because I have to remember to disable wifi for them to work.

Thanks!

Jason

Companion App Logs:

Screenshot or video of problem:

Additional information:

dshokouhi commented 1 year ago

Please do not skip the logs portion. It will contain the reason why it's not loading on Wi-Fi for you. Most likely something to do with your network.

dshokouhi commented 1 year ago

Closing due to lack of response from author

jhaar commented 11 months ago

Sorry, I didn't see the followup. Attached is the logfile shown on the Android app under "Show and Share Logs". I couldn't see anything obviously related to the issue. All I can say is that I reproduced the "Unable to connect to Home Assistant" error, disabled wifi, successfully refreshed HA app, then saved the logfile.

homeassistant_companion_log_10-30-2023_14-50-38.txt

dshokouhi commented 11 months ago

the cannot connect pop-up show up in 2 cases.

jhaar commented 11 months ago

OK. I triggered the issue in the Android HA app again (i.e. on wifi on my home LAN), then went into Chrome on the same Android and successfully accessed the same HA instance (i.e. to my mind proving there is no LAN issue). I then went into the HA app settings and dumped out the logs - attached

homeassistant_companion_log_11-1-2023_14-48-2.txt

dshokouhi commented 11 months ago

in this case we do not see the HA frontend responding that it has been connected in the allotted time of 10 seconds, the pop-up should go away if response comes back that it has connected but I don't see that communication at all.

https://github.com/home-assistant/android/blob/master/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt#L691

Whats odd is that we do see communication like opening settings but not what we expect.

12-01 14:46:05.848 18810 30092 D WebviewActivity: External bus {"type":"config/get","id":1}
12-01 14:46:05.850 18810 18810 D WebviewActivity: externalBus({"id":1,"type":"result","success":true,"result":{"hasSettingsScreen":true,"canWriteTag":true,"hasExoPlayer":true,"canCommissionMatter":true,"hasAssist":true}});

now I do see some other interesting errors:

12-01 14:45:04.397 18810 19647 W SensorReceiver: Exception while updating sensors: IntegrationException: java.net.UnknownHostException

Double check the URLs the app has in Settings > Companion app > Selecting your server

host exception is peculiar and unexpected and might be why the app is not getting the expected communication

12-01 14:45:03.773 18810 18810 E WebViewPresenterImpl: Unable to retrieve external auth

if you say this only happens in a certain situation can you try logging out and logging back in and making sure to enter your credentials instead of using something like trusted networks to rule out a weird issue?

jhaar commented 11 months ago

well that gets weirder. I was able to continually hit the "Settings" link on the "Unable to connect to Home Assistant" popup to access my profile, and after a few popups->SETTINGS->action iterations hit the logout button. After that (deliberately stuck to wifi) I went into Android Settings and killed the HA app to ensure it was starting fresh and accessed it again. It must still have my HA url as it immediately does the error popup - but now the SETTINGS doesn't work as I'm not logged in. But that implies my logout worked whilst it was telling me it couldn't access the server???

Then I disabled wifi, hit the "REFRESH", and was logged in (ie I didn't have to enter password). That contradicts my previous sentence :-)

I'll attach that here. I'll logout again via Cellular and make a new followup

homeassistant_companion_log_11-1-2023_16-59-36.txt

dshokouhi commented 11 months ago

if settings window does not open then that further suggests the frontend is not communicating to the app

if you logged out and were still logged in then please follow the start fresh steps as something sounds corrupted

https://companion.home-assistant.io/docs/troubleshooting/faqs/#starting-fresh-with-the-android-app

jhaar commented 11 months ago

OK, after disabling wifi, I reinitialized the app, ie had to add my URL, then my username/password. Got it up-and-running.

I've re-enabled wifi and it's still working - even after I killed the app and restarted it. However, yesterday ago I did exactly the same thing - but the problem came back within hours. So let's wait and see what happens next :-)

Here's the latest logfile from this working situation

homeassistant_companion_log_11-1-2023_17-11-51.txt

jhaar commented 11 months ago

As predicted it has broken again. I had left the house for a while, so it would have gone from wifi+cellular to cellular and then back to wifi+cellular. Attached is the logfile taken straight after showing evidence of being broken, and again I cranked up Chrome on Android and - unlike the HA app - Chrome could access the HA url just fine.

homeassistant_companion_log_11-1-2023_19-37-4.txt

dshokouhi commented 11 months ago

I can see that immediately we got a disconnected message so that is expected to see the pop-up

12-01 19:36:45.957 24500 24709 D WebviewActivity: External bus {"type":"connection-status","payload":{"event":"disconnected"},"id":5}

did you double check the URLs we are still seeing the Unknown Host Exception.

12-01 19:36:55.209 24500 24553 W SensorReceiver: Exception while updating sensors: IntegrationException: java.net.UnknownHostException

did you buy chance try the starting fresh steps I linked? your logs still show an issue getting authentication.

12-01 19:36:47.122 24500 24500 E WebViewPresenterImpl: Unable to retrieve external auth
jhaar commented 11 months ago

So to remove any confusion, I uninstalled the HA app and installed again from Google Play. While on wifi, I started it and it was pre-filled with the correct URL for my HA server. I think HA does some multicast on LAN to achieve that? Anyway, I clicked next and immediately got an error that it was unable to connect (server or proxy hostname lookup failed). I then selected configuring it manually, entered the same URL and it failed with the same error. I then cut-n-pasted that URL into Chrome and it worked...

I then disabled wifi and retried in the HA app - then it worked. I then logged into my account first time, clicked through the settings wizard and dumped out the logfile (attached).

homeassistant_companion_log_11-2-2023_7-1-0.txt

jhaar commented 11 months ago

hmmm, I can see the following line

base_url=https://homeassistant.whanau.org-external_url=https://homeassistant.whanau.org-internal_url=https://homeassistant.whanau.org%uuid=1b8ec99ce7de4a24ae71dfbcd2151aa3

that internal one is corrupt: shouldn't "org%uid=" be "org/%uid="?

I just edited my server config and changed the url (external and internal were identical) to explicitly end with a slash (they didn't), restarted my HA docker and reinstalled HA app on my Android again. Didn't help - still the same error. Interestingly, even though the HA server "Network->Home Assistant URL" external/internal urls now end with a slash, the HA app autodetect still doesn't show the slash. And when I choose to set it manually, it is pre-filled with that url - but the "connect" button is grayed out until I put a slash at the end. Doesn't really matter - it still failed with "unable to connect" :-(

dshokouhi commented 11 months ago

I'm afraid this isn't going to be something the app can solve but your research should show you that its some sort of networking issue maybe at the proxy or hostname but if you saw that error from the app then its coming from somewhere and only the user whose in control of the network can solve it.

the pre-filled data is from discovery which is handled at the server level, indeed some setups can send incorrect data and some users need to manually enter the URL which is why that option is there.

the fact that it works sometimes and not everytime does indeed suggest an internal networking issue as the app handles all URLs the same way, we just attempt to load and use them.

jhaar commented 11 months ago

FYI you've mentioned "trusted networks" a couple of times. To answer that, there is no "trusted_networks" nor "auth_providers" defined in my configuration.yaml

jhaar commented 11 months ago

I ran tcpdump on the HA server (which is also the LAN DNS server) and captured traffic from the Android. When I install and then open the HA app, I can see it doing the mdns lookups and discovering the url - but I see ZERO attempts to connect to the url - and no DNS lookup for the hostname either. That got me thinking: as the Android is on both wifi and cellular, what DNS value is returned when it does the DNS lookup? The LAN DNS maps to an IPv4 192.168.XX address, and cellular would map to a different Internet IPv4 address (NAT firewall). So maybe it had cached the DNS value from cellular DNS lookups and was trying to connect to the Internet IPv4 over the LAN? That would fail: the Internet IPv4 address isn't reachable on the LAN. And maybe it works now-and-then when the cached DNS value is from the LAN?

So I edited the internal_url to be http://192.168.X.Y:8123 instead of the "proper" DNS url - and it worked! It's been a few hours now and it's still working.

There's an old Internet saying: "it isn't DNS, it isn't DNS.... It was DNS..."