core: Error when calling Sonos websocket: Connection timed out

The problem

Effectively the same problem as the original issue in https://github.com/home-assistant/core/issues/92570 - I’ve read through the networking steps and by all accounts I have the right ports open; I’m running HA in docker host mode on a Synology NAS that allows all local networking in the firewall. My Sonos speakers are all on the same subnet/vlan as the NAS.

What’s weird is if I copy the yaml for the automation action and run it in the “services” panel it works no problem. If I run the action itself in the automation, also works fine. And that’s the case everytime I notice the log message and try retesting.

I’m playing the TTS on a Sonos group with an Arc, Sub, and 2 Era 300s.

What version of Home Assistant Core has the issue?

2023.6.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Sonos

Link to integration documentation on our website

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

Diagnostics information

I’ve enabled debug logging, will add when the issue next happens.

Example YAML snippet

service: media_player.play_media
data:
  announce: true
  media_content_id: media-source://tts/cloud?message="Someone is at the door"
  media_content_type: music
target:
  entity_id:
    - media_player.living_room_sonos
continue_on_error: true


### Anything in the logs that might be useful for us?

_No response_

### Additional information

_No response_

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 21 (4 by maintainers)

Most upvoted comments

It will be in 2024.2.

This was annoying me tonight, for me, on my old play:1 units, if they’re already playing something, the web socket connection appears to take more than 3seconds to iniitalise itself. (This is tested from a client separate from my home assistant server.)

I’ve submitted a PR to the underlying sonos_websocket library to allow for providing a tunable connection timeout parameter, as when I tune it to 6 seconds (the default is 3) the chance of a successful web socket connection nears 100%, but at 3s it was closer to 20% success rate. If this gets accepted, routing it through to the home assistant core code will allow those of us experiencing this behaviour to be more accepting of the connection timeout should we want to be.

Here’s the logs as promised:

Logger: homeassistant
Source: components/sonos/media_player.py:529 
First occurred: 22 June 2023 at 10:57:59 (3 occurrences) 
Last logged: 13:59:43

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/sonos_websocket/websocket.py", line 60, in connect
    self.ws = await self.session.ws_connect(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 779, in _ws_connect
    resp = await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 536, in _request
    conn = await self._connector.connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 540, in connect
    proto = await self._create_connection(req, traces, timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 901, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/connector.py", line 980, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore[return-value]  # noqa
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 1112, in create_connection
    transport, protocol = await self._create_connection_transport(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 1145, in _create_connection_transport
    await 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.11/site-packages/sonos_websocket/websocket.py", line 59, in connect
    async with asyncio_timeout(3):
  File "/usr/local/lib/python3.11/asyncio/timeouts.py", line 111, 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/sonos/media_player.py", line 524, in async_play_media
    response, _ = await self.speaker.websocket.play_clip(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sonos_websocket/websocket.py", line 137, in play_clip
    return await self.send_command(command, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sonos_websocket/websocket.py", line 92, in send_command
    await self.connect()
  File "/usr/local/lib/python3.11/site-packages/sonos_websocket/websocket.py", line 73, in connect
    raise SonosWSConnectionError("Connection timed out") from exc
sonos_websocket.exception.SonosWSConnectionError: Connection timed out

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1034, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 851, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/sonos/media_player.py", line 529, in async_play_media
    raise HomeAssistantError(
homeassistant.exceptions.HomeAssistantError: Error when calling Sonos websocket: Connection timed out

#94687 Same issue here