From 3cd0b6b9c1b27c3f60cf66ab4a3298f2d826a8e1 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Thu, 11 Feb 2021 23:56:07 +0100 Subject: [PATCH] few improvements --- music_assistant/constants.py | 2 +- music_assistant/helpers/compare.py | 19 ++++++++++--------- music_assistant/managers/players.py | 14 ++++++++++++-- music_assistant/models/player.py | 3 +++ .../providers/chromecast/__init__.py | 2 +- .../providers/chromecast/player.py | 15 +++++++++------ 6 files changed, 36 insertions(+), 19 deletions(-) diff --git a/music_assistant/constants.py b/music_assistant/constants.py index e0e2ac82..186735fd 100755 --- a/music_assistant/constants.py +++ b/music_assistant/constants.py @@ -1,6 +1,6 @@ """All constants for Music Assistant.""" -__version__ = "0.0.82" +__version__ = "0.0.83" REQUIRED_PYTHON_VER = "3.7" # configuration keys/attributes diff --git a/music_assistant/helpers/compare.py b/music_assistant/helpers/compare.py index fe3ae5a8..1e357437 100644 --- a/music_assistant/helpers/compare.py +++ b/music_assistant/helpers/compare.py @@ -37,12 +37,13 @@ def compare_version(left_version: str, right_version: str): def compare_artists(left_artists: List[Artist], right_artists: List[Artist]): - """Compare two lists of artist and return True if a match was found.""" + """Compare two lists of artist and return True if both lists match.""" + matches = 0 for left_artist in left_artists: for right_artist in right_artists: if compare_strings(left_artist.name, right_artist.name): - return True - return False + matches += 1 + return len(left_artists) == matches def compare_albums(left_albums: List[Album], right_albums: List[Album]): @@ -97,10 +98,10 @@ def compare_track(left_track: Track, right_track: Track): # album match OR near exact duration match left_albums = left_track.albums or [left_track.album] right_albums = right_track.albums or [right_track.album] - if not ( + if ( compare_albums(left_albums, right_albums) - or abs(left_track.duration - right_track.duration) <= 3 - ): - return False - # 100% match, all criteria passed - return True + and abs(left_track.duration - right_track.duration) < 3 + ) or abs(left_track.duration - right_track.duration) < 1: + # 100% match, all criteria passed + return True + return False diff --git a/music_assistant/managers/players.py b/music_assistant/managers/players.py index a9243ce2..bc6a7456 100755 --- a/music_assistant/managers/players.py +++ b/music_assistant/managers/players.py @@ -4,6 +4,7 @@ import logging from typing import List, Optional, Union from music_assistant.constants import ( + CONF_ENABLED, CONF_POWER_CONTROL, CONF_VOLUME_CONTROL, EVENT_PLAYER_ADDED, @@ -152,10 +153,19 @@ class PlayerManager: return if player.player_id in self._player_states: return await self.async_update_player(player) - # set the mass object on the 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 + return + # set the mass object on the player and call on_add function player.mass = self.mass + await player.async_on_add() # create playerstate and queue object - self._player_states[player.player_id] = PlayerState(self.mass, player) + player_state = PlayerState(self.mass, player) + self._player_states[player.player_id] = player_state + self._player_queues[player.player_id] = PlayerQueue(self.mass, player.player_id) # TODO: turn on player if it was previously turned on ? LOGGER.info( diff --git a/music_assistant/models/player.py b/music_assistant/models/player.py index 633b41f5..56c5c5e8 100755 --- a/music_assistant/models/player.py +++ b/music_assistant/models/player.py @@ -150,6 +150,9 @@ class Player: """Call when player is periodically polled by the player manager (should_poll=True).""" self.update_state() + async def async_on_add(self) -> None: + """Call when player is added to the player manager.""" + async def async_on_remove(self) -> None: """Call when player is removed from the player manager.""" diff --git a/music_assistant/providers/chromecast/__init__.py b/music_assistant/providers/chromecast/__init__.py index e0ec0751..f6efdc4e 100644 --- a/music_assistant/providers/chromecast/__init__.py +++ b/music_assistant/providers/chromecast/__init__.py @@ -93,7 +93,7 @@ class ChromecastProvider(PlayerProvider): player = self.mass.players.get_player(player_id) if not player: player = ChromecastPlayer(self.mass, cast_info) - # if player was already added, it player will take care of reconnects itself. + # if player was already added, the player will take care of reconnects itself. self.mass.add_job(player.async_set_cast_info, cast_info) self.mass.add_job(self.mass.players.async_add_player(player)) diff --git a/music_assistant/providers/chromecast/player.py b/music_assistant/providers/chromecast/player.py index a0e4949e..43748c29 100644 --- a/music_assistant/providers/chromecast/player.py +++ b/music_assistant/providers/chromecast/player.py @@ -192,9 +192,8 @@ class ChromecastPlayer(Player): """Return player specific config entries (if any).""" return PLAYER_CONFIG_ENTRIES - async def async_set_cast_info(self, cast_info: ChromecastInfo) -> None: - """Set the cast information and set up the chromecast object.""" - self._cast_info = cast_info + 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 @@ -208,9 +207,9 @@ class ChromecastPlayer(Player): pychromecast.get_chromecast_from_service, ( self.services, - cast_info.uuid, - cast_info.model_name, - cast_info.friendly_name, + self._cast_info.uuid, + self._cast_info.model_name, + self._cast_info.friendly_name, None, None, ), @@ -228,6 +227,10 @@ class ChromecastPlayer(Player): chromecast.mz_controller = mz_controller self._chromecast.start() + async def async_set_cast_info(self, cast_info: ChromecastInfo) -> None: + """Set (or update) the cast discovery info.""" + self._cast_info = cast_info + async def async_disconnect(self): """Disconnect Chromecast object if it is set.""" if self._chromecast is None: -- 2.34.1