refactor(netd): switch to services object
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-10-31 20:07:05 -04:00
parent a2289ba070
commit 2595445f14
4 changed files with 76 additions and 145 deletions

View file

@ -19,7 +19,7 @@ public class PlayAlbum
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlayAlbum(IHaContext ha) public PlayAlbum(IHaContext ha, Services services, Entities entities)
{ {
ha.RegisterServiceCallBack<PlayAlbumData>( ha.RegisterServiceCallBack<PlayAlbumData>(
"spotify_play_album", "spotify_play_album",
@ -27,18 +27,13 @@ public class PlayAlbum
{ {
try try
{ {
var result = (await ha.CallServiceWithResponseAsync( var result = (await services.Spotifyplus.SearchAlbumsAsync(
"spotifyplus", criteria: $"{e?.artist} {e?.album}",
"search_albums", limitTotal: 1,
data: new SpotifyplusSearchAlbumsParameters entityId: Global.DEFAULT_ENTITY_ID,
{
Criteria = $"{e?.artist} {e?.album}",
LimitTotal = 1,
EntityId = Global.DEFAULT_ENTITY_ID,
// My Defaults // My Defaults
Market = "CA", market: "CA",
IncludeExternal = "audio", includeExternal: "audio"
}
)).Value.Deserialize<SpotifyplusSearchAlbumsResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusSearchAlbumsResponse>(_jsonOptions);
string uri = result?.Result?.Items?[0]?.Uri ?? 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." $"The album {e?.album}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found."
); );
ha.CallService( services.Spotifyplus.PlayerMediaPlayContext(
"spotifyplus", contextUri: uri,
"player_media_play_context", entityId: Global.DEFAULT_ENTITY_ID,
data: new SpotifyplusPlayerMediaPlayContextParameters deviceId: Global.DEFAULT_DEV_ID,
{
ContextUri = uri,
EntityId = Global.DEFAULT_ENTITY_ID,
DeviceId = Global.DEFAULT_DEV_ID,
// My Defaults // My Defaults
PositionMs = 0, positionMs: 0,
Delay = 0.50, delay: 0.50
}
); );
} }
catch (Exception error) catch (Exception error)
{ {
ha.CallService( services.PersistentNotification.Create(
"notify", message: error.Message,
"persistent_notification", title: "Erreur Spotify");
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
} }
} }
); );

View file

@ -19,7 +19,7 @@ public class PlayArtist
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlayArtist(IHaContext ha) public PlayArtist(IHaContext ha, Services services, Entities entities)
{ {
ha.RegisterServiceCallBack<PlayArtistData>( ha.RegisterServiceCallBack<PlayArtistData>(
"spotify_play_artist", "spotify_play_artist",
@ -27,48 +27,32 @@ public class PlayArtist
{ {
try try
{ {
var result = (await ha.CallServiceWithResponseAsync( var result = (await services.Spotifyplus.SearchArtistsAsync(
"spotifyplus", criteria: e?.artist ?? throw new NullReferenceException($"The artist {e?.artist} could not be found."),
"search_artists", limitTotal: 1,
data: new SpotifyplusSearchArtistsParameters entityId: Global.DEFAULT_ENTITY_ID,
{
Criteria = e?.artist,
LimitTotal = 1,
EntityId = Global.DEFAULT_ENTITY_ID,
// My Defaults // My Defaults
Market = "CA", market: "CA",
IncludeExternal = "audio", includeExternal: "audio"
}
)).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions);
string uri = result?.Result?.Items?[0]?.Uri ?? string uri = result?.Result?.Items?[0]?.Uri ??
throw new NullReferenceException($"The artist {e?.artist} could not be found."); throw new NullReferenceException($"The artist {e?.artist} could not be found.");
ha.CallService( services.Spotifyplus.PlayerMediaPlayContext(
"spotifyplus", contextUri: uri,
"player_media_play_context", entityId: Global.DEFAULT_ENTITY_ID,
data: new SpotifyplusPlayerMediaPlayContextParameters deviceId: Global.DEFAULT_DEV_ID,
{
ContextUri = uri,
EntityId = Global.DEFAULT_ENTITY_ID,
DeviceId = Global.DEFAULT_DEV_ID,
// My Defaults // My Defaults
PositionMs = 0, positionMs: 0,
Delay = 0.50, delay: 0.50
}
); );
} }
catch (Exception error) catch (Exception error)
{ {
ha.CallService( services.PersistentNotification.Create(
"notify", message: error.Message,
"persistent_notification", title: "Erreur Spotify");
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
} }
} }
); );

View file

@ -22,7 +22,7 @@ public class PlayPlaylist
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlayPlaylist(IHaContext ha) public PlayPlaylist(IHaContext ha, Services services, Entities entities)
{ {
ha.RegisterServiceCallBack<PlayPlaylistData>( ha.RegisterServiceCallBack<PlayPlaylistData>(
"spotify_play_playlist", "spotify_play_playlist",
@ -32,15 +32,10 @@ public class PlayPlaylist
{ {
string query = e?.playlist ?? throw new NullReferenceException("Query not found."); string query = e?.playlist ?? throw new NullReferenceException("Query not found.");
var result = (await ha.CallServiceWithResponseAsync( var result = (await services.Spotifyplus.GetPlaylistFavoritesAsync(
"spotifyplus", limitTotal: 200,
"get_playlist_favorites", sortResult: true,
data: new SpotifyplusGetPlaylistFavoritesParameters entityId: Global.DEFAULT_ENTITY_ID
{
LimitTotal = 200,
SortResult = true,
EntityId = Global.DEFAULT_ENTITY_ID,
}
)).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions);
List<PlaylistsItem> myPlaylists = result?.Result?.Items ?? List<PlaylistsItem> myPlaylists = result?.Result?.Items ??
@ -58,18 +53,13 @@ public class PlayPlaylist
// We search outside the user's playlists if the score is too low // We search outside the user's playlists if the score is too low
if (match.Score < 85) if (match.Score < 85)
{ {
var otherResult = (await ha.CallServiceWithResponseAsync( var otherResult = (await services.Spotifyplus.SearchPlaylistsAsync(
"spotifyplus", criteria: query,
"search_playlists", limitTotal: 1,
data: new SpotifyplusSearchPlaylistsParameters entityId: Global.DEFAULT_ENTITY_ID,
{
Criteria = query,
LimitTotal = 1,
EntityId = Global.DEFAULT_ENTITY_ID,
// My Defaults // My Defaults
Market = "CA", market: "CA",
IncludeExternal = "audio", includeExternal: "audio"
}
)).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions);
string potentialUri = otherResult?.Result?.Items?[0]?.Uri ?? string potentialUri = otherResult?.Result?.Items?[0]?.Uri ??
@ -78,31 +68,20 @@ public class PlayPlaylist
uri = potentialUri; uri = potentialUri;
} }
ha.CallService( services.Spotifyplus.PlayerMediaPlayContext(
"spotifyplus", contextUri: uri,
"player_media_play_context", entityId: Global.DEFAULT_ENTITY_ID,
data: new SpotifyplusPlayerMediaPlayContextParameters deviceId: Global.DEFAULT_DEV_ID,
{
ContextUri = uri,
EntityId = Global.DEFAULT_ENTITY_ID,
DeviceId = Global.DEFAULT_DEV_ID,
// My Defaults // My Defaults
PositionMs = 0, positionMs: 0,
Delay = 0.50, delay: 0.50
}
); );
} }
catch (Exception error) catch (Exception error)
{ {
ha.CallService( services.PersistentNotification.Create(
"notify", message: error.Message,
"persistent_notification", title: "Erreur Spotify");
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
} }
} }
); );

View file

@ -18,7 +18,7 @@ public class PlaySong
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlaySong(IHaContext ha) public PlaySong(IHaContext ha, Services services, Entities entities)
{ {
ha.RegisterServiceCallBack<PlaySongData>( ha.RegisterServiceCallBack<PlaySongData>(
"spotify_play_song", "spotify_play_song",
@ -26,49 +26,33 @@ public class PlaySong
{ {
try try
{ {
var result = (await ha.CallServiceWithResponseAsync( var result = (await services.Spotifyplus.SearchTracksAsync(
"spotifyplus", criteria: $"{e?.artist} {e?.song}",
"search_tracks", limitTotal: 1,
data: new SpotifyplusSearchTracksParameters entityId: Global.DEFAULT_ENTITY_ID,
{
Criteria = $"{e?.artist} {e?.song}",
LimitTotal = 1,
EntityId = Global.DEFAULT_ENTITY_ID,
// My Defaults // My Defaults
Market = "CA", market: "CA",
IncludeExternal = "audio", includeExternal: "audio"
}
)).Value.Deserialize<SpotifyplusSearchTracksResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusSearchTracksResponse>(_jsonOptions);
string uri = result?.Result?.Items?[0]?.Uri ?? throw new NullReferenceException( 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." $"The song {e?.song}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found."
); );
ha.CallService( services.Spotifyplus.PlayerMediaPlayTracks(
"spotifyplus", uris: uri,
"player_media_play_tracks", entityId: Global.DEFAULT_ENTITY_ID,
data: new SpotifyplusPlayerMediaPlayTracksParameters deviceId: Global.DEFAULT_DEV_ID,
{
Uris = uri,
EntityId = Global.DEFAULT_ENTITY_ID,
DeviceId = Global.DEFAULT_DEV_ID,
// My Defaults // My Defaults
PositionMs = 0, positionMs: 0,
Delay = 0.50, delay: 0.50
}
); );
} }
catch (Exception error) catch (Exception error)
{ {
ha.CallService( services.PersistentNotification.Create(
"notify", message: error.Message,
"persistent_notification", title: "Erreur Spotify");
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
} }
} }
); );