Skip to content

Commit

Permalink
fix: gracefully proceed if favorites are not available for a player
Browse files Browse the repository at this point in the history
  • Loading branch information
punxaphil committed Dec 29, 2023
1 parent 0891712 commit 9214c4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/sections/media-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ export class MediaBrowser extends LitElement {
...(this.config.customSources?.all?.map(MediaBrowser.createSource) || []),
...allFavorites,
];
allFavorites = this.config.numberOfFavoritesToShow
return this.config.numberOfFavoritesToShow
? allFavorites.slice(0, this.config.numberOfFavoritesToShow)
: allFavorites;
return allFavorites;
}

private sortOnTopFavoritesThenAlphabetically(a: string, b: string) {
Expand Down
17 changes: 11 additions & 6 deletions src/services/media-browse-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ export default class MediaBrowseService {
}

private async getFavoritesForPlayer(player: MediaPlayer) {
const favoritesRoot = await this.hassService.browseMedia(player, 'favorites', '');
const favoriteTypesPromise = favoritesRoot.children?.map((favoriteItem) =>
this.hassService.browseMedia(player, favoriteItem.media_content_type, favoriteItem.media_content_id),
);
const favoriteTypes = favoriteTypesPromise ? await Promise.all(favoriteTypesPromise) : [];
return favoriteTypes.flatMap((item) => item.children ?? []);
try {
const favoritesRoot = await this.hassService.browseMedia(player, 'favorites', '');
const favoriteTypesPromise = favoritesRoot.children?.map((favoriteItem) =>
this.hassService.browseMedia(player, favoriteItem.media_content_type, favoriteItem.media_content_id),
);
const favoriteTypes = favoriteTypesPromise ? await Promise.all(favoriteTypesPromise) : [];
return favoriteTypes.flatMap((item) => item.children ?? []);
} catch (e) {
console.log('Custom Sonos Card: error getting favorites for player ' + player.id + ': ' + JSON.stringify(e));
return [];
}
}

private getFavoritesFromStates(mediaPlayers: MediaPlayer[]) {
Expand Down

0 comments on commit 9214c4f

Please sign in to comment.