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

Allow playback to be nullable #8

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/spotifyaio/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ async def _put(self, uri: str, data: dict[str, Any]) -> str:
"""Handle a PUT request to Spotify."""
return await self._request(METH_PUT, uri, data=data)

async def get_playback(self) -> PlaybackState:
async def get_playback(self) -> PlaybackState | None:
"""Get playback state."""
response = await self._get("v1/me/player")
if response == "":
return None
return PlaybackState.from_json(response)

async def transfer_playback(self, device_id: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/test_playback.ambr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# serializer version: 1
# name: test_get_devices
# name: test_get_playback_state
dict({
'context': dict({
'context_type': <ContextType.COLLECTION: 'collection'>,
Expand Down
23 changes: 22 additions & 1 deletion tests/test_playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .const import SPOTIFY_URL


async def test_get_devices(
async def test_get_playback_state(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
) -> None:
Expand All @@ -34,6 +34,27 @@ async def test_get_devices(
await spotify.close()


async def test_get_no_playback_state(
aresponses: ResponsesMockServer,
) -> None:
"""Test retrieving devices."""
aresponses.add(
SPOTIFY_URL,
"/v1/me/player",
METH_GET,
aresponses.Response(
status=204,
headers={"Content-Type": "application/json"},
),
)
async with aiohttp.ClientSession() as session:
spotify = SpotifyClient(session=session)
spotify.authenticate("test")
response = await spotify.get_playback()
assert response is None
await spotify.close()


async def test_transfer_playback(
aresponses: ResponsesMockServer,
) -> None:
Expand Down