feat: add PowerMonitor service registration and default configuration tests
This commit is contained in:
@@ -27,10 +27,11 @@
|
||||
"UseAttributeBaseClasses" : "false"
|
||||
},
|
||||
"PowerMonitor": {
|
||||
"Enabled": false,
|
||||
"MainMeterEntityId": "sensor.sonoff_100267d63f_power",
|
||||
"PollIntervalSeconds": 1,
|
||||
"SmoothingWindowSize": 10,
|
||||
"AnomalyThresholdWatts": 500,
|
||||
"AnomalyThresholdWatts": 500,
|
||||
"ExcludedSensorPatterns": [
|
||||
"sensor.nordpool_*",
|
||||
"sensor.powermonitor_*"
|
||||
|
||||
@@ -15,7 +15,6 @@ using NetDaemon.Runtime;
|
||||
using NordPoolManager;
|
||||
using NordPoolManager.Interface;
|
||||
using PowerMonitorManager;
|
||||
using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
#pragma warning disable CA1812
|
||||
@@ -49,11 +48,7 @@ try
|
||||
.AddSingleton<IBoilerControlService, BoilerControlService>()
|
||||
.Configure<PowerMonitorOptions>(
|
||||
context.Configuration.GetSection(PowerMonitorOptions.SectionName))
|
||||
.AddNetDaemonApp<PowerMonitorInit>()
|
||||
.AddScoped<IPowerSensorDiscoveryService, PowerSensorDiscoveryService>()
|
||||
.AddSingleton<IOtherPowerCalculationService, OtherPowerCalculationService>()
|
||||
.AddSingleton<IPowerCostCalculationService, PowerCostCalculationService>()
|
||||
.AddTransient<IPowerMonitorEntityService, PowerMonitorEntityService>()
|
||||
.AddPowerMonitor(context.Configuration)
|
||||
)
|
||||
.Build()
|
||||
.RunAsync()
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using FluentAssertions;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class PowerMonitorOptionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Defaults_ShouldHaveEnabledTrue()
|
||||
{
|
||||
var options = new PowerMonitorOptions();
|
||||
|
||||
options.Enabled.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Defaults_ShouldHaveExpectedPollInterval()
|
||||
{
|
||||
var options = new PowerMonitorOptions();
|
||||
|
||||
options.PollIntervalSeconds.Should().Be(PowerMonitorConsts.DefaultPollIntervalSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Defaults_ShouldHaveExpectedSmoothingWindowSize()
|
||||
{
|
||||
var options = new PowerMonitorOptions();
|
||||
|
||||
options.SmoothingWindowSize.Should().Be(PowerMonitorConsts.DefaultSmoothingWindowSize);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Defaults_ShouldHaveExpectedAnomalyThreshold()
|
||||
{
|
||||
var options = new PowerMonitorOptions();
|
||||
|
||||
options.AnomalyThresholdWatts.Should().Be(PowerMonitorConsts.DefaultAnomalyThresholdWatts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Defaults_ShouldHaveEmptyMainMeterEntityId()
|
||||
{
|
||||
var options = new PowerMonitorOptions();
|
||||
|
||||
options.MainMeterEntityId.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Defaults_ShouldHaveEmptyExcludedSensorPatterns()
|
||||
{
|
||||
var options = new PowerMonitorOptions();
|
||||
|
||||
options.ExcludedSensorPatterns.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SectionName_ShouldBePowerMonitor()
|
||||
{
|
||||
PowerMonitorOptions.SectionName.Should().Be("PowerMonitor");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Enabled_WhenSetToFalse_ShouldReturnFalse()
|
||||
{
|
||||
var options = new PowerMonitorOptions { Enabled = false };
|
||||
|
||||
options.Enabled.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ public sealed class PowerMonitorOptions
|
||||
{
|
||||
public const string SectionName = "PowerMonitor";
|
||||
|
||||
public bool Enabled { get; set; } = true;
|
||||
public string MainMeterEntityId { get; set; } = string.Empty;
|
||||
public int PollIntervalSeconds { get; set; } = PowerMonitorConsts.DefaultPollIntervalSeconds;
|
||||
public int SmoothingWindowSize { get; set; } = PowerMonitorConsts.DefaultSmoothingWindowSize;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetDaemon.AppModel;
|
||||
using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitorManager;
|
||||
|
||||
public static class PowerMonitorServiceRegistration
|
||||
{
|
||||
public static IServiceCollection AddPowerMonitor(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var options = configuration
|
||||
.GetSection(PowerMonitorOptions.SectionName)
|
||||
.Get<PowerMonitorOptions>() ?? new PowerMonitorOptions();
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return services;
|
||||
}
|
||||
|
||||
services
|
||||
.AddNetDaemonApp<PowerMonitorInit>()
|
||||
.AddScoped<IPowerSensorDiscoveryService, PowerSensorDiscoveryService>()
|
||||
.AddSingleton<IOtherPowerCalculationService, OtherPowerCalculationService>()
|
||||
.AddSingleton<IPowerCostCalculationService, PowerCostCalculationService>()
|
||||
.AddTransient<IPowerMonitorEntityService, PowerMonitorEntityService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user