Skip to content

Commit

Permalink
Merge pull request #17 from imalkevich/hadotnet-info-and-stats-clients
Browse files Browse the repository at this point in the history
HADotNet - adding Info and Stats clients
  • Loading branch information
qJake authored Jan 31, 2021
2 parents 5da45cd + 221fc4d commit b802b1d
Show file tree
Hide file tree
Showing 11 changed files with 687 additions and 88 deletions.
58 changes: 58 additions & 0 deletions HADotNet.Core.Tests/InfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Threading.Tasks;
using HADotNet.Core.Clients;
using NUnit.Framework;

namespace HADotNet.Core.Tests
{
public class InfoTests
{
private Uri Instance { get; set; }
private string ApiKey { get; set; }

[SetUp]
public void Setup()
{
Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance"));
ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey");

ClientFactory.Initialize(Instance, ApiKey);
}

[Test]
public async Task ShouldRetrieveSupervisorInfo()
{
var client = ClientFactory.GetClient<InfoClient>();

var info = await client.GetSupervisorInfo();

Assert.AreEqual("ok", info.Result);
Assert.IsNotNull(info.Data);
Assert.IsNotNull(info.Data.Version);
}

[Test]
public async Task ShouldRetrieveHostInfo()
{
var client = ClientFactory.GetClient<InfoClient>();

var info = await client.GetHostInfo();

Assert.AreEqual("ok", info.Result);
Assert.IsNotNull(info.Data);
Assert.IsNotNull(info.Data.OperatingSystem);
}

[Test]
public async Task ShouldRetrieveCoreInfo()
{
var client = ClientFactory.GetClient<InfoClient>();

var info = await client.GetCoreInfo();

Assert.AreEqual("ok", info.Result);
Assert.IsNotNull(info.Data);
Assert.IsNotNull(info.Data.Version);
}
}
}
44 changes: 44 additions & 0 deletions HADotNet.Core.Tests/StatsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using HADotNet.Core.Clients;
using NUnit.Framework;

namespace HADotNet.Core.Tests
{
public class StatsTests
{
private Uri Instance { get; set; }
private string ApiKey { get; set; }

[SetUp]
public void Setup()
{
Instance = new Uri(Environment.GetEnvironmentVariable("HADotNet:Tests:Instance"));
ApiKey = Environment.GetEnvironmentVariable("HADotNet:Tests:ApiKey");

ClientFactory.Initialize(Instance, ApiKey);
}

[Test]
public async Task ShouldRetrieveSupervisorStats()
{
var client = ClientFactory.GetClient<StatsClient>();

var stats = await client.GetSupervisorStats();

Assert.AreEqual("ok", stats.Result);
Assert.IsNotNull(stats.Data);
}

[Test]
public async Task ShouldRetrieveCoreStats()
{
var client = ClientFactory.GetClient<StatsClient>();

var stats = await client.GetCoreStats();

Assert.AreEqual("ok", stats.Result);
Assert.IsNotNull(stats.Data);
}
}
}
37 changes: 37 additions & 0 deletions HADotNet.Core/Clients/InfoClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using HADotNet.Core.Models;

namespace HADotNet.Core.Clients
{
/// <summary>
/// Provides access to the info API for retrieving information about Supervisor, Core and Host.
/// </summary>
public class InfoClient : BaseClient
{
/// <summary>
/// Initializes a new instance of the <see cref="InfoClient" />.
/// </summary>
/// <param name="instance">The Home Assistant base instance URL.</param>
/// <param name="apiKey">The Home Assistant long-lived access token.</param>
public InfoClient(Uri instance, string apiKey) : base(instance, apiKey) { }

/// <summary>
/// Retrieves Supervisor information.
/// </summary>
/// <returns>A <see cref="SupervisorInfoObject"/> representing Supervisor informatio.</returns>
public async Task<ResponseObject<SupervisorInfoObject>> GetSupervisorInfo() => await Get<ResponseObject<SupervisorInfoObject>>("/api/hassio/supervisor/info");

/// <summary>
/// Retrieves Host information.
/// </summary>
/// <returns>A <see cref="HostInfoObject"/> representing Host informatio.</returns>
public async Task<ResponseObject<HostInfoObject>> GetHostInfo() => await Get<ResponseObject<HostInfoObject>>("/api/hassio/host/info");

/// <summary>
/// Retrieves Core information.
/// </summary>
/// <returns>A <see cref="CoreInfoObject"/> representing Host informatio.</returns>
public async Task<ResponseObject<CoreInfoObject>> GetCoreInfo() => await Get<ResponseObject<CoreInfoObject>>("/api/hassio/core/info");
}
}
31 changes: 31 additions & 0 deletions HADotNet.Core/Clients/StatsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Threading.Tasks;
using HADotNet.Core.Models;

