111 lines
3.4 KiB
C#
111 lines
3.4 KiB
C#
using System.Text.Json;
|
|
using System.Globalization;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Evergreen.Health.Application.Health.Integrations.HomeAssistant;
|
|
|
|
public sealed class HomeAssistantState
|
|
{
|
|
[JsonPropertyName("entity_id")]
|
|
public string EntityId { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("state")]
|
|
public string State { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("attributes")]
|
|
public JsonElement Attributes { get; set; }
|
|
|
|
[JsonPropertyName("last_updated")]
|
|
public DateTimeOffset? LastUpdated { get; set; }
|
|
|
|
[JsonPropertyName("last_reported")]
|
|
public DateTimeOffset? LastReported { get; set; }
|
|
|
|
public string? GetStringAttribute(string name)
|
|
{
|
|
if (Attributes.ValueKind != JsonValueKind.Object || !Attributes.TryGetProperty(name, out var value)) return null;
|
|
return value.ValueKind == JsonValueKind.String ? value.GetString() : value.ToString();
|
|
}
|
|
|
|
public double? GetDoubleAttribute(string name)
|
|
{
|
|
if (Attributes.ValueKind != JsonValueKind.Object || !Attributes.TryGetProperty(name, out var value)) return null;
|
|
if (value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var d) && double.IsFinite(d)) return d;
|
|
if (double.TryParse(value.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed) && double.IsFinite(parsed)) return parsed;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public sealed class HomeAssistantDeviceRegistryItem
|
|
{
|
|
[JsonPropertyName("id")]
|
|
public string Id { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("name")]
|
|
public string? Name { get; set; }
|
|
|
|
[JsonPropertyName("name_by_user")]
|
|
public string? NameByUser { get; set; }
|
|
|
|
[JsonPropertyName("model")]
|
|
public string? Model { get; set; }
|
|
|
|
[JsonPropertyName("manufacturer")]
|
|
public string? Manufacturer { get; set; }
|
|
|
|
[JsonPropertyName("area_id")]
|
|
public string? AreaId { get; set; }
|
|
|
|
[JsonPropertyName("disabled_by")]
|
|
public string? DisabledBy { get; set; }
|
|
|
|
[JsonPropertyName("entry_type")]
|
|
public string? EntryType { get; set; }
|
|
}
|
|
|
|
public sealed class HomeAssistantEntityRegistryItem
|
|
{
|
|
[JsonPropertyName("entity_id")]
|
|
public string EntityId { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("device_id")]
|
|
public string? DeviceId { get; set; }
|
|
|
|
[JsonPropertyName("name")]
|
|
public string? Name { get; set; }
|
|
|
|
[JsonPropertyName("original_name")]
|
|
public string? OriginalName { get; set; }
|
|
|
|
[JsonPropertyName("platform")]
|
|
public string? Platform { get; set; }
|
|
|
|
[JsonPropertyName("disabled_by")]
|
|
public string? DisabledBy { get; set; }
|
|
}
|
|
|
|
public sealed class HomeAssistantAreaRegistryItem
|
|
{
|
|
[JsonPropertyName("area_id")]
|
|
public string AreaId { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("name")]
|
|
public string Name { get; set; } = string.Empty;
|
|
}
|
|
|
|
public sealed class HomeAssistantRegistries
|
|
{
|
|
public List<HomeAssistantDeviceRegistryItem> Devices { get; set; } = [];
|
|
public List<HomeAssistantEntityRegistryItem> Entities { get; set; } = [];
|
|
public List<HomeAssistantAreaRegistryItem> Areas { get; set; } = [];
|
|
public DateTimeOffset CachedAt { get; set; } = DateTimeOffset.UtcNow;
|
|
}
|
|
|
|
public sealed class HomeAssistantActionResult
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string EntityId { get; set; } = string.Empty;
|
|
public string Status { get; set; } = string.Empty;
|
|
public string Message { get; set; } = string.Empty;
|
|
}
|