2024-10-05 14:43:43 -04:00
|
|
|
namespace AppModel;
|
|
|
|
|
2024-10-05 21:00:33 -04:00
|
|
|
using HomeAssistantGenerated;
|
|
|
|
using NetDaemon.AppModel;
|
|
|
|
using NetDaemon.HassModel;
|
|
|
|
using NetDaemon.HassModel.Integration;
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
record ServiceData(string? criteria);
|
2024-10-05 14:43:43 -04:00
|
|
|
|
|
|
|
[NetDaemonApp]
|
|
|
|
public class TestScript
|
|
|
|
{
|
2024-10-05 21:00:33 -04:00
|
|
|
// Snake-case json options
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
};
|
|
|
|
|
2024-10-05 14:43:43 -04:00
|
|
|
public TestScript(IHaContext ha)
|
|
|
|
{
|
|
|
|
ha.RegisterServiceCallBack<ServiceData>(
|
|
|
|
"callback_demo",
|
2024-10-05 21:00:33 -04:00
|
|
|
async (e) =>
|
|
|
|
{
|
|
|
|
var result = (await ha.CallServiceWithResponseAsync(
|
|
|
|
"spotifyplus",
|
|
|
|
"search_artists",
|
|
|
|
data: new SpotifyplusSearchArtistsParameters
|
|
|
|
{
|
|
|
|
Criteria = e?.criteria,
|
|
|
|
Limit = 1,
|
|
|
|
EntityId = "media_player.spotifyplus"
|
|
|
|
}
|
|
|
|
)).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions);
|
|
|
|
|
|
|
|
string? uri = result?.Result?.Items?[0]?.Uri;
|
|
|
|
|
|
|
|
if (uri is not null)
|
|
|
|
{
|
|
|
|
ha.CallService(
|
|
|
|
"notify",
|
|
|
|
"persistent_notification",
|
|
|
|
data: new PersistentNotificationCreateParameters
|
|
|
|
{
|
|
|
|
Message = $"value: {uri}",
|
|
|
|
Title = "title"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-10-05 14:43:43 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|