Skip to content

Commit

Permalink
dynamic polling capped
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyanogenbot committed Sep 13, 2024
1 parent b9646d2 commit 1666f25
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion music_assistant/server/providers/bluesound/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def __init__(
self.client = BluosPlayer(self.ip_address, self.port, self.mass.http_session)
self.sync_status = SyncStatus
self.status = Status
self.poll_state = "Static"
self.dynamic_poll_count: int
self.mass_player: Player | None = None
self._listen_task: asyncio.Task | None = None

Expand All @@ -138,6 +140,9 @@ async def disconnect(self) -> None:

async def update_attributes(self) -> None:
"""Update the BluOS player attributes."""
if self.dynamic_poll_count > 0:
self.dynamic_poll_count = -1

self.sync_status = await self.client.sync_status()
self.status = await self.client.status()

Expand All @@ -158,7 +163,12 @@ async def update_attributes(self) -> None:
PLAYBACK_STATE_POLL_MAP[self.status.state],
self.mass_player.state,
)
if self.mass_player.state == PLAYBACK_STATE_POLL_MAP[self.status.state]:

if (
self.poll_state == "dynamic"
and self.mass_player.state == PLAYBACK_STATE_POLL_MAP[self.status.state]
or self.dynamic_poll_count == 0
):
self.mass_player.poll_interval = 30

if self.status.state == "stream":
Expand Down Expand Up @@ -322,6 +332,8 @@ async def cmd_stop(self, player_id: str) -> None:
if bluos_player := self.bluos_players[player_id]:
play_state = await bluos_player.client.stop(timeout=1)
if play_state == "stop":
self.poll_state = "dynamic"
self.dynamic_poll_count = 6
bluos_player.mass_player.poll_interval = 1
self.logger.debug("Set BluOS state to %s", play_state)
# Update media info then optimistically override playback state and source
Expand All @@ -331,6 +343,8 @@ async def cmd_play(self, player_id: str) -> None:
if bluos_player := self.bluos_players[player_id]:
play_state = await bluos_player.client.play(timeout=1)
if play_state == "stream":
self.poll_state = "dynamic"
self.dynamic_poll_count = 6
bluos_player.mass_player.poll_interval = 1
self.logger.debug("Set BluOS state to %s", play_state)
# Optimistic state, reduces interface lag
Expand All @@ -340,6 +354,8 @@ async def cmd_pause(self, player_id: str) -> None:
if bluos_player := self.bluos_players[player_id]:
play_state = await bluos_player.client.pause(timeout=1)
if play_state == "pause":
self.poll_state = "dynamic"
self.dynamic_poll_count = 6
bluos_player.mass_player.poll_interval = 1
self.logger.debug("Set BluOS state to %s", play_state)
# Optimistic state, reduces interface lag
Expand Down Expand Up @@ -371,7 +387,10 @@ async def play_media(
if bluos_player := self.bluos_players[player_id]:
self.mass.players.update(player_id)
play_state = await bluos_player.client.play_url(media.uri, timeout=1)
# Enable dynamic polling
if play_state == "stream":
self.poll_state = "dynamic"
self.dynamic_poll_count = 6
bluos_player.mass_player.poll_interval = 1
self.logger.debug("Set BluOS state to %s", play_state)

Expand Down

0 comments on commit 1666f25

Please sign in to comment.