Skip to content

Commit 38e6133

Browse files
PeteRagerfrenck
authored andcommitted
Fix browsing Audible Favorites in Sonos (#140378)
* initial commit * updates * update test data
1 parent 8541dc5 commit 38e6133

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

homeassistant/components/sonos/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
SONOS_COMPOSER = "composers"
3333
SONOS_RADIO = "radio"
3434
SONOS_OTHER_ITEM = "other items"
35+
SONOS_AUDIO_BOOK = "audio book"
3536

3637
SONOS_STATE_PLAYING = "PLAYING"
3738
SONOS_STATE_TRANSITIONING = "TRANSITIONING"
@@ -67,6 +68,7 @@
6768
"object.item": MediaClass.TRACK,
6869
"object.item.audioItem.musicTrack": MediaClass.TRACK,
6970
"object.item.audioItem.audioBroadcast": MediaClass.GENRE,
71+
"object.item.audioItem.audioBook": MediaClass.TRACK,
7072
}
7173

7274
SONOS_TO_MEDIA_TYPES = {
@@ -84,6 +86,7 @@
8486
"object.container.playlistContainer.sameArtist": MediaType.ARTIST,
8587
"object.container.playlistContainer": MediaType.PLAYLIST,
8688
"object.item.audioItem.musicTrack": MediaType.TRACK,
89+
"object.item.audioItem.audioBook": MediaType.TRACK,
8790
}
8891

8992
MEDIA_TYPES_TO_SONOS: dict[MediaType | str, str] = {
@@ -113,6 +116,7 @@
113116
"object.item": SONOS_OTHER_ITEM,
114117
"object.item.audioItem.musicTrack": SONOS_TRACKS,
115118
"object.item.audioItem.audioBroadcast": SONOS_RADIO,
119+
"object.item.audioItem.audioBook": SONOS_AUDIO_BOOK,
116120
}
117121

118122
LIBRARY_TITLES_MAPPING = {

tests/components/sonos/fixtures/sonos_favorites.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,23 @@
3434
"protocol_info": "a:b:c:d"
3535
}
3636
]
37+
},
38+
{
39+
"title": "American Tall Tales",
40+
"parent_id": "FV:2",
41+
"item_id": "FV:2/66",
42+
"restricted": false,
43+
"resource_meta_data": "<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" xmlns:r=\"urn:schemas-rinconnetworks-com:metadata-1-0/\" xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"><item id=\"101340c8reftitleC9F27_com\" parentID=\"101340c8reftitleC9F27_com\" restricted=\"true\"><dc:title>American Tall Tales</dc:title><upnp:class>object.item.audioItem.audioBook</upnp:class><desc id=\"cdudn\" nameSpace=\"urn:schemas-rinconnetworks-com:metadata-1-0/\">SA_RINCON61191_X_#Svc6-0-Token</desc></item></DIDL-Lite>",
44+
"resources": [
45+
{
46+
"uri": "x-rincon-cpcontainer:101340c8reftitle%C9F27_com?sid=239&flags=16584&sn=5",
47+
"protocol_info": "x-rincon-cpcontainer:*:*:*"
48+
}
49+
],
50+
"desc": null,
51+
"album_art_uri": "https://m.media-amazon.com/images/I/810lqLo5m0L._SL600_.jpg",
52+
"type": "instantPlay",
53+
"description": "Audible",
54+
"favorite_nr": "0"
3755
}
3856
]

tests/components/sonos/snapshots/test_media_browser.ambr

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,74 @@
11
# serializer version: 1
2+
# name: test_browse_media_favorites[-favorites]
3+
dict({
4+
'can_expand': True,
5+
'can_play': False,
6+
'children': list([
7+
dict({
8+
'can_expand': True,
9+
'can_play': False,
10+
'children_media_class': None,
11+
'media_class': 'album',
12+
'media_content_id': 'object.container.album.musicAlbum',
13+
'media_content_type': 'favorites_folder',
14+
'thumbnail': None,
15+
'title': 'Albums',
16+
}),
17+
dict({
18+
'can_expand': True,
19+
'can_play': False,
20+
'children_media_class': None,
21+
'media_class': 'track',
22+
'media_content_id': 'object.item.audioItem.audioBook',
23+
'media_content_type': 'favorites_folder',
24+
'thumbnail': None,
25+
'title': 'Audio Book',
26+
}),
27+
dict({
28+
'can_expand': True,
29+
'can_play': False,
30+
'children_media_class': None,
31+
'media_class': 'genre',
32+
'media_content_id': 'object.item.audioItem.audioBroadcast',
33+
'media_content_type': 'favorites_folder',
34+
'thumbnail': None,
35+
'title': 'Radio',
36+
}),
37+
]),
38+
'children_media_class': 'directory',
39+
'media_class': 'directory',
40+
'media_content_id': '',
41+
'media_content_type': 'favorites',
42+
'not_shown': 0,
43+
'thumbnail': None,
44+
'title': 'Favorites',
45+
})
46+
# ---
47+
# name: test_browse_media_favorites[object.item.audioItem.audioBook-favorites_folder]
48+
dict({
49+
'can_expand': True,
50+
'can_play': False,
51+
'children': list([
52+
dict({
53+
'can_expand': False,
54+
'can_play': True,
55+
'children_media_class': None,
56+
'media_class': 'track',
57+
'media_content_id': 'FV:2/66',
58+
'media_content_type': 'favorite_item_id',
59+
'thumbnail': 'https://m.media-amazon.com/images/I/810lqLo5m0L._SL600_.jpg',
60+
'title': 'American Tall Tales',
61+
}),
62+
]),
63+
'children_media_class': 'track',
64+
'media_class': 'directory',
65+
'media_content_id': '',
66+
'media_content_type': 'favorites',
67+
'not_shown': 0,
68+
'thumbnail': None,
69+
'title': 'Audio Book',
70+
})
71+
# ---
272
# name: test_browse_media_library
373
list([
474
dict({

tests/components/sonos/test_media_browser.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from functools import partial
44

5+
import pytest
56
from syrupy import SnapshotAssertion
67

78
from homeassistant.components.media_player import BrowseMedia, MediaClass, MediaType
@@ -176,3 +177,39 @@ async def test_browse_media_library_albums(
176177
assert response["success"]
177178
assert response["result"]["children"] == snapshot
178179
assert soco_mock.music_library.browse_by_idstring.call_count == 1
180+
181+
182+
@pytest.mark.parametrize(
183+
("media_content_id", "media_content_type"),
184+
[
185+
(
186+
"",
187+
"favorites",
188+
),
189+
(
190+
"object.item.audioItem.audioBook",
191+
"favorites_folder",
192+
),
193+
],
194+
)
195+
async def test_browse_media_favorites(
196+
async_autosetup_sonos,
197+
hass_ws_client: WebSocketGenerator,
198+
snapshot: SnapshotAssertion,
199+
media_content_id,
200+
media_content_type,
201+
) -> None:
202+
"""Test the async_browse_media method."""
203+
client = await hass_ws_client()
204+
await client.send_json(
205+
{
206+
"id": 1,
207+
"type": "media_player/browse_media",
208+
"entity_id": "media_player.zone_a",
209+
"media_content_id": media_content_id,
210+
"media_content_type": media_content_type,
211+
}
212+
)
213+
response = await client.receive_json()
214+
assert response["success"]
215+
assert response["result"] == snapshot

0 commit comments

Comments
 (0)