namespace HADotNet.Core.Clients
{
/// <summary>
/// Provides access to the info API for retrieving statistics about Supervisor and Core.
/// </summary>
public class StatsClient : BaseClient
{
/// <summary>
/// Initializes a new instance of the <see cref="StatsClient" />.
/// </summary>
/// <param name="instance">The Home Assistant base instance URL.</param>
/// <param name="apiKey">The Home Assistant long-lived access token.</param>
public StatsClient(Uri instance, string apiKey) : base(instance, apiKey) { }

/// <summary>
/// Retrieves Supervisor information.
/// </summary>
/// <returns>A <see cref="StatsObject"/> representing Supervisor stats.</returns>
public async Task<ResponseObject<StatsObject>> GetSupervisorStats() => await Get<ResponseObject<StatsObject>>("/api/hassio/supervisor/stats");

/// <summary>
/// Retrieves Core stats.
/// </summary>
/// <returns>A <see cref="StatsObject"/> representing Core stats.</returns>
public async Task<ResponseObject<StatsObject>> GetCoreStats() => await Get<ResponseObject<StatsObject>>("/api/hassio/core/stats");
}
}
70 changes: 70 additions & 0 deletions HADotNet.Core/Models/AddonObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Newtonsoft.Json;

namespace HADotNet.Core.Models
{
/// <summary>
/// Represents Add-on object.
/// </summary>
public class AddonObject
{
/// <summary>
/// Gets or sets the description.
/// </summary>
[JsonProperty("description")]
public string Description { get; set; }

/// <summary>
/// <code>True</code> if icon is available, otherwise <code>False</code>.
/// </summary>
[JsonProperty("icon")]
public bool Icon { get; set; }

/// <summary>
/// <code>True</code> if logo is available, otherwise <code>False</code>.
/// </summary>
[JsonProperty("logo")]
public bool Logo { get; set; }

/// <summary>
/// Gets or sets the name.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the repository.
/// </summary>
[JsonProperty("repository")]
public string Repository { get; set; }

/// <summary>
/// Gets or sets the slug.
/// </summary>
[JsonProperty("slug")]
public string Slug { get; set; }

/// <summary>
/// Gets or sets the state.
/// </summary>
[JsonProperty("state")]
public string State { get; set; }

/// <summary>
/// <code>True</code> if update is available, otherwise <code>False</code>.
/// </summary>
[JsonProperty("update_available")]
public bool UpdateAvailable { get; set; }

/// <summary>
/// Gets or sets the version.
/// </summary>
[JsonProperty("version")]
public string Version { get; set; }

/// <summary>
/// Gets or sets the latest version.
/// </summary>
[JsonProperty("version_latest")]
public string VersionLatest { get; set; }
}
}
100 changes: 100 additions & 0 deletions HADotNet.Core/Models/CoreInfoObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Newtonsoft.Json;

namespace HADotNet.Core.Models
{
/// <summary>
/// Represents Core info object.
/// </summary>
public class CoreInfoObject
{
/// <summary>
/// Gets or sets the processor architecture.
/// </summary>
[JsonProperty("arch")]
public string Arch { get; set; }

/// <summary>
/// Gets or sets the audio input.
/// </summary>
[JsonProperty("audio_input")]
public string AudioInput { get; set; }

/// <summary>
/// Gets or sets the audio output.
/// </summary>
[JsonProperty("audio_output")]
public string AudioOutput { get; set; }

/// <summary>
/// <code>True</code> if booted, otherwise <code>False</code>.
/// </summary>
[JsonProperty("boot")]
public bool Boot { get; set; }

/// <summary>
/// Gets or sets the image.
/// </summary>
[JsonProperty("image")]
public string Image { get; set; }

/// <summary>
/// Gets or sets the IP address.
/// </summary>
[JsonProperty("ip_address")]
public string IpAddress { get; set; }

/// <summary>
/// Gets or sets the last_version.
/// </summary>
[JsonProperty("last_version")]
public string LastVersion { get; set; }

/// <summary>
/// Gets or sets the machine.
/// </summary>
[JsonProperty("machine")]
public string Machine { get; set; }

/// <summary>
/// Gets or sets the port.
/// </summary>
[JsonProperty("port")]
public int Port { get; set; }

/// <summary>
/// <code>True</code> if SSL is enabled, otherwise <code>False</code>.
/// </summary>
[JsonProperty("ssl")]
public bool Ssl { get; set; }

/// <summary>
/// <code>True</code> if update is available, otherwise <code>False</code>.
/// </summary>
[JsonProperty("update_available")]
public bool UpdateAvailable { get; set; }

/// <summary>
/// Gets or sets the version.
/// </summary>
[JsonProperty("version")]
public string Version { get; set; }

/// <summary>
/// Gets or sets the latest version.
/// </summary>
[JsonProperty("version_latest")]
public string VersionLatest { get; set; }

/// <summary>
/// Gets or sets the wait boot timeout.
/// </summary>
[JsonProperty("wait_boot")]
public int WaitBoot { get; set; }

/// <summary>
/// <code>True</code> if watchdog is enabled, otherwise <code>False</code>.
/// </summary>
[JsonProperty("watchdog")]
public bool Watchdog { get; set; }
}
}
Loading

0 comments on commit b802b1d

Please sign in to comment.