feat: add PowerMonitor service registration and default configuration tests
This commit is contained in:
@@ -27,10 +27,11 @@
|
|||||||
"UseAttributeBaseClasses" : "false"
|
"UseAttributeBaseClasses" : "false"
|
||||||
},
|
},
|
||||||
"PowerMonitor": {
|
"PowerMonitor": {
|
||||||
|
"Enabled": false,
|
||||||
"MainMeterEntityId": "sensor.sonoff_100267d63f_power",
|
"MainMeterEntityId": "sensor.sonoff_100267d63f_power",
|
||||||
"PollIntervalSeconds": 1,
|
"PollIntervalSeconds": 1,
|
||||||
"SmoothingWindowSize": 10,
|
"SmoothingWindowSize": 10,
|
||||||
"AnomalyThresholdWatts": 500,
|
"AnomalyThresholdWatts": 500,
|
||||||
"ExcludedSensorPatterns": [
|
"ExcludedSensorPatterns": [
|
||||||
"sensor.nordpool_*",
|
"sensor.nordpool_*",
|
||||||
"sensor.powermonitor_*"
|
"sensor.powermonitor_*"
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ using NetDaemon.Runtime;
|
|||||||
using NordPoolManager;
|
using NordPoolManager;
|
||||||
using NordPoolManager.Interface;
|
using NordPoolManager.Interface;
|
||||||
using PowerMonitorManager;
|
using PowerMonitorManager;
|
||||||
using PowerMonitorManager.Interface;
|
|
||||||
using PowerMonitorManager.Models;
|
using PowerMonitorManager.Models;
|
||||||
|
|
||||||
#pragma warning disable CA1812
|
#pragma warning disable CA1812
|
||||||
@@ -49,11 +48,7 @@ try
|
|||||||
.AddSingleton<IBoilerControlService, BoilerControlService>()
|
.AddSingleton<IBoilerControlService, BoilerControlService>()
|
||||||
.Configure<PowerMonitorOptions>(
|
.Configure<PowerMonitorOptions>(
|
||||||
context.Configuration.GetSection(PowerMonitorOptions.SectionName))
|
context.Configuration.GetSection(PowerMonitorOptions.SectionName))
|
||||||
.AddNetDaemonApp<PowerMonitorInit>()
|
.AddPowerMonitor(context.Configuration)
|
||||||
.AddScoped<IPowerSensorDiscoveryService, PowerSensorDiscoveryService>()
|
|
||||||
.AddSingleton<IOtherPowerCalculationService, OtherPowerCalculationService>()
|
|
||||||
.AddSingleton<IPowerCostCalculationService, PowerCostCalculationService>()
|
|
||||||
.AddTransient<IPowerMonitorEntityService, PowerMonitorEntityService>()
|
|
||||||
)
|
)
|
||||||
.Build()
|
.Build()
|
||||||
.RunAsync()
|
.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 const string SectionName = "PowerMonitor";
|
||||||
|
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
public string MainMeterEntityId { get; set; } = string.Empty;
|
public string MainMeterEntityId { get; set; } = string.Empty;
|
||||||
public int PollIntervalSeconds { get; set; } = PowerMonitorConsts.DefaultPollIntervalSeconds;
|
public int PollIntervalSeconds { get; set; } = PowerMonitorConsts.DefaultPollIntervalSeconds;
|
||||||
public int SmoothingWindowSize { get; set; } = PowerMonitorConsts.DefaultSmoothingWindowSize;
|
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