2024-10-05 21:51:43 -04:00
|
|
|
namespace Spotify;
|
2024-10-05 14:43:43 -04:00
|
|
|
|
2024-10-05 21:00:33 -04:00
|
|
|
using HomeAssistantGenerated;
|
|
|
|
using NetDaemon.AppModel;
|
|
|
|
using NetDaemon.HassModel;
|
|
|
|
using NetDaemon.HassModel.Integration;
|
|
|
|
using System.Text.Json;
|
|
|
|
|
2024-10-08 02:07:05 -04:00
|
|
|
record PlayArtistData(string? artist);
|
2024-10-05 14:43:43 -04:00
|
|
|
|
|
|
|
[NetDaemonApp]
|
2024-10-05 21:51:43 -04:00
|
|
|
public class PlayArtist
|
2024-10-05 14:43:43 -04:00
|
|
|
{
|
2024-10-05 21:00:33 -04:00
|
|
|
// Snake-case json options
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
};
|
|
|
|
|
2024-10-05 21:51:43 -04:00
|
|
|
public PlayArtist(IHaContext ha)
|
2024-10-05 14:43:43 -04:00
|
|
|
{
|
2024-10-08 02:07:05 -04:00
|
|
|
ha.RegisterServiceCallBack<PlayArtistData>(
|
2024-10-05 21:51:43 -04:00
|
|
|
"spotify_play_artist",
|
2024-10-05 21:00:33 -04:00
|
|
|
async (e) =>
|
|
|
|
{
|
|
|
|
var result = (await ha.CallServiceWithResponseAsync(
|
|
|
|
"spotifyplus",
|
|
|
|
"search_artists",
|
|
|
|
data: new SpotifyplusSearchArtistsParameters
|
|
|
|
{
|
2024-10-08 02:07:05 -04:00
|
|
|
Criteria = e?.artist,
|
2024-10-05 21:00:33 -04:00
|
|
|
Limit = 1,
|
|
|
|
EntityId = "media_player.spotifyplus"
|
|
|
|
}
|
|
|
|
)).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions);
|
|
|
|
|
|
|
|
string? uri = result?.Result?.Items?[0]?.Uri;
|
|
|
|
|
|
|
|
if (uri is not null)
|
|
|
|
{
|
|
|
|
ha.CallService(
|
2024-10-05 21:51:43 -04:00
|
|
|
"spotifyplus",
|
|
|
|
"player_media_play_context",
|
|
|
|
data: new SpotifyplusPlayerMediaPlayContextParameters
|
2024-10-05 21:00:33 -04:00
|
|
|
{
|
2024-10-05 21:51:43 -04:00
|
|
|
ContextUri = uri,
|
|
|
|
EntityId = "media_player.spotifyplus"
|
2024-10-05 21:00:33 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-10-05 14:43:43 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|