From 1b0421f87cd13fe052640f278010c7149b85d89e Mon Sep 17 00:00:00 2001 From: matt1432 Date: Sat, 5 Oct 2024 21:00:33 -0400 Subject: [PATCH] feat(netd): setup script with service callback --- .../apps/AppModel/TestScript/SpotifyTypes.cs | 64 +++++++++++++++++++ .../apps/AppModel/TestScript/TestScript.cs | 50 ++++++++++++--- .../netdaemon/apps/GlobalUsings.cs | 6 -- .../home-assistant/netdaemon/program.cs | 7 +- 4 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/SpotifyTypes.cs delete mode 100644 devices/homie/modules/home-assistant/netdaemon/apps/GlobalUsings.cs diff --git a/devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/SpotifyTypes.cs b/devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/SpotifyTypes.cs new file mode 100644 index 00000000..b8d0e04e --- /dev/null +++ b/devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/SpotifyTypes.cs @@ -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? Genres { get; init; } + public string? Href { get; init; } + public string? Id { get; init; } + public string? Image_url { get; init; } + public List? 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? 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; } +} diff --git a/devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/TestScript.cs b/devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/TestScript.cs index daf6bd7c..43fbb61f 100644 --- a/devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/TestScript.cs +++ b/devices/homie/modules/home-assistant/netdaemon/apps/AppModel/TestScript/TestScript.cs @@ -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); -/// -/// Showcases how to instance apps with yaml and use automatic configuration population -/// [NetDaemonApp] public class TestScript { + // Snake-case json options + private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower + }; + public TestScript(IHaContext ha) { ha.RegisterServiceCallBack( "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(_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" + } + ); + } + } ); } } diff --git a/devices/homie/modules/home-assistant/netdaemon/apps/GlobalUsings.cs b/devices/homie/modules/home-assistant/netdaemon/apps/GlobalUsings.cs deleted file mode 100644 index 1566bd72..00000000 --- a/devices/homie/modules/home-assistant/netdaemon/apps/GlobalUsings.cs +++ /dev/null @@ -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; diff --git a/devices/homie/modules/home-assistant/netdaemon/program.cs b/devices/homie/modules/home-assistant/netdaemon/program.cs index 7664333c..cbe7a6e3 100644 --- a/devices/homie/modules/home-assistant/netdaemon/program.cs +++ b/devices/homie/modules/home-assistant/netdaemon/program.cs @@ -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