Fix several issues in Browse feature (#439)
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Mon, 25 Jul 2022 19:47:52 +0000 (21:47 +0200)
committerGitHub <noreply@github.com>
Mon, 25 Jul 2022 19:47:52 +0000 (21:47 +0200)
- fix sort order of items in browse
- fix parsing item that has not (yet_ been added to the db
- fix relative file paths within playlist

music_assistant/models/player_queue.py
music_assistant/music_providers/filesystem.py

index ff5ae60daeb52c7d968f757ff10e0f469d5bc9e0..8fea1673dee5e72da8e4b2674e62096991be54b6 100644 (file)
@@ -687,9 +687,8 @@ class PlayerQueue:
                 return
 
             # queue exhausted or player turned off/stopped
-            if (
-                self.stream
-                and new_state[0] in (PlayerState.IDLE, PlayerState.OFF)
+            if self.stream and (
+                new_state[0] in (PlayerState.IDLE, PlayerState.OFF)
                 or not self.player.available
             ):
                 self.stream.signal_next = None
index 590cf78299d5a83353025ccf6a8f76f9bdfb5add..72580d923f61fd6fe8efd343161e2c70fdf2d9d4 100644 (file)
@@ -180,12 +180,27 @@ class FileSystemProvider(MusicProvider):
             _, ext = filename.rsplit(".", 1)
 
             if ext in TRACK_EXTENSIONS:
-                if track := await self._parse_track(full_path):
-                    subitems.append(track)
+                item_id = self._get_item_id(full_path)
+                if db_item := await self.mass.music.tracks.get_db_item_by_prov_id(
+                    item_id, provider_id=self.id
+                ):
+                    subitems.append(db_item)
+                elif track := await self._parse_track(full_path):
+                    # make sure that the item exists
+                    # https://github.com/music-assistant/hass-music-assistant/issues/707
+                    db_item = await self.mass.music.tracks.add_db_item(track)
+                    subitems.append(db_item)
                 continue
             if ext in PLAYLIST_EXTENSIONS:
-                if playlist := await self._parse_playlist(full_path):
-                    subitems.append(playlist)
+                if db_item := await self.mass.music.playlists.get_db_item_by_prov_id(
+                    item_id, provider_id=self.id
+                ):
+                    subitems.append(db_item)
+                elif playlist := await self._parse_playlist(full_path):
+                    # make sure that the item exists
+                    # https://github.com/music-assistant/hass-music-assistant/issues/707
+                    db_item = await self.mass.music.playlists.add_db_item(playlist)
+                    subitems.append(db_item)
                 continue
 
         return BrowseFolder(
@@ -193,7 +208,8 @@ class FileSystemProvider(MusicProvider):
             provider=self.type,
             path=path,
             name=sub_path or self.name,
-            items=subitems,
+            # make sure to sort the resulting listing
+            items=sorted(subitems, key=lambda x: x.name),
         )
 
     async def sync_library(
@@ -242,13 +258,15 @@ class FileSystemProvider(MusicProvider):
                     playlist = await self._parse_playlist(entry.path)
                     # add/update] playlist to db
                     playlist.metadata.checksum = checksum
+                    # playlist is always in-library
+                    playlist.in_library = True
                     await self.mass.music.playlists.add_db_item(playlist)
             except Exception as err:  # pylint: disable=broad-except
                 # we don't want the whole sync to crash on one file so we catch all exceptions here
                 self.logger.exception("Error processing %s - %s", entry.path, str(err))
 
             # save checksums every 100 processed items
-            # this allows us to pickup where we leftoff when initial scan gets intterrupted
+            # this allows us to pickup where we leftoff when initial scan gets interrupted
             if save_checksum_interval == 100:
                 await self.mass.cache.set(cache_key, cur_checksums, SCHEMA_VERSION)
                 save_checksum_interval = 0
@@ -765,8 +783,7 @@ class FileSystemProvider(MusicProvider):
 
         playlist = Playlist(playlist_item_id, provider=self.type, name=name)
         playlist.is_editable = True
-        # playlist is always in-library
-        playlist.in_library = True
+
         playlist.add_provider_id(
             MediaItemProviderId(
                 item_id=playlist_item_id,
@@ -787,9 +804,6 @@ class FileSystemProvider(MusicProvider):
         if not file_path:
             return False  # guard
         file_path = await self.resolve(file_path)
-        if self.config.path not in file_path:
-            # additional guard (needed for files within m3u files)
-            return False
         _exists = wrap(os.path.exists)
         return await _exists(file_path)