Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use subscription callbacks to discover Sonos speakers #85411

Merged

Conversation

jjlawren
Copy link
Contributor

@jjlawren jjlawren commented Jan 8, 2023

Proposed change

Polling Sonos devices for the ZoneGroupState (which describes the device topology of the whole system) can fail with large installations of many speakers. It appears that this is a firmware limitation on the Sonos devices. This prevents the integration from starting once there are 20+ devices.

Subscription callbacks can be used instead to obtain the same information without errors. This changes the behavior of discovery to avoid polling and rely on subscriptions whenever possible.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Jan 8, 2023

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

Code owner commands

Code owners of sonos can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign sonos Removes the current integration label and assignees on the issue, add the integration domain after the command.

@bdraco
Copy link
Member

bdraco commented Jan 8, 2023

If I unplug/turn off the relay for a speaker and restart

2023-01-08 07:49:44.480 WARNING (SyncWorker_1) [homeassistant.components.sonos] Failed to add SonosSpeaker using <SoCo object at ip 192.168.106.180>
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
    raise err
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
    sock.connect(sa)
OSError: [Errno 113] Host is unreachable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/local/lib/python3.10/http/client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.10/http/client.py", line 1037, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.10/http/client.py", line 975, in send
    self.connect()
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f8fcfceba00>: Failed to establish a new connection: [Errno 113] Host is unreachable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 787, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.106.180', port=1400): Max retries exceeded with url: /xml/device_description.xml (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f8fcfceba00>: Failed to establish a new connection: [Errno 113] Host is unreachable'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sonos/__init__.py", line 302, in _add_speaker
    def _add_speaker(
  File "/usr/local/lib/python3.10/site-packages/soco/core.py", line 2018, in get_speaker_info
    response = requests.get(
  File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 565, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.106.180', port=1400): Max retries exceeded with url: /xml/device_description.xml (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f8fcfceba00>: Failed to establish a new connection: [Errno 113] Host is unreachable'))

That may be expected though

@jjlawren
Copy link
Contributor Author

jjlawren commented Jan 8, 2023

That may be expected though

Not exactly expected, but not caused by this PR. I forget why now, but I included exc_info=True in this log message a while back so it's pretty noisy with tracebacks. I plan on adjusting this in a followup.

jjlawren added a commit to jjlawren/sonos_custom_component that referenced this pull request Jan 8, 2023
@bdraco
Copy link
Member

bdraco commented Jan 8, 2023

Retesting this now after I've had the relay off all night....

@bdraco
Copy link
Member

bdraco commented Jan 8, 2023

The bootseq issue didn't reappear in testing this morning 👍

This looks good to go and the other trace issue is existing which you mentioned taking care of in a followup so LGTM

@bdraco bdraco merged commit 1b592e6 into home-assistant:dev Jan 8, 2023
@jjlawren jjlawren deleted the sonos/zgs-subscription-on-startup branch January 8, 2023 20:00
shbatm pushed a commit to shbatm/home-assistant-core that referenced this pull request Jan 9, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jan 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sonos : UPnP Error 501 and Large Sonos Installations
3 participants