Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them - Add boiler run-time tracker abstraction (implementation pending) - Drop unused NordPool init/update-result types and stale PLAN.md - Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
@@ -9,6 +9,6 @@ public interface IPowerMonitorEntityService
|
||||
Task CreateDeviceEntitiesAsync(IReadOnlyList<PowerSensorInfo> sensors, CancellationToken cancellationToken = default);
|
||||
Task UpdateEntitiesAsync(PowerUpdateContext context, CancellationToken cancellationToken = default);
|
||||
Task ResetEntitiesAsync(CancellationToken cancellationToken = default);
|
||||
Task CleanupEntitiesAsync(CancellationToken cancellationToken = default);
|
||||
Task CleanupDeviceEntitiesAsync(IReadOnlyList<string> removedDeviceNames, CancellationToken cancellationToken = default);
|
||||
Task RemoveSystemEntitiesAsync(CancellationToken cancellationToken = default);
|
||||
Task RemoveDeviceEntitiesAsync(IReadOnlyList<string> removedDeviceNames, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public sealed class PowerMonitorOptions
|
||||
{
|
||||
public const string SectionName = "PowerMonitor";
|
||||
|
||||
public bool Enabled { get; set; } = true;
|
||||
public bool Enabled { get; set; }
|
||||
public string MainMeterEntityId { get; set; } = string.Empty;
|
||||
public int PollIntervalSeconds { get; set; } = PowerMonitorConsts.DefaultPollIntervalSeconds;
|
||||
public int SmoothingWindowSize { get; set; } = PowerMonitorConsts.DefaultSmoothingWindowSize;
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.5" />
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.11.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="11.0.0" />
|
||||
<PackageReference Include="NetDaemon.HassModel" Version="26.43.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -64,7 +64,7 @@ public sealed class PowerMonitorEntityService(
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var entities = sensors.SelectMany(GetDeviceEntities).ToList();
|
||||
var entities = sensors.SelectMany(GetPowerDeviceEntitiesNames).ToList();
|
||||
var tasks = entities.Select(async entity =>
|
||||
{
|
||||
try
|
||||
@@ -139,14 +139,16 @@ public sealed class PowerMonitorEntityService(
|
||||
logger.LogInformation("All PowerMonitor entity states reset to defaults");
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
public async Task RemoveSystemEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
try
|
||||
{
|
||||
var tasks = SystemEntityIds.Select(id => entityManager.RemoveEntity(id));
|
||||
var tasks = SystemEntityIds.Select(entityManager.RemoveEntity);
|
||||
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("PowerMonitor system entities cleaned up");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -156,16 +158,19 @@ public sealed class PowerMonitorEntityService(
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CleanupDeviceEntitiesAsync(IReadOnlyList<string> removedDeviceNames, CancellationToken cancellationToken = default)
|
||||
public async Task RemoveDeviceEntitiesAsync(IReadOnlyList<string> removedDeviceNames, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var entityIds = removedDeviceNames.SelectMany(GetDeviceCostEntityIds).ToList();
|
||||
|
||||
var tasks = entityIds.Select(async id =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await entityManager.RemoveEntity(id).ConfigureAwait(false);
|
||||
|
||||
logger.LogDebug("Entity removed: {EntityId}", id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -175,6 +180,7 @@ public sealed class PowerMonitorEntityService(
|
||||
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
internal ICollection<PowerMonitorEntity> GetSystemEntitiesList()
|
||||
{
|
||||
@@ -222,27 +228,27 @@ public sealed class PowerMonitorEntityService(
|
||||
};
|
||||
}
|
||||
|
||||
internal static IEnumerable<PowerMonitorEntity> GetDeviceEntities(PowerSensorInfo sensor)
|
||||
internal static IEnumerable<PowerMonitorEntity> GetPowerDeviceEntitiesNames(PowerSensorInfo sensor)
|
||||
{
|
||||
var deviceName = PowerSensorDiscoveryService.ExtractDeviceName(sensor.EntityId);
|
||||
return new[]
|
||||
{
|
||||
return
|
||||
[
|
||||
CreateCostEntity($"powermonitor_{deviceName}_cost_15min", $"{sensor.FriendlyName} cost (15 min)"),
|
||||
CreateCostEntity($"powermonitor_{deviceName}_cost_1h", $"{sensor.FriendlyName} cost (1h)"),
|
||||
CreateCostEntity($"powermonitor_{deviceName}_cost_daily", $"{sensor.FriendlyName} cost (daily)"),
|
||||
CreateCostEntity($"powermonitor_{deviceName}_cost_monthly", $"{sensor.FriendlyName} cost (monthly)")
|
||||
};
|
||||
];
|
||||
}
|
||||
|
||||
internal static IEnumerable<string> GetDeviceCostEntityIds(string deviceName)
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
return
|
||||
[
|
||||
$"sensor.powermonitor_{deviceName}_cost_15min",
|
||||
$"sensor.powermonitor_{deviceName}_cost_1h",
|
||||
$"sensor.powermonitor_{deviceName}_cost_daily",
|
||||
$"sensor.powermonitor_{deviceName}_cost_monthly"
|
||||
};
|
||||
];
|
||||
}
|
||||
|
||||
private async Task UpdateTotalPowerAsync(PowerUpdateContext context, string timestamp)
|
||||
|
||||
@@ -5,6 +5,7 @@ using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using NordPool.Models;
|
||||
using PowerMonitor.Models;
|
||||
using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
@@ -60,10 +61,22 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
{
|
||||
try
|
||||
{
|
||||
await InitializeAsync();
|
||||
SubscribeToMainMeterChanges();
|
||||
SubscribeToRediscoverButton();
|
||||
SubscribeToLimitBreachDebugButton();
|
||||
if (_options.Enabled)
|
||||
{
|
||||
await InitializeAsync();
|
||||
SubscribeToMainMeterChanges();
|
||||
SubscribeToRediscoverButton();
|
||||
SubscribeToLimitBreachDebugButton();
|
||||
}
|
||||
else
|
||||
{
|
||||
await CleanupEntitiesAsync();
|
||||
|
||||
var sensoresToRemove = GetPowerSensorEntitiesNames();
|
||||
|
||||
await _entityService.RemoveDeviceEntitiesAsync([.. sensoresToRemove.Select(s => s.DeviceName)]);
|
||||
await _entityService.RemoveSystemEntitiesAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -82,7 +95,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
var sensors = _discoveryService.DiscoverDeviceSensors();
|
||||
var sensors = GetPowerSensorEntitiesNames();
|
||||
await _entityService.CreateDeviceEntitiesAsync(sensors);
|
||||
|
||||
_logger.LogInformation("PowerMonitor initialized with {SensorCount} tracked sensors", sensors.Count);
|
||||
@@ -96,9 +109,16 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyList<PowerSensorInfo> GetPowerSensorEntitiesNames()
|
||||
{
|
||||
var sensors = _discoveryService.DiscoverDeviceSensors();
|
||||
|
||||
return sensors;
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _entityService.CleanupEntitiesAsync(cancellationToken);
|
||||
await _entityService.RemoveSystemEntitiesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private void SubscribeToMainMeterChanges()
|
||||
@@ -175,9 +195,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
|
||||
if (removed.Count > 0)
|
||||
{
|
||||
var removedNames = removed.Select(s => s.DeviceName).ToList();
|
||||
await _entityService.CleanupDeviceEntitiesAsync(removedNames);
|
||||
_logger.LogInformation("Removed entities for {Count} sensors", removed.Count);
|
||||
await RemoreDevicePriceEntities(removed);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Re-discovery complete: {Total} sensors ({Added} added, {Removed} removed)", newSensors.Count, added.Count, removed.Count);
|
||||
@@ -188,6 +206,14 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RemoreDevicePriceEntities(List<PowerSensorInfo> removed)
|
||||
{
|
||||
var removedNames = removed.Select(s => s.DeviceName).ToList();
|
||||
await _entityService.RemoveDeviceEntitiesAsync(removedNames);
|
||||
|
||||
_logger.LogInformation("Removed entities for {Count} sensors", removed.Count);
|
||||
}
|
||||
|
||||
private async Task PollAndUpdateAsync()
|
||||
{
|
||||
try
|
||||
|
||||
@@ -14,11 +14,6 @@ public static class PowerMonitorServiceRegistration
|
||||
.GetSection(PowerMonitorOptions.SectionName)
|
||||
.Get<PowerMonitorOptions>() ?? new PowerMonitorOptions();
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return services;
|
||||
}
|
||||
|
||||
services
|
||||
.AddNetDaemonApp<PowerMonitorInit>()
|
||||
.AddScoped<IPowerSensorDiscoveryService, PowerSensorDiscoveryService>()
|
||||
|
||||
Reference in New Issue
Block a user