async def async_add_player(self, player: Player) -> None:
"""Register a new player or update an existing one."""
- if not player or not player.available or self.mass.exit:
+ # guard for invalid data or exit in progress
+ if not player or self.mass.exit:
return
+ # redirect to update if player already exists
if player.player_id in self._player_states:
return await self.async_update_player(player)
- player_enabled = self.mass.config.get_player_config(player.player_id)[
- CONF_ENABLED
- ]
- if not player_enabled:
- # do not add the player to states if it's disabled/unavailable
+ # do not add the player to states if it's disabled/unavailable
+ if not self.mass.config.get_player_config(player.player_id)[CONF_ENABLED]:
return
# set the mass object on the player and call on_add function
player.mass = self.mass
self._cur_index = 0
self._cur_item_time = 0
self._last_item = None
- self._next_queue_startindex = 0
- self._last_queue_startindex = 0
+ self._queue_stream_start_index = 0
+ self._queue_stream_next_index = 0
self._last_player_state = PlaybackState.Stopped
# load previous queue settings from disk
self.mass.add_job(self.__async_restore_saved_state())
index = self.__index_by_id(index)
if not len(self.items) > index:
return
+ self._cur_index = index
+ self._queue_stream_next_index = index
if self.use_queue_stream:
- self._next_queue_startindex = index
queue_stream_uri = self.get_stream_url()
return await self.player.async_cmd_play_uri(queue_stream_uri)
if self.supports_queue:
# process new index
if self._cur_index != new_index:
# queue track updated
- self._next_queue_startindex = self.next_index
self._cur_index = new_index
# check if a new track is loaded, wait for the streamdetails
if (
{"queue_id": self.queue_id, "cur_item_time": track_time},
)
- async def async_start_queue_stream(self) -> None:
+ async def async_queue_stream_start(self) -> None:
"""Call when queue_streamer starts playing the queue stream."""
- self._last_queue_startindex = self._next_queue_startindex
self._cur_item_time = 0
- return self.get_item(self._next_queue_startindex)
+ self._cur_index = self._queue_stream_next_index
+ return self._queue_stream_next_index
+
+ async def async_queue_stream_next(self, cur_index: int) -> None:
+ """Call when queue_streamer loads next track in buffer."""
+ next_index = 0
+ if len(self.items) > (next_index):
+ next_index = cur_index + 1
+ elif self._repeat_enabled:
+ # repeat enabled, start queue at beginning
+ next_index = 0
+ self._queue_stream_next_index = next_index + 1
+ return next_index
def to_dict(self) -> dict:
"""Instance attributes as dict so it can be serialized to json."""
elapsed_time_queue = self.player.elapsed_time
total_time = 0
track_time = 0
- if self.items and len(self.items) > self._last_queue_startindex:
+ if self.items and len(self.items) > self._queue_stream_start_index:
queue_index = (
- self._last_queue_startindex
+ self._queue_stream_start_index
) # holds the last starting position
queue_track = None
while len(self.items) > queue_index:
self._shuffle_enabled = cache_data["shuffle_enabled"]
self._repeat_enabled = cache_data["repeat_enabled"]
self._items = cache_data["items"]
- self._cur_index = cache_data["cur_item"]
- self._next_queue_startindex = cache_data["next_queue_index"]
+ self._cur_index = cache_data.get("cur_index", 0)
+ self._queue_stream_next_index = self._cur_index
# pylint: enable=unused-argument
"shuffle_enabled": self._shuffle_enabled,
"repeat_enabled": self._repeat_enabled,
"items": self._items,
- "cur_item": self._cur_index,
- "next_queue_index": self._next_queue_startindex,
+ "cur_index": self._cur_index,
}
await self.mass.cache.async_set(cache_str, cache_data)
LOGGER.info("queue state saved to file for player %s", self.queue_id)
"""ChromeCast playerprovider."""
import logging
-import uuid
from typing import List
import pychromecast
def __chromecast_add_update_callback(self, cast_uuid, cast_service_name):
"""Handle zeroconf discovery of a new or updated chromecast."""
# pylint: disable=unused-argument
- if uuid is None:
- return # Discovered chromecast without uuid
+ if cast_uuid is None:
+ return # Discovered chromecast without uuid?
service = self._listener.services[cast_uuid]
cast_info = ChromecastInfo(
if not player:
player = ChromecastPlayer(self.mass, cast_info)
# if player was already added, the player will take care of reconnects itself.
- self.mass.add_job(player.async_set_cast_info, cast_info)
+ player.set_cast_info(cast_info)
self.mass.add_job(self.mass.players.async_add_player(player))
@staticmethod
async def async_on_add(self) -> None:
"""Call when player is added to the player manager."""
- # Only setup the chromecast once, changes will automatically be picked up.
- if self._chromecast is not None:
- return
- LOGGER.debug(
- "[%s] Connecting to cast device by service %s",
- self._cast_info.friendly_name,
- self.services,
- )
chromecast = await self.mass.loop.run_in_executor(
None,
pychromecast.get_chromecast_from_service,
chromecast.mz_controller = mz_controller
self._chromecast.start()
- async def async_set_cast_info(self, cast_info: ChromecastInfo) -> None:
+ def set_cast_info(self, cast_info: ChromecastInfo) -> None:
"""Set (or update) the cast discovery info."""
self._cast_info = cast_info