Integrate Home Assistant API support with new service, enhance UI to display entity states, and update configuration for web UI.
This commit is contained in:
@@ -1,7 +1,48 @@
|
||||
@page "/"
|
||||
@using Intentor.Services
|
||||
@using Intentor.Models
|
||||
@inject HomeAssistantService HomeAssistant
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
<h1>Entity States</h1>
|
||||
|
||||
Welcome to your new app.
|
||||
@if (states == null)
|
||||
{
|
||||
<p>Loading...</p>
|
||||
}
|
||||
else if (!states.Any())
|
||||
{
|
||||
<p>No entities found.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Entity ID</th>
|
||||
<th>State</th>
|
||||
<th>Last Changed</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var state in states)
|
||||
{
|
||||
<tr>
|
||||
<td>@state.EntityId</td>
|
||||
<td>@state.State</td>
|
||||
<td>@state.LastChanged.ToString("g")</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
@code {
|
||||
private List<EntityState>? states;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
states = await HomeAssistant.GetStatesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, object> Attributes { get; set; } = new();
|
||||
}
|
||||
@@ -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<HomeAssistantService>(client =>
|
||||
{
|
||||
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {supervisorToken}");
|
||||
});
|
||||
builder.Services.AddSingleton(sp =>
|
||||
new HomeAssistantService(sp.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(HomeAssistantService)), supervisorToken));
|
||||
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
|
||||
@@ -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<List<EntityState>> GetStatesAsync()
|
||||
{
|
||||
var states = await GetAsync<List<EntityState>>("core/api/states");
|
||||
return states ?? new List<EntityState>();
|
||||
}
|
||||
|
||||
public async Task<T?> GetAsync<T>(string endpoint)
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"{SupervisorApiBase}/{endpoint}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<T>();
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> PostAsync(string endpoint, object? content = null)
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync($"{SupervisorApiBase}/{endpoint}", content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<T?> PostAsync<T>(string endpoint, object? content = null)
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync($"{SupervisorApiBase}/{endpoint}", content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<T>();
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> PutAsync(string endpoint, object content)
|
||||
{
|
||||
var response = await _httpClient.PutAsJsonAsync($"{SupervisorApiBase}/{endpoint}", content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> DeleteAsync(string endpoint)
|
||||
{
|
||||
var response = await _httpClient.DeleteAsync($"{SupervisorApiBase}/{endpoint}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
webui: "http://[HOST]:[PORT:8080]"
|
||||
homeassistant_api: true
|
||||
Reference in New Issue
Block a user