Skip to content

Playback State improperly loaded in limited api access condition #417

@fcusson

Description

@fcusson

Problem/Motivation

Currently, if the Spotify API is none responsive (possible for rate limits or Developper App request ceiling) The spotifyaio library still tries to load the playback from JSON

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 382, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/spotify/coordinator.py", line 79, in _async_update_data
    current = await self.client.get_playback()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/deps/spotifyaio/spotify.py", line 315, in get_playback
    return PlaybackState.from_json(response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 8, in __mashumaro_from_json__
mashumaro.exceptions.MissingField: Field "context" of type Optional[Context] is missing in PlaybackState instance

This is the current response of the playback state in 429 state for the user

"{\n  \"error\": {\n    \"status\": 429,\n    \"message\": \"API rate limit exceeded\"\n  }\n}"

Expected behavior

Spotify AIO should gracefully handle issues related to rate limit

Actual behavior

Spotify AIO fails by being unable to load playback state due to an unexpected error

Steps to reproduce

Saturate API calls to get a 429 error

Proposed changes

The reason behind the error is speculative and could be something different, but seems the most likely explaination. If this is the case, we should check the status of any response and handle error in case of playback rate limit. In case of an unexpected playback format, assume an error from the API and mark the playback unavailable instead of sending a traceback.

From spotcast side, I will add the recommandation to create a separate API app to handle the spotcast requests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions