diff --git a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayAlbum/PlayAlbum.cs b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayAlbum/PlayAlbum.cs index a91952b2..158bd2d6 100644 --- a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayAlbum/PlayAlbum.cs +++ b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayAlbum/PlayAlbum.cs @@ -19,7 +19,7 @@ public class PlayAlbum PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; - public PlayAlbum(IHaContext ha) + public PlayAlbum(IHaContext ha, Services services, Entities entities) { ha.RegisterServiceCallBack( "spotify_play_album", @@ -27,18 +27,13 @@ public class PlayAlbum { try { - var result = (await ha.CallServiceWithResponseAsync( - "spotifyplus", - "search_albums", - data: new SpotifyplusSearchAlbumsParameters - { - Criteria = $"{e?.artist} {e?.album}", - LimitTotal = 1, - EntityId = Global.DEFAULT_ENTITY_ID, - // My Defaults - Market = "CA", - IncludeExternal = "audio", - } + var result = (await services.Spotifyplus.SearchAlbumsAsync( + criteria: $"{e?.artist} {e?.album}", + limitTotal: 1, + entityId: Global.DEFAULT_ENTITY_ID, + // My Defaults + market: "CA", + includeExternal: "audio" )).Value.Deserialize(_jsonOptions); string uri = result?.Result?.Items?[0]?.Uri ?? @@ -46,31 +41,20 @@ public class PlayAlbum $"The album {e?.album}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found." ); - ha.CallService( - "spotifyplus", - "player_media_play_context", - data: new SpotifyplusPlayerMediaPlayContextParameters - { - ContextUri = uri, - EntityId = Global.DEFAULT_ENTITY_ID, - DeviceId = Global.DEFAULT_DEV_ID, - // My Defaults - PositionMs = 0, - Delay = 0.50, - } + services.Spotifyplus.PlayerMediaPlayContext( + contextUri: uri, + entityId: Global.DEFAULT_ENTITY_ID, + deviceId: Global.DEFAULT_DEV_ID, + // My Defaults + positionMs: 0, + delay: 0.50 ); } catch (Exception error) { - ha.CallService( - "notify", - "persistent_notification", - data: new PersistentNotificationCreateParameters - { - Message = error.Message, - Title = "Erreur Spotify", - } - ); + services.PersistentNotification.Create( + message: error.Message, + title: "Erreur Spotify"); } } ); diff --git a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayArtist/PlayArtist.cs b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayArtist/PlayArtist.cs index 81ffdb19..9cb5764c 100644 --- a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayArtist/PlayArtist.cs +++ b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayArtist/PlayArtist.cs @@ -19,7 +19,7 @@ public class PlayArtist PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; - public PlayArtist(IHaContext ha) + public PlayArtist(IHaContext ha, Services services, Entities entities) { ha.RegisterServiceCallBack( "spotify_play_artist", @@ -27,48 +27,32 @@ public class PlayArtist { try { - var result = (await ha.CallServiceWithResponseAsync( - "spotifyplus", - "search_artists", - data: new SpotifyplusSearchArtistsParameters - { - Criteria = e?.artist, - LimitTotal = 1, - EntityId = Global.DEFAULT_ENTITY_ID, - // My Defaults - Market = "CA", - IncludeExternal = "audio", - } + var result = (await services.Spotifyplus.SearchArtistsAsync( + criteria: e?.artist ?? throw new NullReferenceException($"The artist {e?.artist} could not be found."), + limitTotal: 1, + entityId: Global.DEFAULT_ENTITY_ID, + // My Defaults + market: "CA", + includeExternal: "audio" )).Value.Deserialize(_jsonOptions); string uri = result?.Result?.Items?[0]?.Uri ?? throw new NullReferenceException($"The artist {e?.artist} could not be found."); - ha.CallService( - "spotifyplus", - "player_media_play_context", - data: new SpotifyplusPlayerMediaPlayContextParameters - { - ContextUri = uri, - EntityId = Global.DEFAULT_ENTITY_ID, - DeviceId = Global.DEFAULT_DEV_ID, - // My Defaults - PositionMs = 0, - Delay = 0.50, - } + services.Spotifyplus.PlayerMediaPlayContext( + contextUri: uri, + entityId: Global.DEFAULT_ENTITY_ID, + deviceId: Global.DEFAULT_DEV_ID, + // My Defaults + positionMs: 0, + delay: 0.50 ); } catch (Exception error) { - ha.CallService( - "notify", - "persistent_notification", - data: new PersistentNotificationCreateParameters - { - Message = error.Message, - Title = "Erreur Spotify", - } - ); + services.PersistentNotification.Create( + message: error.Message, + title: "Erreur Spotify"); } } ); diff --git a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayPlaylist/PlayPlaylist.cs b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayPlaylist/PlayPlaylist.cs index da32b049..eb44e3b6 100644 --- a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayPlaylist/PlayPlaylist.cs +++ b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayPlaylist/PlayPlaylist.cs @@ -22,7 +22,7 @@ public class PlayPlaylist PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; - public PlayPlaylist(IHaContext ha) + public PlayPlaylist(IHaContext ha, Services services, Entities entities) { ha.RegisterServiceCallBack( "spotify_play_playlist", @@ -32,15 +32,10 @@ public class PlayPlaylist { string query = e?.playlist ?? throw new NullReferenceException("Query not found."); - var result = (await ha.CallServiceWithResponseAsync( - "spotifyplus", - "get_playlist_favorites", - data: new SpotifyplusGetPlaylistFavoritesParameters - { - LimitTotal = 200, - SortResult = true, - EntityId = Global.DEFAULT_ENTITY_ID, - } + var result = (await services.Spotifyplus.GetPlaylistFavoritesAsync( + limitTotal: 200, + sortResult: true, + entityId: Global.DEFAULT_ENTITY_ID )).Value.Deserialize(_jsonOptions); List myPlaylists = result?.Result?.Items ?? @@ -58,18 +53,13 @@ public class PlayPlaylist // We search outside the user's playlists if the score is too low if (match.Score < 85) { - var otherResult = (await ha.CallServiceWithResponseAsync( - "spotifyplus", - "search_playlists", - data: new SpotifyplusSearchPlaylistsParameters - { - Criteria = query, - LimitTotal = 1, - EntityId = Global.DEFAULT_ENTITY_ID, - // My Defaults - Market = "CA", - IncludeExternal = "audio", - } + var otherResult = (await services.Spotifyplus.SearchPlaylistsAsync( + criteria: query, + limitTotal: 1, + entityId: Global.DEFAULT_ENTITY_ID, + // My Defaults + market: "CA", + includeExternal: "audio" )).Value.Deserialize(_jsonOptions); string potentialUri = otherResult?.Result?.Items?[0]?.Uri ?? @@ -78,31 +68,20 @@ public class PlayPlaylist uri = potentialUri; } - ha.CallService( - "spotifyplus", - "player_media_play_context", - data: new SpotifyplusPlayerMediaPlayContextParameters - { - ContextUri = uri, - EntityId = Global.DEFAULT_ENTITY_ID, - DeviceId = Global.DEFAULT_DEV_ID, - // My Defaults - PositionMs = 0, - Delay = 0.50, - } + services.Spotifyplus.PlayerMediaPlayContext( + contextUri: uri, + entityId: Global.DEFAULT_ENTITY_ID, + deviceId: Global.DEFAULT_DEV_ID, + // My Defaults + positionMs: 0, + delay: 0.50 ); } catch (Exception error) { - ha.CallService( - "notify", - "persistent_notification", - data: new PersistentNotificationCreateParameters - { - Message = error.Message, - Title = "Erreur Spotify", - } - ); + services.PersistentNotification.Create( + message: error.Message, + title: "Erreur Spotify"); } } ); diff --git a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlaySong/PlaySong.cs b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlaySong/PlaySong.cs index 3aac5c3d..0cbe9edb 100644 --- a/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlaySong/PlaySong.cs +++ b/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlaySong/PlaySong.cs @@ -18,7 +18,7 @@ public class PlaySong PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; - public PlaySong(IHaContext ha) + public PlaySong(IHaContext ha, Services services, Entities entities) { ha.RegisterServiceCallBack( "spotify_play_song", @@ -26,49 +26,33 @@ public class PlaySong { try { - var result = (await ha.CallServiceWithResponseAsync( - "spotifyplus", - "search_tracks", - data: new SpotifyplusSearchTracksParameters - { - Criteria = $"{e?.artist} {e?.song}", - LimitTotal = 1, - EntityId = Global.DEFAULT_ENTITY_ID, - // My Defaults - Market = "CA", - IncludeExternal = "audio", - } + var result = (await services.Spotifyplus.SearchTracksAsync( + criteria: $"{e?.artist} {e?.song}", + limitTotal: 1, + entityId: Global.DEFAULT_ENTITY_ID, + // My Defaults + market: "CA", + includeExternal: "audio" )).Value.Deserialize(_jsonOptions); string uri = result?.Result?.Items?[0]?.Uri ?? throw new NullReferenceException( $"The song {e?.song}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found." ); - ha.CallService( - "spotifyplus", - "player_media_play_tracks", - data: new SpotifyplusPlayerMediaPlayTracksParameters - { - Uris = uri, - EntityId = Global.DEFAULT_ENTITY_ID, - DeviceId = Global.DEFAULT_DEV_ID, - // My Defaults - PositionMs = 0, - Delay = 0.50, - } + services.Spotifyplus.PlayerMediaPlayTracks( + uris: uri, + entityId: Global.DEFAULT_ENTITY_ID, + deviceId: Global.DEFAULT_DEV_ID, + // My Defaults + positionMs: 0, + delay: 0.50 ); } catch (Exception error) { - ha.CallService( - "notify", - "persistent_notification", - data: new PersistentNotificationCreateParameters - { - Message = error.Message, - Title = "Erreur Spotify", - } - ); + services.PersistentNotification.Create( + message: error.Message, + title: "Erreur Spotify"); } } );