nixos-configs/devices/homie/modules/home-assistant/netdaemon/apps/Spotify/PlayArtist/PlayArtist.cs
matt1432 1ff2b3442a
All checks were successful
Discord / discord commits (push) Has been skipped
feat(hass): switch to notify for notifs
2024-12-01 15:41:42 -05:00

66 lines
2.3 KiB
C#

using System;
using System.Reflection;
using System.Text.Json;
using HomeAssistantGenerated;
using NetDaemon.AppModel;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Integration;
namespace NetDaemonConfig.Apps.Spotify.PlayArtist
{
public record PlayArtistData(string? artist);
[NetDaemonApp]
public class PlayArtist
{
// Snake-case json options
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
public PlayArtist(IHaContext ha, Services services)
{
ha.RegisterServiceCallBack<PlayArtistData>(
"spotify_play_artist",
async (e) =>
{
try
{
SpotifyplusSearchArtistsResponse? result = (
await services.Spotifyplus.SearchArtistsAsync(
criteria: e?.artist ?? throw new TargetException($"The artist {e?.artist} could not be found."),
limitTotal: 1,
entityId: SpotifyTypes.DefaultEntityId,
// My Defaults
market: "CA",
includeExternal: "audio"
)
).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions);
string uri = result?.Result?.Items?[0]?.Uri ??
throw new TargetException($"The artist {e?.artist} could not be found.");
services.Spotifyplus.PlayerMediaPlayContext(
contextUri: uri,
entityId: SpotifyTypes.DefaultEntityId,
deviceId: SpotifyTypes.DefaultDevId,
// My Defaults
positionMs: 0,
delay: 0.50
);
}
catch (Exception error)
{
services.Notify.PersistentNotification(
message: error.Message,
title: "Erreur Spotify");
}
}
);
}
}
}