Skip to content

[bug] Unable to connect with Home Assistant #243

Open

Description

@Depechie

Is there an existing issue for this?

  • I have searched the existing issues

Description

Been using the remote with the Home Assistant connection since the beta release and always worked great.
But since v1.4.8 I have not been able to use the remote ( cfr #206 ).

So I thought to do a factory reset with the firmware being on v1.5.2 to see if things would be better. But I guess this maybe brings up the actual problem?
I can no longer connect to Home Assistant, or rather complete the setup procedure because I think the connection is working, but somehow querying Home Assistant fails.
I can totally understand that this is somehow a local network issue, but I have no idea how to go about checking things and reporting, although it worked well with previous firmwares.

I recorded the procedure and you will see that during it, I also got several remote disconnects, although it was constantly in the dock.

https://github.com/Depechie/RemoteTwo/blob/main/Screen%20Recording%202023-12-26_1080.mov

The log that is reproduced during the procedure several times is this

Logger: aiohttp.server
Source: /usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py:421
First occurred: 12:34:02 PM (2 occurrences)
Last logged: 12:34:18 PM

Error handling request
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/http.py", line 371, in async_handle
    msg = await wsock.receive()
          ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_ws.py", line 440, in receive
    msg = await self._reader.read()
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/streams.py", line 663, in read
    return await super().read()
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/streams.py", line 622, in read
    await self._waiter
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 452, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_app.py", line 543, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 114, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 85, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 233, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/headers.py", line 31, in headers_middleware
    response = await handler(request)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 149, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/http.py", line 50, in get
    return await WebSocketHandler(request.app["hass"], request).async_handle()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/http.py", line 435, in async_handle
    await self._writer_task
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/http.py", line 151, in _writer
    await send_str(message)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_ws.py", line 336, in send_str
    await self._writer.send(data, binary=False, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 729, in send
    await self._send_frame(message, WSMsgType.TEXT, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 691, in _send_frame
    await self.protocol._drain_helper()
  File "/usr/local/lib/python3.11/site-packages/aiohttp/base_protocol.py", line 90, in _drain_helper
    await asyncio.shield(waiter)
  File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 1076, in _write_ready
    n = self._sock.send(self._buffer)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BrokenPipeError: [Errno 32] Broken pipe

How to Reproduce

Open the web configurator
Go to integrations
Add Home Assistant integration

Expected behavior

Integration has an active connection and would load all entities

System version

v1.5.2

What part of the system affected by the problem?

Integration

Additional context

No response

Activity

  1. Depechie commented on Dec 26, 2023

    @Depechie
    Author

    And seeing that I own 2 remotes, I unboxed the other one and that one is still running v1.0.5 !
    And looking at the recording https://github.com/Depechie/RemoteTwo/blob/main/Screen%20Recording%202023-12-26_2_1080.mov my thoughts are correct.
    That version has no problem with my Home Assistant setup and handles the integration perfectly.

  2. zehnm commented on Dec 27, 2023

    @zehnm

    @Depechie can you provide the Home Assistant integration logs from Remote2?
    I don't see much in the Home Assistant server logs. The sock send error is most likely because the remote went to sleep during configuration (as seen in the recording).

    To make sure the Remote doesn't go to sleep it's best to put it in the dock while configuring with the web-configurator.

    In my home setup I'm also using 1.5.2 with HA on a daily basis and haven't seen these issues. I've retested with a new remote with 1.4.4, setup up the HA integration, upgraded the remote to 1.5.2, without getting an error.

    From the screen recording I see that you use ws:// protocol and an IPv4 address. Is this correct? Using an IP address already rules out mDNS name resolving.
    Is the remote on the same network as your HA server?

    With the integration logs I hopefully see what kind of issue you have. The only thing which could maybe cause an issue is the number of entities (> 500). I haven't reached that many yet :-) Depending on the returned data, the Max WebSocket frame size in the extended settings might have to be increased, or the connection timeout setting. But let me have a look at the logs first.

  3. Depechie commented on Dec 27, 2023

    @Depechie
    Author

    Hey Markus!
    Thx already for the detailed questions. I will try to do some more tests and will upload the logs.

    In reference to the remote, it was constantly in the dock!

    I reference to the ws:// and the IP address, I use that because I think my setup is not broadcasting with a name.
    It are docker compose installs...
    I normally use my external public https URL for regular use, can I use that in the remote too?

    In reference to the entity count, I do have a lot! But I also have a clean home assistant running on another device with only the Denon integration running and I see the same problems... but if needed I can always do tests with that one too and provide logs.

  4. Depechie commented on Dec 27, 2023

    @Depechie
    Author

    UCR2_logs_2023-12-27 (2).txt
    UCR2_logs_2023-12-27 (1).txt
    UCR2_logs_2023-12-27.txt

    @zehnm I the file with (2) at the end is the last one where you can see that I tried both my Home Assistant instances ( difference is one is my main driver with all the integrations I need, the other is for testing things out and now only has the Denon integration and my TV )

  5. zehnm commented on Dec 28, 2023

    @zehnm

    Thanks for the logs. I see two things:

    1. Continous Websocket server heartbeat failed, disconnecting! entries.
    2. [192.168.1.137:8123] Invalid authentication
      This is only logged if the Home Assistant server rejected the authentication.
      But that could also be a rejection because of too many connection attempts.

    There are many Authentication OK entries, which are logged if HA accepted the long lived access token.
    Looking at the log timestamps shows something quite interesting: a pair of successfull connection events (Connected to: ws://192.168.1.137:8123/api/websocket and Authentication OK) sometimes appear within the same second, as two client connections are active at the same time and fighting over the server connection (and probably "killing" each other because of modified heartbeat timings). This should not happen, there should only be one client connection.

    Since you have 2 HA servers, I suspect there's an issue with connection management when the server configuration is changed and the old HA client (re-)connection handling isn't properly closed.

    Could you please do the following to get the startup logs?

    1. Change the Home Assistant integration configuration to the server which cannot be connected (192.168.1.137)
    2. Reboot the remote
    3. Wait two to three minutes and making sure the remote doesn't go to sleep
    4. Download again the Home Assistant integration logs with DEBUG, number of entries: 1000

    If a simple reboot after changing the HA settings in the integration "fixes" it (or only shows a single reconnection pattern), then my assumption about the connection handling is true. Otherwise, we have to keep digging.

    I normally use my external public https URL for regular use, can I use that in the remote too?

    If everything is properly setup, then yes. Personally I don't recommend it if your HA server is on the same internal network and accessible with ws://.
    Either you need a split DNS setup or the router needs to support NAT hairpinning. The reverse proxy for https has to be setup correctly as well, with a public certificate (not self-signed).
    There's another open ticket with a Traefik reverse proxy where it doesn't work: #189
    I've successfully tested it with Nginx, but this wasn't on the internal network.

  6. Depechie commented on Dec 29, 2023

    @Depechie
    Author

    UCR2_logs_2023-12-29.txt
    Here are the latests logs, so I first added the HA integration to the ws://192.168.1.137 endpoint and rebooted the remote.
    Waited a few minutes ( while the remote was in the dock ) and then extracted the logs.

    It seems the heartbeat problem is still visible in the logs.

    Just out of curiosity, would it be better to stop the other HA server I have running?

  7. Depechie commented on Dec 29, 2023

    @Depechie
    Author

    UCR2_logs_2023-12-29 (1).txt
    Screenshot 2023-12-29 at 17 45 11

    So stopping the other HA, does seem to have 'some' effect?
    Not always, but it seems that now the integration will actually load the entities... I still sometimes get timeout errors in the UI.

    On the other hand, I do have to stress that also the remote itself often is unresponsive even though it is kept in the dock, example:

    • so the UI will not load all items when going to Settings
    • full page refresh with the 2 dots spinner

    Still think that a combination of both is giving me these problems, but hard to tell of course. Just let me know what I can provide as info to get a complete picture.

  8. zehnm commented on Dec 29, 2023

    @zehnm

    Just out of curiosity, would it be better to stop the other HA server I have running?

    No, they don't interfere and our HA integration doesn't do any automatic discovery of server instances yet.

    There is definitely an issue with the WebSocket ping pong heartbeat. The integration never receives a pong after sending a ping and therefore closes the connection after the configured timeout of 40s.
    The HA integration doesn't use regular text messages (as described in the Home Assistant WebSocket API), but native ping frames. This has been working well with the official HA Docker image ghcr.io/home-assistant/home-assistant:stable and with the Home Assistant Yellow.

    @Depechie a few more questions about your setup:

    • Do you have any proxy, reverse proxy, firewall, etc between the Remote Two and your Home Assistant server (basically anything that could interfere with the WebSocket communication)?
      Or does your Docker Compose setup directly map port 8123 to the Home Assistant server?
    • Wich version of Home Assistant do you have?
    • Which Docker image do you use in your Docker Compose installation?
    • Do you have multiple WiFi access points with roaming?

    To rule out a general connection or communication problem with your HA server (and to pin the issue on ping pong frames), I'd like you to try increasing the heartbeat timeout:

    1. In the HA integration page: select "Start integration setup"
    2. Enable Configure enhanced options and click Next
    image
    1. Set Heartbeat timeout to the max value of 300 (the current version doesn't allow disabling it or setting higher values).
    image

    This gives you more time to try out setting up an entity and see if the interaction with Home Assistant is working, or if the communication is not working at all. For example with a single light entity.
    If the communication is working, then it's the ping pong frames. Otherwise, we need to keep trouble shooting...

    In the meantime I start preparing a new version for the next release where one can choose the native ping method (as of now), or the manual ping messages as described in the HA API. A long holiday weekend is coming up, so this will take a few days. But I'll keep an eye on this issue.
    Thank you for your patience!

  9. Depechie commented on Dec 29, 2023

    @Depechie
    Author

    @zehnm I'm on my holiday so that is why I have time... but please do not feel the urge to respond directly and take time off 😃

    Most important thing for me is that someone at least tries to help, how long it takes for replies or suggestions is of no importance.

  10. Depechie commented on Dec 29, 2023

    @Depechie
    Author

    Some answers

    • If the communication is working, then it's the ping pong frames. Otherwise, we need to keep trouble shooting...

    Since I turned of the test HA, it is actually working much better, I can control lights and also my Denon AVR and Panasonic TV through the Remote ( through the HA integration ).
    Also the entities seem to load fine. I did reboot the remote a lot also.
    I did NOT change the Heartbeat timeout yet and I looked at the logs and that does keep happening ( heartbeat failed ) a lot.
    UCR2_logs_2023-12-29 (2).txt

    Did a small test with the test HA back up again and I got a timeout again for loading the entities. Could be totally random of course, as you said both should not interfere. But still, going to keep the test HA off to have better playing environment to rule it out.

    • Do you have any proxy, reverse proxy, firewall, etc between the Remote Two and your Home Assistant server (basically anything that could interfere with the WebSocket communication)?

    Both devices run on the same network, that is hosted by a Unifi Controller. No separated vnet's or anything. I do have 2 pi-hole's on the same network.
    If needed I can provide more info ( but maybe not in a public channel? I do have access to the beta repo )

    • Or does your Docker Compose setup directly map port 8123 to the Home Assistant server?

    The test HA is started on a raspberry pi with: https://github.com/Depechie/HomeLab/blob/main/raspberry/docker-compose.yml it is running netwok mode host
    The main HA was done with https://community.home-assistant.io/t/installing-home-assistant-supervised-using-debian-12/200253 ( but that page get's updated a lot when linux versions of HA versions change )

    • Which version of Home Assistant do you have?

    Main HA: 2023.12.4
    Test HA: 2023.12.3

    • Which Docker image do you use in your Docker Compose installation?

    Cfr github link

    • Do you have multiple WiFi access points with roaming?

    Yes! Unifi AP's. I could set the remote to a fixed AP in the Unifi config I think. Maybe that worth a shot? On the otherhand, 1 AP is right below the Remote Two dock. If I look at the logs in the Unifi Controller I also do not see it switching to another AP it was always connected to the one in the same room.

  11. Depechie commented on Dec 29, 2023

    @Depechie
    Author

    UCR2_logs_2023-12-29 (4).txt
    Added the core logs too seeing that I still often have the disconnected remote spinner in the UI even though it is in the dock.

  12. 10 remaining items

  13. gerryex3 commented on Jan 20, 2024

    @gerryex3

    I'm getting closer but still not working. There were two updates for HA Green and the firmware update to R2. Now when I try the integration the "Select Entities" screen that comes up actually lists the entities that are in my HA Green. Wow, that never happened before - progress. But I was truly disappointed that after the "You're all set" screen, I was still left with nothing below "Available Entities" and to the left it still says State: Active but "Disconnected" right below that.

    R2 must have made some connection to HA to get the list of Entities but for some reason after that it can no longer communicate to HA, says its disconnected and cannot display any Available Activities.

    ANY help would be appreciated!

  14. Depechie commented on Jan 21, 2024

    @Depechie
    Author

    I indeed see same behaviour as @gerryex3 the integration does show the amount of entities ( just the total number ). But after that no connection.

  15. zehnm commented on Feb 6, 2024

    @zehnm

    The just released 1.6.9 firmware fixes the Home Assistant connection timeout setting. This was always using the WebSocket framework's default value of 5 seconds and the connection timeout was used as overall request timeout.

    To help analysing connection issues I've created a simple command line tool for Windows, Mac & Linux, which uses the exact same code and logic as the integration. It connectes to the HA server, subscribes for entity state changes and requests all entity states. The full debug log including message communication is printed in the console (only the long-lived HA access token is filtered out).

    I hope this will help figuring out why the integration is not working for you. It would be great if you could test it and post any errors you get.

  16. Depechie commented on Feb 11, 2024

    @Depechie
    Author

    Hey @zehnm the lack of response is not about not having time or anything. But since some point in time from 1.6.1 the integration started to work!
    The only thing I notice is that sometime a command will still lag or not fire and I just need to recall it.

    For reference I added a log again, I still see some reconnections so maybe that could be why I still get the lags?
    But overall, the actual connection works, although still not flawless or super great.

    UCR2_logs_2024-02-11_homeassistant.txt

  17. Depechie commented on Feb 11, 2024

    @Depechie
    Author

    UCR2_logs_2024-02-11_homeassistant.txt
    Got some 'internal error please try again later' while trying to change the volume of my receiver through the Home Assistant integration.

    Have the same issue with AppleTV. Somehow still think it is related and related to network. But not sure what or how.

  18. Depechie commented on Jun 13, 2024

    @Depechie
    Author

    After changing my network settings in Unifi, these problems seem to be fixed.

  19. moved this from Reported to Done in Bug reportson Jun 13, 2024
  20. rohankapoorcom commented on Jun 13, 2024

    @rohankapoorcom

    What settings did you change? I'm seeing similar issues with home assistant.

  21. Depechie commented on Jun 13, 2024

    @Depechie
    Author

    To be honest... not sure what or how I fixed it.
    But I had other issues also and with some online readme's I came to following:

    Screenshot 2024-06-13 at 23 05 51
  22. Depechie commented on Jun 16, 2024

    @Depechie
    Author

    2 days it worked flawlessly and now the remote keeps saying 'connecting' again for home assistant.
    I really still think it is a network issue, but totally no clue how to keep it stable :(

    UCR2_logs_2024-06-16.txt

  23. moved this from Done to In Progress in Bug reportson Jun 16, 2024
  24. ERAU6 commented on Sep 13, 2024

    @ERAU6

    I have a similar, if not the same issue. It does appear to be a communication issues between Remote and HA, but don't know how to troubleshoot. I tried to download the logs, but the file is empty (I did not apply the filters correctly, I guess).

    It also looks like I have the same Unifi network as @Depechie, so it may be a configuration setting.

    I would appreciate some feedback and guidance on this issue.

  25. ERAU6 commented on Sep 17, 2024

    @ERAU6

    Hello @zehnm - Can you update us on this issue of UCR connecting to Home Assistant, if you can reproduce, and if this will be fixed in the next firmware update or is there a configuration solution?

    Image 9-16-24 at 7 22 PM
    Image 9-17-24 at 11 21 PM

    New Note

    UCR2_logs_2024-09-18.txt

    Thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingintegrationIntegration driver related issue

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions