2024-10-31 23:13:02 -04:00
|
|
|
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.PlayAlbum
|
|
|
|
{
|
2024-11-01 00:09:42 -04:00
|
|
|
public record PlayAlbumData(string? artist, string? album);
|
2024-10-31 23:13:02 -04:00
|
|
|
|
|
|
|
[NetDaemonApp]
|
|
|
|
public class PlayAlbum
|
|
|
|
{
|
|
|
|
// Snake-case json options
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions = new()
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
};
|
|
|
|
|
|
|
|
public PlayAlbum(IHaContext ha, Services services)
|
|
|
|
{
|
|
|
|
ha.RegisterServiceCallBack<PlayAlbumData>(
|
|
|
|
"spotify_play_album",
|
|
|
|
async (e) =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SpotifyplusSearchAlbumsResponse? result = (
|
|
|
|
await services.Spotifyplus.SearchAlbumsAsync(
|
2024-11-01 00:09:42 -04:00
|
|
|
criteria: $"{e?.artist} {e?.album}",
|
2024-10-31 23:13:02 -04:00
|
|
|
limitTotal: 1,
|
|
|
|
entityId: SpotifyTypes.DefaultEntityId,
|
|
|
|
// My Defaults
|
|
|
|
market: "CA",
|
|
|
|
includeExternal: "audio"
|
|
|
|
)
|
|
|
|
).Value.Deserialize<SpotifyplusSearchAlbumsResponse>(_jsonOptions);
|
|
|
|
|
|
|
|
string uri = result?.Result?.Items?[0]?.Uri ??
|
|
|
|
throw new TargetException(
|
2024-11-01 00:09:42 -04:00
|
|
|
$"The album {e?.album}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found."
|
2024-10-31 23:13:02 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
services.Spotifyplus.PlayerMediaPlayContext(
|
|
|
|
contextUri: uri,
|
|
|
|
entityId: SpotifyTypes.DefaultEntityId,
|
|
|
|
deviceId: SpotifyTypes.DefaultDevId,
|
|
|
|
// My Defaults
|
|
|
|
positionMs: 0,
|
|
|
|
delay: 0.50
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
services.PersistentNotification.Create(
|
|
|
|
message: error.Message,
|
|
|
|
title: "Erreur Spotify");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|