remove auto setting from music display to simplify polling

This commit is contained in:
ChuckBuilds
2025-05-25 18:55:39 -05:00
parent e5aeb579f0
commit 5d72ea620e

View File

@@ -38,7 +38,7 @@ class MusicManager:
self.update_callback = update_callback self.update_callback = update_callback
self.polling_interval = 2 # Default self.polling_interval = 2 # Default
self.enabled = False # Default self.enabled = False # Default
self.preferred_source = "auto" # Default self.preferred_source = "spotify" # Default changed from "auto"
self.stop_event = threading.Event() self.stop_event = threading.Event()
self.track_info_lock = threading.Lock() # Added lock self.track_info_lock = threading.Lock() # Added lock
@@ -57,7 +57,7 @@ class MusicManager:
def _load_config(self): def _load_config(self):
default_interval = 2 default_interval = 2
default_preferred_source = "auto" # default_preferred_source = "auto" # Removed
self.enabled = False # Assume disabled until config proves otherwise self.enabled = False # Assume disabled until config proves otherwise
if not os.path.exists(CONFIG_PATH): if not os.path.exists(CONFIG_PATH):
@@ -70,14 +70,20 @@ class MusicManager:
music_config = config_data.get("music", {}) music_config = config_data.get("music", {})
self.enabled = music_config.get("enabled", False) self.enabled = music_config.get("enabled", False)
self.polling_interval = music_config.get("POLLING_INTERVAL_SECONDS", default_interval)
self.preferred_source = music_config.get("preferred_source", default_preferred_source).lower()
if not self.enabled: if not self.enabled:
logging.info("Music manager is disabled in config.json.") logging.info("Music manager is disabled in config.json (top level 'enabled': false).")
return # Don't proceed further if disabled return # Don't proceed further if disabled
logging.info(f"Music manager enabled. Polling interval: {self.polling_interval}s. Preferred source: {self.preferred_source}") self.polling_interval = music_config.get("POLLING_INTERVAL_SECONDS", default_interval)
configured_source = music_config.get("preferred_source", "spotify").lower()
if configured_source in ["spotify", "ytm"]:
self.preferred_source = configured_source
logging.info(f"Music manager enabled. Polling interval: {self.polling_interval}s. Preferred source: {self.preferred_source}")
else:
logging.warning(f"Invalid 'preferred_source' ('{configured_source}') in config.json. Must be 'spotify' or 'ytm'. Music manager disabled.")
self.enabled = False
return
except json.JSONDecodeError: except json.JSONDecodeError:
logging.error(f"Error decoding JSON from {CONFIG_PATH}. Music manager disabled.") logging.error(f"Error decoding JSON from {CONFIG_PATH}. Music manager disabled.")
@@ -96,40 +102,34 @@ class MusicManager:
logging.info("Initializing music clients...") logging.info("Initializing music clients...")
# Initialize Spotify Client if needed # Initialize Spotify Client if needed
if self.preferred_source in ["auto", "spotify"]: if self.preferred_source == "spotify":
try: try:
self.spotify = SpotifyClient() self.spotify = SpotifyClient()
if not self.spotify.is_authenticated(): if not self.spotify.is_authenticated():
logging.warning("Spotify client initialized but not authenticated. Please run src/authenticate_spotify.py if you want to use Spotify.") logging.warning("Spotify client initialized but not authenticated. Please run src/authenticate_spotify.py if you want to use Spotify.")
# The SpotifyClient will log more details if cache loading failed.
# No need to attempt auth URL generation here.
else: else:
logging.info("Spotify client authenticated.") logging.info("Spotify client authenticated.")
except Exception as e: except Exception as e:
logging.error(f"Failed to initialize Spotify client: {e}") logging.error(f"Failed to initialize Spotify client: {e}")
self.spotify = None self.spotify = None
else: else:
logging.info("Spotify client initialization skipped due to preferred_source setting.") self.spotify = None # Ensure it's None if not preferred
self.spotify = None
# Initialize YTM Client if needed # Initialize YTM Client if needed
if self.preferred_source in ["auto", "ytm"]: if self.preferred_source == "ytm":
try: try:
self.ytm = YTMClient(update_callback=self._handle_ytm_direct_update) self.ytm = YTMClient(update_callback=self._handle_ytm_direct_update)
# We no longer check is_available() or connect here. Connection is on-demand.
logging.info(f"YTMClient initialized. Connection will be managed on-demand. Configured URL: {self.ytm.base_url}") logging.info(f"YTMClient initialized. Connection will be managed on-demand. Configured URL: {self.ytm.base_url}")
except Exception as e: except Exception as e:
logging.error(f"Failed to initialize YTM client: {e}") logging.error(f"Failed to initialize YTM client: {e}")
self.ytm = None self.ytm = None
else: else:
logging.info("YTM client initialization skipped due to preferred_source setting.") self.ytm = None # Ensure it's None if not preferred
self.ytm = None
def activate_music_display(self): def activate_music_display(self):
logger.info("Music display activated.") logger.info("Music display activated.")
self.is_music_display_active = True self.is_music_display_active = True
if self.ytm and self.preferred_source in ["auto", "ytm"]: if self.ytm and self.preferred_source == "ytm": # Only connect YTM if it's the preferred source
if not self.ytm.is_connected: if not self.ytm.is_connected:
logger.info("Attempting to connect YTM client due to music display activation.") logger.info("Attempting to connect YTM client due to music display activation.")
# Pass a reasonable timeout for on-demand connection # Pass a reasonable timeout for on-demand connection
@@ -155,18 +155,14 @@ class MusicManager:
logger.debug("Skipping YTM direct update: Manager disabled or music display not active.") logger.debug("Skipping YTM direct update: Manager disabled or music display not active.")
return return
# Only process if YTM is the preferred source, or if auto and Spotify isn't actively playing. # Only process if YTM is the preferred source
spotify_is_playing_flag = False if self.preferred_source != "ytm":
with self.track_info_lock: logger.debug(f"Skipping YTM direct update: Preferred source is '{self.preferred_source}', not 'ytm'.")
if self.current_source == MusicSource.SPOTIFY and self.current_track_info and self.current_track_info.get('is_playing'):
spotify_is_playing_flag = True
if not (self.preferred_source == "ytm" or (self.preferred_source == "auto" and not spotify_is_playing_flag)):
logger.debug("Skipping YTM direct update due to preferred_source/Spotify state.")
return return
player_info = ytm_data.get('player', {}) ytm_player_info = ytm_data.get('player', {}) if ytm_data else {}
is_actually_playing_ytm = (player_info.get('trackState') == 1) and not player_info.get('adPlaying', False) is_actually_playing_ytm = (ytm_player_info.get('trackState') == 1) and \
not ytm_player_info.get('adPlaying', False)
simplified_info = self.get_simplified_track_info(ytm_data if is_actually_playing_ytm else None, simplified_info = self.get_simplified_track_info(ytm_data if is_actually_playing_ytm else None,
MusicSource.YTM if is_actually_playing_ytm else MusicSource.NONE) MusicSource.YTM if is_actually_playing_ytm else MusicSource.NONE)
@@ -251,10 +247,7 @@ class MusicManager:
polled_source = MusicSource.NONE polled_source = MusicSource.NONE
is_playing_from_poll = False # Renamed to avoid conflict is_playing_from_poll = False # Renamed to avoid conflict
poll_spotify = self.preferred_source in ["auto", "spotify"] and self.spotify and self.spotify.is_authenticated() if self.preferred_source == "spotify" and self.spotify and self.spotify.is_authenticated():
poll_ytm = self.preferred_source in ["auto", "ytm"] and self.ytm
if poll_spotify:
try: try:
spotify_track = self.spotify.get_current_track() spotify_track = self.spotify.get_current_track()
if spotify_track and spotify_track.get('is_playing'): if spotify_track and spotify_track.get('is_playing'):
@@ -269,25 +262,29 @@ class MusicManager:
if "token" in str(e).lower(): if "token" in str(e).lower():
logging.warning("Spotify auth token issue detected during polling.") logging.warning("Spotify auth token issue detected during polling.")
should_poll_ytm_now = poll_ytm and (self.preferred_source == "ytm" or (self.preferred_source == "auto" and not is_playing_from_poll)) elif self.preferred_source == "ytm" and self.ytm and self.ytm.is_connected:
try:
if should_poll_ytm_now: ytm_track_data = self.ytm.get_current_track() # Data from YTMClient's cache
if self.ytm and self.ytm.is_connected: if ytm_track_data and ytm_track_data.get('player') and \
try: not ytm_track_data.get('player', {}).get('isPaused') and \
ytm_track_data = self.ytm.get_current_track() # Data from YTMClient's cache not ytm_track_data.get('player',{}).get('adPlaying', False):
if ytm_track_data and ytm_track_data.get('player') and not ytm_track_data.get('player', {}).get('isPaused') and not ytm_track_data.get('player',{}).get('adPlaying', False): polled_track_info_data = ytm_track_data
if self.preferred_source == "ytm" or not is_playing_from_poll: polled_source = MusicSource.YTM
polled_track_info_data = ytm_track_data is_playing_from_poll = True # YTM is now considered playing
polled_source = MusicSource.YTM logger.debug(f"Polling YTM: Active track - {ytm_track_data.get('track', {}).get('title')}")
is_playing_from_poll = True # YTM is now considered playing else:
logger.debug(f"Polling YTM: Active track - {ytm_track_data.get('track', {}).get('title')}") logging.debug("Polling YTM: No active track or player paused (or track data missing player info).")
else: except Exception as e:
logging.debug("Polling YTM: No active track or player paused (or track data missing player info).") logging.error(f"Error polling YTM: {e}")
except Exception as e: elif self.preferred_source == "ytm" and self.ytm and not self.ytm.is_connected:
logging.error(f"Error polling YTM: {e}") logging.debug("Skipping YTM poll: Client not connected. Will attempt reconnect on next cycle if display active.")
else: # Attempt to reconnect YTM if music display is active and it's the preferred source
logging.debug("Skipping YTM poll: Client not initialized or not connected.") if self.is_music_display_active:
# Consider setting self.ytm = None if it becomes unavailable repeatedly? logger.info("YTM is preferred and display active, attempting reconnect during poll cycle.")
if self.ytm.connect_client(timeout=5):
logger.info("YTM reconnected during poll cycle.")
else:
logger.warning("YTM failed to reconnect during poll cycle.")
simplified_info_poll = self.get_simplified_track_info(polled_track_info_data, polled_source) simplified_info_poll = self.get_simplified_track_info(polled_track_info_data, polled_source)