Skip to content

Commit

Permalink
Update Spotify state after mutation (home-assistant#129607)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored and balloob committed Nov 4, 2024
1 parent de453ab commit 6e98343
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
29 changes: 27 additions & 2 deletions homeassistant/components/spotify/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

from __future__ import annotations

from collections.abc import Callable
import asyncio
from collections.abc import Awaitable, Callable, Coroutine
import datetime as dt
import logging
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Concatenate

from spotifyaio import (
Device,
Expand Down Expand Up @@ -63,6 +64,7 @@
REPEAT_MODE_MAPPING_TO_SPOTIFY = {
value: key for key, value in REPEAT_MODE_MAPPING_TO_HA.items()
}
AFTER_REQUEST_SLEEP = 1


async def async_setup_entry(
Expand Down Expand Up @@ -93,6 +95,19 @@ def wrapper(self: SpotifyMediaPlayer) -> _R | None:
return wrapper


def async_refresh_after[_T: SpotifyEntity, **_P](
func: Callable[Concatenate[_T, _P], Awaitable[None]],
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]:
"""Define a wrapper to yield and refresh after."""

async def _async_wrap(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
await func(self, *args, **kwargs)
await asyncio.sleep(AFTER_REQUEST_SLEEP)
await self.coordinator.async_refresh()

return _async_wrap


class SpotifyMediaPlayer(SpotifyEntity, MediaPlayerEntity):
"""Representation of a Spotify controller."""

Expand Down Expand Up @@ -267,30 +282,37 @@ def repeat(self) -> RepeatMode | None:
return None
return REPEAT_MODE_MAPPING_TO_HA.get(self.currently_playing.repeat_mode)

@async_refresh_after
async def async_set_volume_level(self, volume: float) -> None:
"""Set the volume level."""
await self.coordinator.client.set_volume(int(volume * 100))

@async_refresh_after
async def async_media_play(self) -> None:
"""Start or resume playback."""
await self.coordinator.client.start_playback()

@async_refresh_after
async def async_media_pause(self) -> None:
"""Pause playback."""
await self.coordinator.client.pause_playback()

@async_refresh_after
async def async_media_previous_track(self) -> None:
"""Skip to previous track."""
await self.coordinator.client.previous_track()

@async_refresh_after
async def async_media_next_track(self) -> None:
"""Skip to next track."""
await self.coordinator.client.next_track()

@async_refresh_after
async def async_media_seek(self, position: float) -> None:
"""Send seek command."""
await self.coordinator.client.seek_track(int(position * 1000))

@async_refresh_after
async def async_play_media(
self, media_type: MediaType | str, media_id: str, **kwargs: Any
) -> None:
Expand Down Expand Up @@ -334,17 +356,20 @@ async def async_play_media(

await self.coordinator.client.start_playback(**kwargs)

@async_refresh_after
async def async_select_source(self, source: str) -> None:
"""Select playback device."""
for device in self.devices.data:
if device.name == source:
await self.coordinator.client.transfer_playback(device.device_id)
return

@async_refresh_after
async def async_set_shuffle(self, shuffle: bool) -> None:
"""Enable/Disable shuffle mode."""
await self.coordinator.client.set_shuffle(state=shuffle)

@async_refresh_after
async def async_set_repeat(self, repeat: RepeatMode) -> None:
"""Set repeat mode."""
if repeat not in REPEAT_MODE_MAPPING_TO_SPOTIFY:
Expand Down
7 changes: 7 additions & 0 deletions tests/components/spotify/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ async def setup_credentials(hass: HomeAssistant) -> None:
)


@pytest.fixture(autouse=True)
async def patch_sleep() -> Generator[AsyncMock]:
"""Fixture to setup credentials."""
with patch("homeassistant.components.spotify.media_player.AFTER_REQUEST_SLEEP", 0):
yield


@pytest.fixture
def mock_spotify() -> Generator[AsyncMock]:
"""Mock the Spotify API."""
Expand Down

0 comments on commit 6e98343

Please sign in to comment.