feat(netd): setup script with service callback
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-10-05 21:00:33 -04:00
parent 7855e8ecdd
commit 1b0421f87c
4 changed files with 110 additions and 17 deletions

View file

@ -0,0 +1,64 @@
namespace AppModel;
using System.Collections.Generic;
public record ExternalUrls
{
public string? Spotify { get; init; }
}
public record Followers
{
public string? Href { get; init; }
public int Total { get; init; }
}
public record Image
{
public string? Url { get; init; }
public int Height { get; init; }
public int Width { get; init; }
}
public record Item
{
public ExternalUrls? External_urls { get; init; }
public Followers? Followers { get; init; }
public List<string?>? Genres { get; init; }
public string? Href { get; init; }
public string? Id { get; init; }
public string? Image_url { get; init; }
public List<Image>? Images { get; init; }
public string? Name { get; init; }
public int Popularity { get; init; }
public string? Type { get; init; }
public string? Uri { get; init; }
}
public record Result
{
public string? Href { get; init; }
public int Limit { get; init; }
public string? Next { get; init; }
public int Offset { get; set; }
public object? Previous { get; init; }
public int Total { get; init; }
public List<Item>? Items { get; init; }
}
public record SpotifyplusSearchArtistsResponse
{
public UserProfile? UserProfile { get; init; }
public Result? Result { get; init; }
}
public record UserProfile
{
public string? Country { get; init; }
public string? DisplayName { get; init; }
public string? Email { get; init; }
public string? Id { get; init; }
public string? Product { get; init; }
public string? Type { get; init; }
public string? Uri { get; init; }
}

View file

@ -1,22 +1,54 @@
namespace AppModel;
record ServiceData(string? action, int value);
using HomeAssistantGenerated;
using NetDaemon.AppModel;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Integration;
using System.Text.Json;
record ServiceData(string? criteria);
/// <summary>
/// Showcases how to instance apps with yaml and use automatic configuration population
/// </summary>
[NetDaemonApp]
public class TestScript
{
// Snake-case json options
private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
public TestScript(IHaContext ha)
{
ha.RegisterServiceCallBack<ServiceData>(
"callback_demo",
e => ha.CallService(
"notify",
"persistent_notification",
data: new {message = $"value: {e?.value}", title = $"{e?.action}"}
)
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"
}
);
}
}
);
}
}

View file

@ -1,6 +0,0 @@
// Common usings for NetDaemon apps
global using System;
global using System.Reactive.Linq;
global using NetDaemon.AppModel;
global using NetDaemon.HassModel;
global using NetDaemon.HassModel.Integration;

View file

@ -1,10 +1,13 @@
using System.Reflection;
using HomeAssistantGenerated;
using Microsoft.Extensions.Hosting;
using NetDaemon.AppModel;
using NetDaemon.Extensions.Logging;
using NetDaemon.Extensions.Scheduler;
using NetDaemon.Extensions.Tts;
using NetDaemon.Runtime;
using HomeAssistantGenerated;
using System;
using System.Reactive.Linq;
using System.Reflection;
#pragma warning disable CA1812