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
};
public PlayAlbum(IHaContext ha)
public PlayAlbum(IHaContext ha, Services services, Entities entities)
{
ha.RegisterServiceCallBack<PlayAlbumData>(
"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,
var result = (await services.Spotifyplus.SearchAlbumsAsync(
criteria: $"{e?.artist} {e?.album}",
limitTotal: 1,
entityId: Global.DEFAULT_ENTITY_ID,
// My Defaults
Market = "CA",
IncludeExternal = "audio",
}
market: "CA",
includeExternal: "audio"
)).Value.Deserialize<SpotifyplusSearchAlbumsResponse>(_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,
services.Spotifyplus.PlayerMediaPlayContext(
contextUri: uri,
entityId: Global.DEFAULT_ENTITY_ID,
deviceId: Global.DEFAULT_DEV_ID,
// My Defaults
PositionMs = 0,
Delay = 0.50,
}
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");
}
}
);

View file

@ -19,7 +19,7 @@ public class PlayArtist
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
public PlayArtist(IHaContext ha)
public PlayArtist(IHaContext ha, Services services, Entities entities)
{
ha.RegisterServiceCallBack<PlayArtistData>(
"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,
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",
}
market: "CA",
includeExternal: "audio"
)).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_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,
services.Spotifyplus.PlayerMediaPlayContext(
contextUri: uri,
entityId: Global.DEFAULT_ENTITY_ID,
deviceId: Global.DEFAULT_DEV_ID,
// My Defaults
PositionMs = 0,
Delay = 0.50,
}
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");
}
}
);

View file

@ -22,7 +22,7 @@ public class PlayPlaylist
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
public PlayPlaylist(IHaContext ha)
public PlayPlaylist(IHaContext ha, Services services, Entities entities)
{
ha.RegisterServiceCallBack<PlayPlaylistData>(
"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<SpotifyplusPlaylistResponse>(_jsonOptions);
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
if (match.Score < 85)
{
var otherResult = (await ha.CallServiceWithResponseAsync(
"spotifyplus",
"search_playlists",
data: new SpotifyplusSearchPlaylistsParameters
{
Criteria = query,
LimitTotal = 1,
EntityId = Global.DEFAULT_ENTITY_ID,
var otherResult = (await services.Spotifyplus.SearchPlaylistsAsync(
criteria: query,
limitTotal: 1,
entityId: Global.DEFAULT_ENTITY_ID,
// My Defaults
Market = "CA",
IncludeExternal = "audio",
}
market: "CA",
includeExternal: "audio"
)).Value.Deserialize<SpotifyplusPlaylistResponse>(_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,
services.Spotifyplus.PlayerMediaPlayContext(
contextUri: uri,
entityId: Global.DEFAULT_ENTITY_ID,
deviceId: Global.DEFAULT_DEV_ID,
// My Defaults
PositionMs = 0,
Delay = 0.50,
}
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");
}
}
);

View file

@ -18,7 +18,7 @@ public class PlaySong
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
public PlaySong(IHaContext ha)
public PlaySong(IHaContext ha, Services services, Entities entities)
{
ha.RegisterServiceCallBack<PlaySongData>(
"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,
var result = (await services.Spotifyplus.SearchTracksAsync(
criteria: $"{e?.artist} {e?.song}",
limitTotal: 1,
entityId: Global.DEFAULT_ENTITY_ID,
// My Defaults
Market = "CA",
IncludeExternal = "audio",
}
market: "CA",
includeExternal: "audio"
)).Value.Deserialize<SpotifyplusSearchTracksResponse>(_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,
services.Spotifyplus.PlayerMediaPlayTracks(
uris: uri,
entityId: Global.DEFAULT_ENTITY_ID,
deviceId: Global.DEFAULT_DEV_ID,
// My Defaults
PositionMs = 0,
Delay = 0.50,
}
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");
}
}
);