Skip to content

Commit

Permalink
Add PlayState enum
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsayre committed Jan 3, 2025
1 parent 5f6a2a1 commit ba26d59
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
6 changes: 2 additions & 4 deletions pyheos/command/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_players() -> HeosCommand:
return HeosCommand(const.COMMAND_GET_PLAYERS)

@staticmethod
def get_player_state(player_id: int) -> HeosCommand:
def get_play_state(player_id: int) -> HeosCommand:
"""Get the state of the player.
References:
Expand All @@ -42,13 +42,11 @@ def get_player_state(player_id: int) -> HeosCommand:
)

@staticmethod
def set_player_state(player_id: int, state: str) -> HeosCommand:
def set_play_state(player_id: int, state: const.PlayState) -> HeosCommand:
"""Set the state of the player.
References:
4.2.4 Set Play State"""
if state not in const.VALID_PLAY_STATES:
raise ValueError("Invalid play state: " + state)
return HeosCommand(
const.COMMAND_SET_PLAY_STATE,
{const.ATTR_PLAYER_ID: player_id, const.ATTR_STATE: state},
Expand Down
11 changes: 7 additions & 4 deletions pyheos/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@
DATA_NEW: Final = "new"
DATA_MAPPED_IDS: Final = "mapped_ids"

PLAY_STATE_PLAY: Final = "play"
PLAY_STATE_PAUSE: Final = "pause"
PLAY_STATE_STOP: Final = "stop"
VALID_PLAY_STATES: Final = (PLAY_STATE_PLAY, PLAY_STATE_PAUSE, PLAY_STATE_STOP)

class PlayState(StrEnum):
"""Define the play states."""

PLAY = "play"
PAUSE = "pause"
STOP = "stop"


class RepeatType(StrEnum):
Expand Down
14 changes: 7 additions & 7 deletions pyheos/heos.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,24 +387,24 @@ async def load_players(self) -> dict[str, list | dict]:
const.DATA_MAPPED_IDS: mapped_player_ids,
}

async def player_get_state(self, player_id: int) -> str:
async def player_get_play_state(self, player_id: int) -> const.PlayState:
"""Get the state of the player.
References:
4.2.3 Get Play State"""
response = await self._connection.command(
PlayerCommands.get_player_state(player_id)
PlayerCommands.get_play_state(player_id)
)
return response.get_message_value(const.ATTR_STATE)
return const.PlayState(response.get_message_value(const.ATTR_STATE))

async def player_set_state(self, player_id: int, state: str) -> None:
async def player_set_play_state(
self, player_id: int, state: const.PlayState
) -> None:
"""Set the state of the player.
References:
4.2.4 Set Play State"""
await self._connection.command(
PlayerCommands.set_player_state(player_id, state)
)
await self._connection.command(PlayerCommands.set_play_state(player_id, state))

async def get_now_playing_media(
self, player_id: int, update: HeosNowPlayingMedia | None = None
Expand Down
18 changes: 9 additions & 9 deletions pyheos/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __init__(self, heos: "Heos", data: dict[str, Any]) -> None:
self._network: str = data[const.ATTR_NETWORK]
self._line_out: int = data[const.ATTR_LINE_OUT]

self._state: str | None = None
self._state: const.PlayState | None = None
self._volume: int | None = None
self._is_muted: bool | None = None
self._repeat: const.RepeatType = const.RepeatType.OFF
Expand Down Expand Up @@ -185,7 +185,7 @@ async def refresh(self) -> None:

async def refresh_state(self) -> None:
"""Refresh the now playing state."""
self._state = await self.heos.player_get_state(self._player_id)
self._state = await self.heos.player_get_play_state(self._player_id)

async def refresh_now_playing_media(self) -> None:
"""Pull the latest now playing media."""
Expand All @@ -205,21 +205,21 @@ async def refresh_play_mode(self) -> None:
self._repeat = play_mode.repeat
self._shuffle = play_mode.shuffle

async def set_state(self, state: str) -> None:
async def set_state(self, state: const.PlayState) -> None:
"""Set the state of the player."""
await self.heos.player_set_state(self._player_id, state)
await self.heos.player_set_play_state(self._player_id, state)

async def play(self) -> None:
"""Set the start to play."""
await self.set_state(const.PLAY_STATE_PLAY)
await self.set_state(const.PlayState.PLAY)

async def pause(self) -> None:
"""Set the start to pause."""
await self.set_state(const.PLAY_STATE_PAUSE)
await self.set_state(const.PlayState.PAUSE)

async def stop(self) -> None:
"""Set the start to stop."""
await self.set_state(const.PLAY_STATE_STOP)
await self.set_state(const.PlayState.STOP)

async def set_volume(self, level: int) -> None:
"""Set the volume level."""
Expand Down Expand Up @@ -323,8 +323,8 @@ async def event_update(self, event: HeosMessage, all_progress_events: bool) -> b
event, all_progress_events
)
if event.command == const.EVENT_PLAYER_STATE_CHANGED:
self._state = event.get_message_value(const.ATTR_STATE)
if self._state == const.PLAY_STATE_PLAY:
self._state = const.PlayState(event.get_message_value(const.ATTR_STATE))
if self._state == const.PlayState.PLAY:
self._now_playing_media.clear_progress()
elif event.command == const.EVENT_PLAYER_NOW_PLAYING_CHANGED:
await self.refresh_now_playing_media()
Expand Down
10 changes: 5 additions & 5 deletions tests/test_heos.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ async def test_get_players(heos: Heos) -> None:
assert player.line_out == 1
assert player.model == "HEOS Drive"
assert player.network == const.NETWORK_TYPE_WIRED
assert player.state == const.PLAY_STATE_STOP
assert player.state == const.PlayState.STOP
assert player.version == "1.493.180"
assert player.volume == 36
assert not player.is_muted
Expand All @@ -371,7 +371,7 @@ async def test_player_state_changed_event(
# assert not playing
await heos.get_players()
player = heos.players[1]
assert player.state == const.PLAY_STATE_STOP
assert player.state == const.PlayState.STOP

# Attach dispatch handler
signal = asyncio.Event()
Expand All @@ -386,14 +386,14 @@ async def handler(player_id: int, event: str) -> None:
# Write event through mock device
await mock_device.write_event(
"event.player_state_changed",
{"player_id": player.player_id, "state": const.PLAY_STATE_PLAY},
{"player_id": player.player_id, "state": const.PlayState.PLAY},
)

# Wait until the signal
await signal.wait()
# Assert state changed
assert player.state == const.PLAY_STATE_PLAY
assert heos.players[2].state == const.PLAY_STATE_STOP
assert player.state == const.PlayState.PLAY
assert heos.players[2].state == const.PlayState.STOP


@calls_player_commands()
Expand Down
10 changes: 5 additions & 5 deletions tests/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ async def test_str() -> None:


@pytest.mark.parametrize(
"state", (const.PLAY_STATE_PAUSE, const.PLAY_STATE_PLAY, const.PLAY_STATE_STOP)
"state", (const.PlayState.PAUSE, const.PlayState.PLAY, const.PlayState.STOP)
)
@calls_command(
"player.set_play_state",
{const.ATTR_PLAYER_ID: 1, const.ATTR_STATE: value(arg_name="state")},
)
async def test_set_state(player: HeosPlayer, state: str) -> None:
async def test_set_state(player: HeosPlayer, state: const.PlayState) -> None:
"""Test the play, pause, and stop commands."""
await player.set_state(state)


@calls_command(
"player.set_play_state",
{const.ATTR_PLAYER_ID: 1, const.ATTR_STATE: const.PLAY_STATE_PLAY},
{const.ATTR_PLAYER_ID: 1, const.ATTR_STATE: const.PlayState.PLAY},
)
async def test_set_play(player: HeosPlayer) -> None:
"""Test the pause commands."""
Expand All @@ -52,7 +52,7 @@ async def test_set_play(player: HeosPlayer) -> None:

@calls_command(
"player.set_play_state",
{const.ATTR_PLAYER_ID: 1, const.ATTR_STATE: const.PLAY_STATE_PAUSE},
{const.ATTR_PLAYER_ID: 1, const.ATTR_STATE: const.PlayState.PAUSE},
)
async def test_set_pause(player: HeosPlayer) -> None:
"""Test the play commands."""
Expand All @@ -61,7 +61,7 @@ async def test_set_pause(player: HeosPlayer) -> None:

@calls_command(
"player.set_play_state",
{const.ATTR_PLAYER_ID: 1, const.ATTR_STATE: const.PLAY_STATE_STOP},
{const.ATTR_PLAYER_ID: 1, const.ATTR_STATE: const.PlayState.STOP},
)
async def test_set_stop(player: HeosPlayer) -> None:
"""Test the stop commands."""
Expand Down

0 comments on commit ba26d59

Please sign in to comment.