Errors When Using Media_player.join With SONOS

A breaking change announced with release of HA 2022.6 revealed a need to update a couple of my scripts.

BREAKING CHANGE - The sonos.join and sonos.unjoin services will be removed in 2022.8 in favor of the standard media_player.join and media_player.unjoin services.

I’ve been successful at editing one script. A script that previously used sonos.unjoin has been update to use media_player.unjoin. It’s working without an issue. A second script, one that joins three Sonos units, is giving me intermittent errors. The script is;

service: media_player.join
data:
  group_members:
    - media_player.kitchen_sonos
    - media_player.livingroom_sonos
target:
  entity_id: media_player.breakfast_sonos

Here’s what’s in the logs;

Logger: homeassistant.components.websocket_api.http.connection
Source: components/sonos/helpers.py:81
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 7:44:06 AM (2 occurrences)
Last logged: 7:45:01 AM

[281472558628000] Error calling SonosSpeaker.unjoin on Breakfast Sonos: HTTPConnectionPool(host='192.168.86.84', port=1400): Read timed out. (read timeout=9.5)
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/lib/python3.9/http/client.py", line 1377, in getresponse
    response.begin()
  File "/usr/local/lib/python3.9/http/client.py", line 320, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.9/http/client.py", line 281, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/local/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.9/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 451, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 340, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='192.168.86.84', port=1400): Read timed out. (read timeout=9.5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sonos/helpers.py", line 67, in wrapper
    result = funct(self, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/sonos/speaker.py", line 883, in unjoin
    self.soco.unjoin()
  File "/usr/local/lib/python3.9/site-packages/soco/core.py", line 1759, in unjoin
    self.avTransport.BecomeCoordinatorOfStandaloneGroup([("InstanceID", 0)])
  File "/usr/local/lib/python3.9/site-packages/soco/services.py", line 207, in _dispatcher
    return self.send_command(action, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/soco/services.py", line 487, in send_command
    response = requests.post(
  File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 532, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='192.168.86.84', port=1400): Read timed out. (read timeout=9.5)

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 193, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1704, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1741, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 290, in service_handler
    await script_entity.async_turn_on(
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 398, in async_turn_on
    await coro
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 428, in _async_run
    return await self.script.async_run(script_vars, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1513, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 405, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 449, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 472, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 447, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 680, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1704, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1741, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 680, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 964, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 717, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 1024, in async_unjoin_player
    await self.hass.async_add_executor_job(self.unjoin_player)
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/sonos/media_player.py", line 767, in unjoin_player
    self.speaker.unjoin()
  File "/usr/src/homeassistant/homeassistant/components/sonos/helpers.py", line 81, in wrapper
    raise SonosUpdateError(message) from err
homeassistant.components.sonos.exception.SonosUpdateError: Error calling SonosSpeaker.unjoin on Breakfast Sonos: HTTPConnectionPool(host='192.168.86.84', port=1400): Read timed out. (read timeout=9.5)

Please help me understand what may be causing this error so that I may correct the problem.

It’s worth noting the Livingroom Sonos is a pair of speakers configured in stereo.

I have a similar issue and saw a different configuration posted online. Please note I have not tried this myself yet, as I am away from my HA server till this evening.

Have you tried removing “target:” and leaving “entity_id:” as is ( indented the same as “group_members:”)?

Hi, I am having this same issue… did it ever get resolved?

The YAML was built by the UI editor. Following your thought, I edited the underlying code, removing the ‘target:’ line. The script executes without an error. I’ll try that for a few days to see if the error returns.

Thank you.

Try the solution proposed by Stan. I made the change he suggested and the problem seems to have resolved itself.

Which patch version are you using?

2022.6.4 included a bug fix for issues encountered when joining/unjoining Sonos speakers using media_player.join and media_player.unjoin (which didn’t occur when using sonos.join and sonos.unjoin).

I was using 2022.6.4 when I reported the error. I’m now using 2022.6.6. I didn’t realize there was a fix. I’ll return my code to what I had prior then test again.

Thanks for letting me know.

If the problem persists, report it as an Issue in the Core repository on GitHub.

I am using Home Assistant Core 2022.6.7 and still encountered the same issues.

But I found a temporary solution:
The errors only occur when you try to group more then one additional Sonos Speaker.

If you only add the speakers one by one to the target speakers it works.

So I assume in the example above instead of

service: media_player.join
data:
  group_members:
    - media_player.kitchen_sonos
    - media_player.livingroom_sonos
target:
  entity_id: media_player.breakfast_sonos

do

service: media_player.join
data:
  group_members:
    - media_player.kitchen_sonos
target:
  entity_id: media_player.breakfast_sonos

service: media_player.join
data:
  group_members:
    - media_player.livingroom_sonos
target:
  entity_id: media_player.breakfast_sonos

EDIT:
I did that using the interface and had the idea as I had to do the same with Sonos_join there.
It was only possible to add speaker by speaker and not multiple speakers at once …

If joining multiple speakers still fails then the bug is unresolved and should be reported as an Issue in the Core repository on GitHub. Because joining speakers one by one, instead of several at once, means that media_player.join still doesn’t work as well as sonos.join.

If you’re on the current release and still encountering problems, please create an issue in the core Github repo and include any associated logs/tracebacks.

I’ve made some underlying changes to join/unjoin that will be in the next release (and beta which comes out today) if you’re willing to test the latest and greatest.

Hmm, not matter what I try, I can’t get the new script to work. I used the old one for about 2 years and don’t know what to do. Any ideas?

I have the same issue.

  alias: Sonos_all_rooms
  sequence:
  - data_template:
      entity_id:
      - media_player.sonos_move
      - media_player.lift
      - media_player.garage
      - media_player.office
    service: sonos.snapshot
  - data_template:
      entity_id:
      - media_player.sonos_move
      - media_player.lift
      - media_player.garage
      - media_player.office
    service: media_player.unjoin #sonos.unjoin
  - data:
      master: media_player.sonos_move
    entity_id: all
    service: media_player.join #sonos.join
  - data_template:
      entity_id:
      - media_player.sonos_move
      - media_player.lift
      - media_player.garage
      - media_player.office
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: media_player.sonos_move
      media_content_id: '{{ sonos_source }}'
      media_content_type: music
    service: media_player.play_media
  - delay: '{{ delay }}'
  - data_template:
      entity_id:
      - media_player.sonos_move
      - media_player.lift
      - media_player.garage
      - media_player.office
    service: sonos.restore
  - delay: '{{ delay }}'
  - data_template:
      entity_id:
      - media_player.sonos_move
      - media_player.lift
      - media_player.garage
      - media_player.office
    service: sonos.restore
  • Sonos_all_rooms: Error executing script. Invalid data for call_service at pos 3: extra keys not allowed @ data[‘master’]

Those changes are little annoying. :wink:
Any help welcome to get the door ring back working.

The problem is your script doesn’t use the media_player.join service call correctly.

The following error message is telling you that the service call contains an invalid option (master).

Invalid data for call_service at pos 3: extra keys not allowed @ data[‘master’]

The documentation for media_player.join indicates the options are entity_id, to represent the master speaker, and group_members to represent the speakers to be synced with entity_id. You’re using the service call exactly like the now-deprecated sonos.join and that’s incorrect.

Here’s how I suggest you use media_player.join.

      - service: media_player.join
        target:
          entity_id: media_player.sonos_move
        data:
          group_members:
            - media_player.sonos_move
            - media_player.lift
            - media_player.garage
            - media_player.office

If you’re interested, you can use locally defined script variables to make the script more concise. Be advised that the data_template option was deprecated a long time ago in favor of data.

  alias: Sonos_all_rooms
  sequence:
  - variables:
      speaker_master: media_player.sonos_move
      speaker_group:
        - media_player.sonos_move
        - media_player.lift
        - media_player.garage
        - media_player.office
  - service: sonos.snapshot
    target:
      entity_id: '{{ speaker_group }}'
  - service: media_player.unjoin
    target:
      entity_id: '{{ speaker_group }}'
  - service: media_player.join
    target:
      entity_id: '{{ speaker_master }}'
    data:
      group_members: '{{ speaker_group }}'
  - service: media_player.volume_set  
    target:
      entity_id: '{{ speaker_group }}'
    data:
      volume_level: '{{ volume }}'
  - service: media_player.play_media 
    target:
      entity_id: '{{ speaker_master }}'
    data:
      media_content_id: '{{ sonos_source }}'
      media_content_type: music
  - delay: '{{ delay }}'
  - service: sonos.restore
    target:
      entity_id: '{{ speaker_group }}'
1 Like

AWESOME - MANY MANY MANY THANKS!

Works perfectly! :smiley: