2024-10-31 23:13:02 -04:00
|
|
|
using System;
|
|
|
|
using System.Reflection;
|
2024-12-18 16:44:21 -05:00
|
|
|
using System.Text.Json;
|
2024-10-31 23:13:02 -04:00
|
|
|
|
|
|
|
using HomeAssistantGenerated;
|
|
|
|
|
|
|
|
using NetDaemon.AppModel;
|
|
|
|
using NetDaemon.HassModel;
|
|
|
|
using NetDaemon.HassModel.Integration;
|
|
|
|
|
2024-12-18 19:25:22 -05:00
|
|
|
using NetDaemonConfig.Apps.Spotify.Types;
|
|
|
|
|
2024-10-31 23:13:02 -04:00
|
|
|
|
|
|
|
namespace NetDaemonConfig.Apps.Spotify.PlaySong
|
|
|
|
{
|
2024-11-01 00:09:42 -04:00
|
|
|
public record PlaySongData(string? artist, string? song);
|
2024-10-31 23:13:02 -04:00
|
|
|
|
|
|
|
[NetDaemonApp]
|
|
|
|
public class PlaySong
|
|
|
|
{
|
|
|
|
// Snake-case json options
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions = new()
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
};
|
|
|
|
|
|
|
|
public PlaySong(IHaContext ha, Services services)
|
|
|
|
{
|
|
|
|
ha.RegisterServiceCallBack<PlaySongData>(
|
|
|
|
"spotify_play_song",
|
|
|
|
async (e) =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SpotifyplusSearchTracksResponse? result = (
|
|
|
|
await services.Spotifyplus.SearchTracksAsync(
|
2024-11-01 00:09:42 -04:00
|
|
|
criteria: $"{e?.artist} {e?.song}",
|
2024-10-31 23:13:02 -04:00
|
|
|
limitTotal: 1,
|
2024-12-18 19:25:22 -05:00
|
|
|
entityId: Globals.DefaultEntityId,
|
2024-10-31 23:13:02 -04:00
|
|
|
// My Defaults
|
|
|
|
market: "CA",
|
|
|
|
includeExternal: "audio"
|
|
|
|
)
|
|
|
|
).Value.Deserialize<SpotifyplusSearchTracksResponse>(_jsonOptions);
|
|
|
|
|
|
|
|
string uri = result?.Result?.Items?[0]?.Uri ?? throw new TargetException(
|
2024-11-01 00:09:42 -04:00
|
|
|
$"The song {e?.song}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found."
|
2024-10-31 23:13:02 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
services.Spotifyplus.PlayerMediaPlayTracks(
|
|
|
|
uris: uri,
|
2024-12-18 19:25:22 -05:00
|
|
|
entityId: Globals.DefaultEntityId,
|
|
|
|
deviceId: Globals.DefaultDevId,
|
2024-10-31 23:13:02 -04:00
|
|
|
// My Defaults
|
|
|
|
positionMs: 0,
|
|
|
|
delay: 0.50
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
2024-12-01 15:41:42 -05:00
|
|
|
services.Notify.PersistentNotification(
|
2024-10-31 23:13:02 -04:00
|
|
|
message: error.Message,
|
|
|
|
title: "Erreur Spotify");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|