diff --git a/Intentor/Components/Pages/Home.razor b/Intentor/Components/Pages/Home.razor index dfcdf75..c04585b 100644 --- a/Intentor/Components/Pages/Home.razor +++ b/Intentor/Components/Pages/Home.razor @@ -1,7 +1,48 @@ @page "/" +@using Intentor.Services +@using Intentor.Models +@inject HomeAssistantService HomeAssistant Home -

Hello, world!

+

Entity States

-Welcome to your new app. \ No newline at end of file +@if (states == null) +{ +

Loading...

+} +else if (!states.Any()) +{ +

No entities found.

+} +else +{ + + + + + + + + + + @foreach (var state in states) + { + + + + + + } + +
Entity IDStateLast Changed
@state.EntityId@state.State@state.LastChanged.ToString("g")
+} + +@code { + private List? states; + + protected override async Task OnInitializedAsync() + { + states = await HomeAssistant.GetStatesAsync(); + } +} diff --git a/Intentor/Models/EntityState.cs b/Intentor/Models/EntityState.cs new file mode 100644 index 0000000..7684883 --- /dev/null +++ b/Intentor/Models/EntityState.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace Intentor.Models; + +public class EntityState +{ + [JsonPropertyName("entity_id")] + public string EntityId { get; set; } = string.Empty; + + [JsonPropertyName("state")] + public string State { get; set; } = string.Empty; + + [JsonPropertyName("last_changed")] + public DateTime LastChanged { get; set; } + + [JsonPropertyName("attributes")] + public Dictionary Attributes { get; set; } = new(); +} diff --git a/Intentor/Program.cs b/Intentor/Program.cs index b5c7411..5713acc 100644 --- a/Intentor/Program.cs +++ b/Intentor/Program.cs @@ -1,4 +1,5 @@ using Intentor.Components; +using Intentor.Services; namespace Intentor; @@ -8,7 +9,18 @@ public class Program { var builder = WebApplication.CreateBuilder(args); + // Get Home Assistant Supervisor token + var supervisorToken = Environment.GetEnvironmentVariable("SUPERVISOR_TOKEN") + ?? throw new InvalidOperationException("SUPERVISOR_TOKEN environment variable is required"); + // Add services to the container. + builder.Services.AddHttpClient(client => + { + client.DefaultRequestHeaders.Add("Authorization", $"Bearer {supervisorToken}"); + }); + builder.Services.AddSingleton(sp => + new HomeAssistantService(sp.GetRequiredService().CreateClient(nameof(HomeAssistantService)), supervisorToken)); + builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); diff --git a/Intentor/Services/HomeAssistantService.cs b/Intentor/Services/HomeAssistantService.cs new file mode 100644 index 0000000..8716507 --- /dev/null +++ b/Intentor/Services/HomeAssistantService.cs @@ -0,0 +1,58 @@ +using Intentor.Models; + +namespace Intentor.Services; + +public class HomeAssistantService +{ + private readonly HttpClient _httpClient; + private readonly string _supervisorToken; + private const string SupervisorApiBase = "http://supervisor"; + + public HomeAssistantService(HttpClient httpClient, string supervisorToken) + { + _httpClient = httpClient; + _supervisorToken = supervisorToken; + _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {supervisorToken}"); + } + + public async Task> GetStatesAsync() + { + var states = await GetAsync>("core/api/states"); + return states ?? new List(); + } + + public async Task GetAsync(string endpoint) + { + var response = await _httpClient.GetAsync($"{SupervisorApiBase}/{endpoint}"); + response.EnsureSuccessStatusCode(); + return await response.Content.ReadFromJsonAsync(); + } + + public async Task PostAsync(string endpoint, object? content = null) + { + var response = await _httpClient.PostAsJsonAsync($"{SupervisorApiBase}/{endpoint}", content); + response.EnsureSuccessStatusCode(); + return response; + } + + public async Task PostAsync(string endpoint, object? content = null) + { + var response = await _httpClient.PostAsJsonAsync($"{SupervisorApiBase}/{endpoint}", content); + response.EnsureSuccessStatusCode(); + return await response.Content.ReadFromJsonAsync(); + } + + public async Task PutAsync(string endpoint, object content) + { + var response = await _httpClient.PutAsJsonAsync($"{SupervisorApiBase}/{endpoint}", content); + response.EnsureSuccessStatusCode(); + return response; + } + + public async Task DeleteAsync(string endpoint) + { + var response = await _httpClient.DeleteAsync($"{SupervisorApiBase}/{endpoint}"); + response.EnsureSuccessStatusCode(); + return response; + } +} diff --git a/Intentor/config.yaml b/Intentor/config.yaml index 0b475a5..cfc9b74 100644 --- a/Intentor/config.yaml +++ b/Intentor/config.yaml @@ -1,6 +1,6 @@ name: "Intentor" description: "Intent based automation platform" -version: "0.0.4" +version: 0.0.5 slug: "intentor" init: false arch: @@ -9,6 +9,5 @@ arch: startup: application ports: 8080/tcp: 8080 -ingress: true -ingress_entry: / -ingress_port: 8080 \ No newline at end of file +webui: "http://[HOST]:[PORT:8080]" +homeassistant_api: true \ No newline at end of file