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;
|
2024-10-09 14:24:07 -04:00
|
|
|
using System;
|
2024-10-05 21:00:33 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
|
2024-10-08 02:07:05 -04:00
|
|
|
record PlayArtistData(string? artist);
|
2024-10-05 14:43:43 -04:00
|
|
|
|
2024-10-09 11:01:53 -04:00
|
|
|
|
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-31 20:07:05 -04:00
|
|
|
public PlayArtist(IHaContext ha, Services services, Entities entities)
|
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) =>
|
|
|
|
{
|
2024-10-09 14:24:07 -04:00
|
|
|
try
|
|
|
|
{
|
2024-10-31 20:07:05 -04:00
|
|
|
var result = (await services.Spotifyplus.SearchArtistsAsync(
|
|
|
|
criteria: e?.artist ?? throw new NullReferenceException($"The artist {e?.artist} could not be found."),
|
|
|
|
limitTotal: 1,
|
|
|
|
entityId: Global.DEFAULT_ENTITY_ID,
|
|
|
|
// My Defaults
|
|
|
|
market: "CA",
|
|
|
|
includeExternal: "audio"
|
2024-10-09 14:24:07 -04:00
|
|
|
)).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions);
|
2024-10-05 21:00:33 -04:00
|
|
|
|
2024-10-09 14:24:07 -04:00
|
|
|
string uri = result?.Result?.Items?[0]?.Uri ??
|
|
|
|
throw new NullReferenceException($"The artist {e?.artist} could not be found.");
|
2024-10-05 21:00:33 -04:00
|
|
|
|
2024-10-31 20:07:05 -04:00
|
|
|
services.Spotifyplus.PlayerMediaPlayContext(
|
|
|
|
contextUri: uri,
|
|
|
|
entityId: Global.DEFAULT_ENTITY_ID,
|
|
|
|
deviceId: Global.DEFAULT_DEV_ID,
|
|
|
|
// My Defaults
|
|
|
|
positionMs: 0,
|
|
|
|
delay: 0.50
|
2024-10-05 21:00:33 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-09 14:24:07 -04:00
|
|
|
catch (Exception error)
|
|
|
|
{
|
2024-10-31 20:07:05 -04:00
|
|
|
services.PersistentNotification.Create(
|
|
|
|
message: error.Message,
|
|
|
|
title: "Erreur Spotify");
|
2024-10-09 14:24:07 -04:00
|
|
|
}
|
2024-10-05 21:00:33 -04:00
|
|
|
}
|
2024-10-05 14:43:43 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|