From 9214c4ffc9a450cb2fced3d635cbe9853c61d153 Mon Sep 17 00:00:00 2001 From: Johan Frick Date: Fri, 29 Dec 2023 12:12:07 +0100 Subject: [PATCH] fix: gracefully proceed if favorites are not available for a player --- src/sections/media-browser.ts | 3 +-- src/services/media-browse-service.ts | 17 +++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/sections/media-browser.ts b/src/sections/media-browser.ts index 9d9ed831..4d8e188f 100755 --- a/src/sections/media-browser.ts +++ b/src/sections/media-browser.ts @@ -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) { diff --git a/src/services/media-browse-service.ts b/src/services/media-browse-service.ts index 3f3981e4..17a3b24c 100644 --- a/src/services/media-browse-service.ts +++ b/src/services/media-browse-service.ts @@ -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[]) {