From d4f38099aec9dac76b75c04cf4b5a74914c2371d Mon Sep 17 00:00:00 2001
From: peteS-UK <64092177+peteS-UK@users.noreply.github.com>
Date: Fri, 3 Jan 2025 19:28:05 +0000
Subject: [PATCH] Small fix to allow playing of expandable favorites on
 Squeezebox (#134572)

---
 homeassistant/components/squeezebox/browse_media.py | 10 ++++++----
 tests/components/squeezebox/conftest.py             |  3 +++
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/homeassistant/components/squeezebox/browse_media.py b/homeassistant/components/squeezebox/browse_media.py
index 4d1c98bc4fc..331bf383c70 100644
--- a/homeassistant/components/squeezebox/browse_media.py
+++ b/homeassistant/components/squeezebox/browse_media.py
@@ -115,6 +115,7 @@ async def build_item_response(
         item_type = CONTENT_TYPE_TO_CHILD_TYPE[search_type]
 
         children = []
+        list_playable = []
         for item in result["items"]:
             item_id = str(item["id"])
             item_thumbnail: str | None = None
@@ -131,7 +132,7 @@ async def build_item_response(
                     child_media_class = CONTENT_TYPE_MEDIA_CLASS[MediaType.ALBUM]
                     can_expand = True
                     can_play = True
-                elif item["hasitems"]:
+                elif item["hasitems"] and not item["isaudio"]:
                     child_item_type = "Favorites"
                     child_media_class = CONTENT_TYPE_MEDIA_CLASS["Favorites"]
                     can_expand = True
@@ -139,8 +140,8 @@ async def build_item_response(
                 else:
                     child_item_type = "Favorites"
                     child_media_class = CONTENT_TYPE_MEDIA_CLASS[MediaType.TRACK]
-                    can_expand = False
-                    can_play = True
+                    can_expand = item["hasitems"]
+                    can_play = item["isaudio"] and item.get("url")
 
             if artwork_track_id := item.get("artwork_track_id"):
                 if internal_request:
@@ -166,6 +167,7 @@ async def build_item_response(
                     thumbnail=item_thumbnail,
                 )
             )
+            list_playable.append(can_play)
 
     if children is None:
         raise BrowseError(f"Media not found: {search_type} / {search_id}")
@@ -179,7 +181,7 @@ async def build_item_response(
         children_media_class=media_class["children"],
         media_content_id=search_id,
         media_content_type=search_type,
-        can_play=search_type != "Favorites",
+        can_play=any(list_playable),
         children=children,
         can_expand=True,
     )
diff --git a/tests/components/squeezebox/conftest.py b/tests/components/squeezebox/conftest.py
index 2dc0cabeaa6..7b007114420 100644
--- a/tests/components/squeezebox/conftest.py
+++ b/tests/components/squeezebox/conftest.py
@@ -137,6 +137,7 @@ async def mock_async_browse(
             "title": "Fake Item 1",
             "id": FAKE_VALID_ITEM_ID,
             "hasitems": False,
+            "isaudio": True,
             "item_type": child_types[media_type],
             "artwork_track_id": "b35bb9e9",
             "url": "file:///var/lib/squeezeboxserver/music/track_1.mp3",
@@ -145,6 +146,7 @@ async def mock_async_browse(
             "title": "Fake Item 2",
             "id": FAKE_VALID_ITEM_ID + "_2",
             "hasitems": media_type == "favorites",
+            "isaudio": True,
             "item_type": child_types[media_type],
             "image_url": "http://lms.internal:9000/html/images/favorites.png",
             "url": "file:///var/lib/squeezeboxserver/music/track_2.mp3",
@@ -153,6 +155,7 @@ async def mock_async_browse(
             "title": "Fake Item 3",
             "id": FAKE_VALID_ITEM_ID + "_3",
             "hasitems": media_type == "favorites",
+            "isaudio": True,
             "album_id": FAKE_VALID_ITEM_ID if media_type == "favorites" else None,
             "url": "file:///var/lib/squeezeboxserver/music/track_3.mp3",
         },
-- 
GitLab