nixos-configs/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayArtist/PlayArtist.cs
matt1432 98c27b1051
All checks were successful
Discord / discord commits (push) Has been skipped
feat(hass): add PlayAlbum voice command
2024-10-08 02:07:05 -04:00

54 lines
1.6 KiB
C#

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