Skip to content

Commit

Permalink
fix playerstate
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Sep 18, 2020
1 parent 05f728b commit 0ba092b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
11 changes: 8 additions & 3 deletions music_assistant/models/player_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def __init__(self, media_item=None):
class PlayerQueue:
"""Class that holds the queue items for a player."""

def __init__(self, mass, player_id):
def __init__(self, mass, player):
"""Initialize class."""
self.mass = mass
self.player_id = player_id
self._player = player
self._items = []
self._shuffle_enabled = False
self._repeat_enabled = False
Expand All @@ -78,7 +78,12 @@ async def async_close(self):
@property
def player(self):
"""Return handle to player."""
return self.mass.player_manager.get_player(self.player_id)
return self._player

@property
def player_id(self):
"""Return handle to player."""
return self._player.player_id

@property
def shuffle_enabled(self):
Expand Down
11 changes: 5 additions & 6 deletions music_assistant/player_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,14 @@ def get_player_controls(

async def async_add_player(self, player: Player) -> None:
"""Register a new player or update an existing one."""
if not player:
if not player or not player.available:
return
is_new_player = player.player_id not in self._players
await self.__async_create_player_state(player)
if is_new_player:
# create player queue
if player.player_id not in self._player_queues:
self._player_queues[player.player_id] = PlayerQueue(
self.mass, player.player_id
)
self._player_queues[player.player_id] = PlayerQueue(self.mass, player)
# TODO: turn on player if it was previously turned on ?
LOGGER.info(
"New player added: %s/%s",
Expand All @@ -161,6 +159,7 @@ async def async_add_player(self, player: Player) -> None:
async def async_remove_player(self, player_id: str):
"""Remove a player from the registry."""
self._players.pop(player_id, None)
self._player_queues.pop(player_id, None)
self._org_players.pop(player_id, None)
LOGGER.info("Player removed: %s", player_id)
self.mass.signal_event(EVENT_PLAYER_REMOVED, {"player_id": player_id})
Expand Down Expand Up @@ -703,9 +702,9 @@ def __get_player_state(self, player: Player, active_parent: str, powered: bool):
if powered and active_parent != player.player_id:
# use group state
return self._players[active_parent].state
if player.state == PlayerState.Stopped and not powered:
if PlayerState(player.state) == PlayerState.Stopped and not powered:
return PlayerState.Off
return player.state
return PlayerState(player.state)

@callback
@classmethod
Expand Down
23 changes: 8 additions & 15 deletions music_assistant/providers/squeezebox/socket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from enum import Enum
from typing import Awaitable

from music_assistant.models.player import PlayerState
from music_assistant.utils import callback, run_periodic

from .constants import PROV_ID
Expand All @@ -30,14 +31,6 @@
}


class State(str, Enum):
"""Enum for the playstate of a squeezebox player."""

Stopped = "stopped"
Paused = "paused"
Playing = "playing"


class Event(Enum):
"""Enum with the various events the socket client fires."""

Expand Down Expand Up @@ -70,7 +63,7 @@ def __init__(
self._volume_level = 0
self._powered = False
self._muted = False
self._state = State.Stopped
self._state = PlayerState.Stopped
self._elapsed_time = 0
self._current_uri = ""
self._tasks = [
Expand Down Expand Up @@ -356,7 +349,7 @@ def _process_stat_stmf(self, data):
"""Process incoming stat STMf message (connection closed)."""
# pylint: disable=unused-argument
LOGGER.debug("STMf received - connection closed.")
self._state = State.Stopped
self._state = PlayerState.Stopped
asyncio.create_task(self._event_callback(Event.EVENT_UPDATED, self))

@callback
Expand All @@ -376,23 +369,23 @@ def _process_stat_stmp(self, data):
"""Process incoming stat STMp message: Pause confirmed."""
# pylint: disable=unused-argument
LOGGER.debug("STMp received - pause confirmed.")
self._state = State.Paused
self._state = PlayerState.Paused
asyncio.create_task(self._event_callback(Event.EVENT_UPDATED, self))

@callback
def _process_stat_stmr(self, data):
"""Process incoming stat STMr message: Resume confirmed."""
# pylint: disable=unused-argument
LOGGER.debug("STMr received - resume confirmed.")
self._state = State.Playing
self._state = PlayerState.Playing
asyncio.create_task(self._event_callback(Event.EVENT_UPDATED, self))

@callback
def _process_stat_stms(self, data):
# pylint: disable=unused-argument
"""Process incoming stat STMs message: Playback of new track has started."""
LOGGER.debug("STMs received - playback of new track has started.")
self._state = State.Playing
self._state = PlayerState.Playing
asyncio.create_task(self._event_callback(Event.EVENT_UPDATED, self))

@callback
Expand All @@ -419,7 +412,7 @@ def _process_stat_stmt(self, data):
server_timestamp,
error_code,
) = struct.unpack("!BBBLLLLHLLLLHLLH", data)
if self._state == State.Playing and elapsed_seconds != self._elapsed_time:
if self._state == PlayerState.Playing and elapsed_seconds != self._elapsed_time:
self._elapsed_time = elapsed_seconds
asyncio.create_task(self._event_callback(Event.EVENT_UPDATED, self))

Expand All @@ -428,7 +421,7 @@ def _process_stat_stmu(self, data):
"""Process incoming stat STMu message: Buffer underrun: Normal end of playback."""
# pylint: disable=unused-argument
LOGGER.debug("STMu received - end of playback.")
self.state = State.Stopped
self.state = PlayerState.Stopped
asyncio.create_task(self._event_callback(Event.EVENT_UPDATED, self))

@callback
Expand Down

0 comments on commit 0ba092b

Please sign in to comment.