Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
This commit is contained in:
@@ -7,9 +7,9 @@ using PowerMonitorManager.Models.Consts;
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Shared, deterministic datasets and builders used by the PowerMonitor tests.
|
||||
/// Keeping the builders and generated series here avoids copy-pasted setup and
|
||||
/// hard-coded clocks across test classes.
|
||||
/// Shared, deterministic datasets and builders used by the PowerMonitor tests.
|
||||
/// Keeping the builders and generated series here avoids copy-pasted setup and
|
||||
/// hard-coded clocks across test classes.
|
||||
/// </summary>
|
||||
internal static class PowerMonitorTestData
|
||||
{
|
||||
@@ -28,30 +28,38 @@ internal static class PowerMonitorTestData
|
||||
public static readonly DateTimeOffset FixedUtcNow = new(2026, 3, 17, 8, 0, 0, TimeSpan.Zero);
|
||||
|
||||
/// <summary>
|
||||
/// Two-device reading set used by the cost calculation tests.
|
||||
/// Two-device reading set used by the cost calculation tests.
|
||||
/// </summary>
|
||||
public static IReadOnlyDictionary<string, double> CreateCostDeviceReadings() => new Dictionary<string, double>
|
||||
public static IReadOnlyDictionary<string, double> CreateCostDeviceReadings()
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 101.0,
|
||||
["sensor.sonoff_10018909cb_power"] = 109.93
|
||||
};
|
||||
return new Dictionary<string, double>
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 101.0,
|
||||
["sensor.sonoff_10018909cb_power"] = 109.93
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Realistic seven-device reading set used by the entity projection tests.
|
||||
/// Realistic seven-device reading set used by the entity projection tests.
|
||||
/// </summary>
|
||||
private static Dictionary<string, double> CreateDeviceReadings() => new()
|
||||
private static Dictionary<string, double> CreateDeviceReadings()
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 8.55,
|
||||
["sensor.sonoff_10018909cb_power"] = 36.93,
|
||||
["sensor.tv_power"] = 101.0,
|
||||
["sensor.akvariumas_power"] = 21.0,
|
||||
["sensor.sonoff_1002230a48_power"] = 20.06,
|
||||
["sensor.temp_power"] = 14.0,
|
||||
["sensor.balkony_laps_power"] = 9.39
|
||||
};
|
||||
return new Dictionary<string, double>
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 8.55,
|
||||
["sensor.sonoff_10018909cb_power"] = 36.93,
|
||||
["sensor.tv_power"] = 101.0,
|
||||
["sensor.akvariumas_power"] = 21.0,
|
||||
["sensor.sonoff_1002230a48_power"] = 20.06,
|
||||
["sensor.temp_power"] = 14.0,
|
||||
["sensor.balkony_laps_power"] = 9.39
|
||||
};
|
||||
}
|
||||
|
||||
public static IReadOnlyDictionary<string, double> CreateReadings(params (string EntityId, double Watts)[] readings) =>
|
||||
readings.ToDictionary(r => r.EntityId, r => r.Watts);
|
||||
public static IReadOnlyDictionary<string, double> CreateReadings(params (string EntityId, double Watts)[] readings)
|
||||
{
|
||||
return readings.ToDictionary(r => r.EntityId, r => r.Watts);
|
||||
}
|
||||
|
||||
public static PowerCalculationResult CreateCalculation(
|
||||
double totalWatts = DefaultTotalWatts,
|
||||
@@ -77,14 +85,17 @@ internal static class PowerMonitorTestData
|
||||
double cost1Hour = 0.004,
|
||||
double costDaily = 0.03,
|
||||
double costMonthly = 0.9,
|
||||
bool partialWindow = true) => new()
|
||||
bool partialWindow = true)
|
||||
{
|
||||
Cost15Min = cost15Min,
|
||||
Cost1Hour = cost1Hour,
|
||||
CostDaily = costDaily,
|
||||
CostMonthly = costMonthly,
|
||||
PartialWindow = partialWindow
|
||||
};
|
||||
return new CostSnapshot
|
||||
{
|
||||
Cost15Min = cost15Min,
|
||||
Cost1Hour = cost1Hour,
|
||||
CostDaily = costDaily,
|
||||
CostMonthly = costMonthly,
|
||||
PartialWindow = partialWindow
|
||||
};
|
||||
}
|
||||
|
||||
public static PowerUpdateContext CreateUpdateContext(
|
||||
bool isAnomaly = false,
|
||||
@@ -98,10 +109,10 @@ internal static class PowerMonitorTestData
|
||||
var readings = deviceReadings ?? CreateDeviceReadings();
|
||||
var deviceCosts = readings
|
||||
.Select((reading, index) => (reading.Key, Snapshot: CreateCostSnapshot(
|
||||
cost15Min: 0.0005 * (index + 1),
|
||||
cost1Hour: 0.002 * (index + 1),
|
||||
costDaily: 0.03 * (index + 1),
|
||||
costMonthly: 0.9 * (index + 1))))
|
||||
0.0005 * (index + 1),
|
||||
0.002 * (index + 1),
|
||||
0.03 * (index + 1),
|
||||
0.9 * (index + 1))))
|
||||
.ToDictionary(entry => entry.Key, entry => entry.Snapshot);
|
||||
|
||||
return new PowerUpdateContext
|
||||
@@ -135,8 +146,9 @@ internal static class PowerMonitorTestData
|
||||
return options;
|
||||
}
|
||||
|
||||
public static PowerMonitorOptions CreateLimitEnforcementOptions(Action<PowerMonitorOptions>? configure = null) =>
|
||||
CreateOptions(options =>
|
||||
public static PowerMonitorOptions CreateLimitEnforcementOptions(Action<PowerMonitorOptions>? configure = null)
|
||||
{
|
||||
return CreateOptions(options =>
|
||||
{
|
||||
options.EnforceLimitEnabled = true;
|
||||
options.PowerLimitWatts = PowerMonitorConsts.DefaultPowerLimitWatts;
|
||||
@@ -146,18 +158,22 @@ internal static class PowerMonitorTestData
|
||||
options.PauseAndRestoreEntityIds = [AutoManagementEntityId];
|
||||
configure?.Invoke(options);
|
||||
});
|
||||
}
|
||||
|
||||
public static PowerSensorInfo CreateSensor(
|
||||
string entityId,
|
||||
string? friendlyName = null,
|
||||
string? deviceName = null,
|
||||
string unitOfMeasurement = PowerMonitorConsts.WattUnit) => new()
|
||||
string unitOfMeasurement = PowerMonitorConsts.WattUnit)
|
||||
{
|
||||
EntityId = entityId,
|
||||
FriendlyName = friendlyName ?? entityId,
|
||||
DeviceName = deviceName ?? PowerSensorDiscoveryService.ExtractDeviceName(entityId),
|
||||
UnitOfMeasurement = unitOfMeasurement
|
||||
};
|
||||
return new PowerSensorInfo
|
||||
{
|
||||
EntityId = entityId,
|
||||
FriendlyName = friendlyName ?? entityId,
|
||||
DeviceName = deviceName ?? PowerSensorDiscoveryService.ExtractDeviceName(entityId),
|
||||
UnitOfMeasurement = unitOfMeasurement
|
||||
};
|
||||
}
|
||||
|
||||
public static JsonElement CreateAttributesJson(
|
||||
string? deviceClass,
|
||||
@@ -166,42 +182,39 @@ internal static class PowerMonitorTestData
|
||||
{
|
||||
var attributes = new Dictionary<string, object?>();
|
||||
|
||||
if (deviceClass is not null)
|
||||
{
|
||||
attributes["device_class"] = deviceClass;
|
||||
}
|
||||
if (deviceClass is not null) attributes["device_class"] = deviceClass;
|
||||
|
||||
if (unitOfMeasurement is not null)
|
||||
{
|
||||
attributes["unit_of_measurement"] = unitOfMeasurement;
|
||||
}
|
||||
if (unitOfMeasurement is not null) attributes["unit_of_measurement"] = unitOfMeasurement;
|
||||
|
||||
if (friendlyName is not null)
|
||||
{
|
||||
attributes["friendly_name"] = friendlyName;
|
||||
}
|
||||
if (friendlyName is not null) attributes["friendly_name"] = friendlyName;
|
||||
|
||||
return JsonSerializer.SerializeToElement(attributes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deterministic, strictly positive and monotonically increasing power samples
|
||||
/// used by the looping scenarios.
|
||||
/// Deterministic, strictly positive and monotonically increasing power samples
|
||||
/// used by the looping scenarios.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<double> CreatePowerSampleSeries(int count = 96) =>
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(50.0 + i * 37.5 + i % 7 * 0.5, 2))
|
||||
];
|
||||
public static IReadOnlyList<double> CreatePowerSampleSeries(int count = 96)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(50.0 + i * 37.5 + i % 7 * 0.5, 2))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deterministic, strictly positive hourly tariff series (EUR/kWh).
|
||||
/// Deterministic, strictly positive hourly tariff series (EUR/kWh).
|
||||
/// </summary>
|
||||
private static IReadOnlyList<double> CreateTariffPriceSeries(int count = 24) =>
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(0.08 + i * 0.03 + i % 5 * 0.0125, 4))
|
||||
];
|
||||
private static IReadOnlyList<double> CreateTariffPriceSeries(int count = 24)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(0.08 + i * 0.03 + i % 5 * 0.0125, 4))
|
||||
];
|
||||
}
|
||||
|
||||
public static IReadOnlyList<(DateTime Start, double PricePerKwh)> CreateHourlyTariffWindows(DateTime dayStart)
|
||||
{
|
||||
@@ -214,9 +227,12 @@ internal static class PowerMonitorTestData
|
||||
];
|
||||
}
|
||||
|
||||
public static IReadOnlyList<DateTime> CreateSampleTimestamps(DateTime start, int count, int intervalMinutes = 15) =>
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => start.AddMinutes(i * intervalMinutes))
|
||||
];
|
||||
}
|
||||
public static IReadOnlyList<DateTime> CreateSampleTimestamps(DateTime start, int count, int intervalMinutes = 15)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => start.AddMinutes(i * intervalMinutes))
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user