Skip to content

Commit

Permalink
Better handling of unavailable players
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Oct 24, 2024
1 parent 7211fae commit b863e2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions music_assistant/server/controllers/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,10 @@ def update(
if skip_forward and not force_update:
return

# handle player becoming unavailable
if "available" in changed_values and not player.available:
self._handle_player_unavailable(player)

# update/signal group player(s) child's when group updates
for child_player in self.iter_group_members(player, exclude_self=True):
self.update(child_player.player_id, skip_forward=True)
Expand Down Expand Up @@ -1072,6 +1076,27 @@ def _get_group_volume_level(self, player: Player) -> int:
group_volume = group_volume / active_players
return int(group_volume)

def _handle_player_unavailable(self, player: Player) -> None:
"""Handle a player becoming unavailable."""
if player.synced_to:
self.mass.create_task(self.cmd_unsync(player.player_id))
# also set this optimistically because the above command will most likely fail
player.synced_to = None
return
for group_child_id in player.group_childs:
if group_child_id == player.player_id:
continue
if child_player := self.get(group_child_id):
self.mass.create_task(self.cmd_power(group_child_id, False, True))
# also set this optimistically because the above command will most likely fail
child_player.synced_to = None
player.group_childs = set()
if player.active_group and (group_player := self.get(player.active_group)):
# remove player from group if its part of a group
group_player = self.get(player.active_group)
if player.player_id in group_player.group_childs:
group_player.group_childs.remove(player.player_id)

async def _play_announcement(
self,
player: Player,
Expand Down
9 changes: 9 additions & 0 deletions music_assistant/server/providers/player_group/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ async def poll_player(self, player_id: str) -> None:
if 'needs_poll' is set to True in the player object.
"""
if group_player := self.mass.players.get(player_id):
# dynamically change the poll interval
if group_player.state == PlayerState.PLAYING:
group_player.poll_interval = 5
elif group_player.powered:
group_player.poll_interval = 20
else:
group_player.poll_interval = 60
self._update_attributes(group_player)

async def create_group(self, group_type: str, name: str, members: list[str]) -> Player:
Expand Down Expand Up @@ -682,6 +689,8 @@ async def _register_group_player(
supported_features=tuple(player_features),
group_childs=set(members),
active_source=group_player_id,
needs_poll=True,
poll_interval=5,
)

await self.mass.players.register_or_update(player)
Expand Down

0 comments on commit b863e2e

Please sign in to comment.