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:
@@ -1,4 +1,3 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -22,168 +21,258 @@ public class PowerSensorDiscoveryServiceTests
|
||||
}
|
||||
|
||||
private PowerSensorDiscoveryService CreateSut(
|
||||
string mainMeterEntityId = "sensor.sonoff_100267d63f_power",
|
||||
string mainMeterEntityId = PowerMonitorTestData.MainMeterEntityId,
|
||||
string[]? excludedPatterns = null)
|
||||
{
|
||||
var options = Options.Create(new PowerMonitorOptions
|
||||
var options = Options.Create(PowerMonitorTestData.CreateOptions(option =>
|
||||
{
|
||||
MainMeterEntityId = mainMeterEntityId,
|
||||
ExcludedSensorPatterns = excludedPatterns ?? ["sensor.nordpool_*", "sensor.powermonitor_*"]
|
||||
});
|
||||
option.MainMeterEntityId = mainMeterEntityId;
|
||||
option.ExcludedSensorPatterns = excludedPatterns ?? ["sensor.nordpool_*", "sensor.powermonitor_*"];
|
||||
}));
|
||||
return new PowerSensorDiscoveryService(_haContextMock.Object, options, _loggerMock.Object);
|
||||
}
|
||||
|
||||
private void SetupEntities(params (string entityId, string? deviceClass, string? unit, string? friendlyName)[] entities)
|
||||
private Entity SetupEntity(string entityId, string? deviceClass, string? unit, string? friendlyName)
|
||||
{
|
||||
var entityList = new List<Entity>();
|
||||
|
||||
foreach (var (entityId, deviceClass, unit, friendlyName) in entities)
|
||||
var entityState = new EntityState
|
||||
{
|
||||
var attributes = new Dictionary<string, object?>();
|
||||
if (deviceClass is not null)
|
||||
{
|
||||
attributes["device_class"] = deviceClass;
|
||||
}
|
||||
AttributesJson = PowerMonitorTestData.CreateAttributesJson(deviceClass, unit, friendlyName)
|
||||
};
|
||||
|
||||
if (unit is not null)
|
||||
{
|
||||
attributes["unit_of_measurement"] = unit;
|
||||
}
|
||||
|
||||
if (friendlyName is not null)
|
||||
{
|
||||
attributes["friendly_name"] = friendlyName;
|
||||
}
|
||||
|
||||
var attributesJson = JsonSerializer.SerializeToElement(attributes);
|
||||
var entityState = new EntityState { AttributesJson = attributesJson };
|
||||
|
||||
_haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
|
||||
|
||||
var entity = new Entity(_haContextMock.Object, entityId);
|
||||
entityList.Add(entity);
|
||||
}
|
||||
|
||||
_haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList);
|
||||
_haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
|
||||
return new Entity(_haContextMock.Object, entityId);
|
||||
}
|
||||
|
||||
private void SetupEntities(
|
||||
params (string EntityId, string? DeviceClass, string? Unit, string? FriendlyName)[] entities) =>
|
||||
_haContextMock
|
||||
.Setup(h => h.GetAllEntities())
|
||||
.Returns(entities.Select(entity => SetupEntity(entity.EntityId, entity.DeviceClass, entity.Unit, entity.FriendlyName)).ToList());
|
||||
|
||||
private void SetupEntitiesWithoutState(params string[] entityIds) =>
|
||||
_haContextMock
|
||||
.Setup(h => h.GetAllEntities())
|
||||
.Returns(entityIds.Select(entityId => new Entity(_haContextMock.Object, entityId)).ToList());
|
||||
|
||||
#region Discovery
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_ShouldFindPowerSensors()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
|
||||
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
||||
("sensor.sonoff_100215c534_power", "power", "W", "AC Power"),
|
||||
("sensor.sonoff_10018909cb_power", "power", "W", "Induction Power"),
|
||||
("sensor.temperature", "temperature", "°C", "Temperature")
|
||||
);
|
||||
("sensor.temperature", "temperature", "°C", "Temperature"));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(2);
|
||||
result.Should().Contain(s => s.EntityId == "sensor.sonoff_100215c534_power");
|
||||
result.Should().Contain(s => s.EntityId == "sensor.sonoff_10018909cb_power");
|
||||
result.Should().Contain(sensor => sensor.EntityId == "sensor.sonoff_100215c534_power");
|
||||
result.Should().Contain(sensor => sensor.EntityId == "sensor.sonoff_10018909cb_power");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_ShouldExcludeMainMeter()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
|
||||
("sensor.tv_power", "power", "W", "TV Power")
|
||||
);
|
||||
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
||||
("sensor.tv_power", "power", "W", "TV Power"));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(1);
|
||||
result.Should().NotContain(s => s.EntityId == "sensor.sonoff_100267d63f_power");
|
||||
result.Should().NotContain(sensor => sensor.EntityId == PowerMonitorTestData.MainMeterEntityId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_WhenMainMeterNotConfigured_ShouldIncludeAllPowerSensors()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
||||
("sensor.tv_power", "power", "W", "TV Power"));
|
||||
|
||||
var sut = CreateSut(mainMeterEntityId: string.Empty);
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(2);
|
||||
result.Should().Contain(sensor => sensor.EntityId == PowerMonitorTestData.MainMeterEntityId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_ShouldApplyExclusionPatterns()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
|
||||
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
||||
("sensor.tv_power", "power", "W", "TV Power"),
|
||||
("sensor.nordpool_some_power", "power", "W", "NordPool"),
|
||||
("sensor.powermonitor_other_power", "power", "W", "PowerMonitor")
|
||||
);
|
||||
("sensor.powermonitor_other_power", "power", "W", "PowerMonitor"));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(1);
|
||||
result.Should().Contain(s => s.EntityId == "sensor.tv_power");
|
||||
result.Should().Contain(sensor => sensor.EntityId == "sensor.tv_power");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_ShouldHandleKilowattUnit()
|
||||
public void DiscoverDeviceSensors_WhenNoExclusionPatternsConfigured_ShouldKeepMatchingSensors()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
|
||||
("sensor.sonoff_1002230e32_power", "power", "kW", "Boiler Power")
|
||||
);
|
||||
("sensor.tv_power", "power", "W", "TV Power"),
|
||||
("sensor.powermonitor_other_power", "power", "W", "PowerMonitor"));
|
||||
|
||||
var sut = CreateSut();
|
||||
var sut = CreateSut(excludedPatterns: []);
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().EntityId.Should().Be("sensor.sonoff_1002230e32_power");
|
||||
result.First().UnitOfMeasurement.Should().Be("kW");
|
||||
// Assert
|
||||
result.Should().HaveCount(2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("sensor.tv_power", "TV Power", "W", "TV Power", "W", "tv")]
|
||||
[InlineData("sensor.boiler_power", "Boiler Power", "kW", "Boiler Power", "kW", "boiler")]
|
||||
[InlineData("sensor.mystery_power", null, "W", "sensor.mystery_power", "W", "mystery")]
|
||||
public void DiscoverDeviceSensors_ShouldExposeSensorMetadata(
|
||||
string entityId,
|
||||
string? friendlyName,
|
||||
string unit,
|
||||
string expectedFriendlyName,
|
||||
string expectedUnit,
|
||||
string expectedDeviceName)
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities((entityId, "power", unit, friendlyName));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().ContainSingle();
|
||||
var sensor = result.Single();
|
||||
sensor.EntityId.Should().Be(entityId);
|
||||
sensor.FriendlyName.Should().Be(expectedFriendlyName);
|
||||
sensor.UnitOfMeasurement.Should().Be(expectedUnit);
|
||||
sensor.DeviceName.Should().Be(expectedDeviceName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_WhenNoPowerSensors_ShouldReturnEmpty()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
("sensor.temperature", "temperature", "°C", "Temp"),
|
||||
("switch.some_switch", null, null, "Switch")
|
||||
);
|
||||
("switch.some_switch", null, null, "Switch"),
|
||||
("sensor.energy_usage", "energy", "kWh", "Energy"));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_WhenEntityStateMissing_ShouldSkipEntity()
|
||||
{
|
||||
// Arrange - HA can report entities whose state has not been published yet.
|
||||
var tv = SetupEntity("sensor.tv_power", "power", "W", "TV Power");
|
||||
_haContextMock
|
||||
.Setup(h => h.GetAllEntities())
|
||||
.Returns(new List<Entity> { new(_haContextMock.Object, "sensor.ghost_power"), tv });
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().ContainSingle();
|
||||
result.Single().EntityId.Should().Be("sensor.tv_power");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_WhenHaReportsDuplicateEntities_ShouldMirrorTheEntityList()
|
||||
{
|
||||
// Arrange - discovery is a projection of HA's entity list; de-duplication is not its job.
|
||||
SetupEntities(
|
||||
("sensor.tv_power", "power", "W", "TV Power"),
|
||||
("sensor.tv_power", "power", "W", "TV Power"));
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(2);
|
||||
result.Select(sensor => sensor.DeviceName).Should().OnlyContain(name => name == "tv");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_ShouldUpdateCurrentDeviceSensors()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
|
||||
("sensor.akvariumas_power", "power", "W", "Akvariumas Power")
|
||||
);
|
||||
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
||||
("sensor.akvariumas_power", "power", "W", "Akvariumas Power"));
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.CurrentDeviceSensors.Should().BeEmpty();
|
||||
|
||||
// Act
|
||||
sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
sut.CurrentDeviceSensors.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverDeviceSensors_ReDiscovery_ShouldReplacePreviousList()
|
||||
{
|
||||
// Arrange
|
||||
SetupEntities(
|
||||
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
|
||||
("sensor.sonoff_100215c534_power", "power", "W", "AC Power")
|
||||
);
|
||||
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
||||
("sensor.sonoff_100215c534_power", "power", "W", "AC Power"));
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.DiscoverDeviceSensors();
|
||||
sut.CurrentDeviceSensors.Should().HaveCount(1);
|
||||
|
||||
SetupEntities(
|
||||
("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"),
|
||||
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
||||
("sensor.sonoff_100215c534_power", "power", "W", "AC Power"),
|
||||
("sensor.sonoff_10018909cb_power", "power", "W", "Induction Power")
|
||||
);
|
||||
("sensor.sonoff_10018909cb_power", "power", "W", "Induction Power"));
|
||||
|
||||
// Act
|
||||
sut.DiscoverDeviceSensors();
|
||||
|
||||
// Assert
|
||||
sut.CurrentDeviceSensors.Should().HaveCount(2);
|
||||
}
|
||||
|
||||
@@ -197,6 +286,11 @@ public class PowerSensorDiscoveryServiceTests
|
||||
[InlineData("sensor.tv_power", "tv")]
|
||||
[InlineData("sensor.temp_power", "temp")]
|
||||
[InlineData("sensor.power", "power")]
|
||||
[InlineData("sensor.TV_POWER", "TV")]
|
||||
[InlineData("sensor.boiler_output_power", "boiler_output")]
|
||||
[InlineData("sensor.temperature", "temperature")]
|
||||
[InlineData("tv_power", "tv")]
|
||||
[InlineData("sensor.", "")]
|
||||
public void ExtractDeviceName_ShouldStripDomainAndPowerSuffix(string entityId, string expected)
|
||||
{
|
||||
PowerSensorDiscoveryService.ExtractDeviceName(entityId).Should().Be(expected);
|
||||
@@ -209,8 +303,14 @@ public class PowerSensorDiscoveryServiceTests
|
||||
[Theory]
|
||||
[InlineData("sensor.nordpool_*", "sensor.nordpool_current_price", true)]
|
||||
[InlineData("sensor.nordpool_*", "sensor.tv_power", false)]
|
||||
[InlineData("sensor.nordpool_*", "SENSOR.NORDPOOL_CURRENT_PRICE", true)]
|
||||
[InlineData("sensor.powermonitor_*", "sensor.powermonitor_other_power", true)]
|
||||
[InlineData("*_power", "sensor.sonoff_100215c534_power", true)]
|
||||
[InlineData("*_power", "sensor.sonoff_100215c534_energy", false)]
|
||||
[InlineData("sensor.tv_power", "sensor.tv_power", true)]
|
||||
[InlineData("sensor.tv_power", "sensor.tv_power_2", false)]
|
||||
[InlineData("sensor.?.power", "sensor.a.power", false)]
|
||||
[InlineData("*", "anything at all", true)]
|
||||
public void GlobToRegex_ShouldMatchCorrectly(string glob, string input, bool expected)
|
||||
{
|
||||
var regex = PowerSensorDiscoveryService.GlobToRegex(glob);
|
||||
@@ -221,34 +321,50 @@ public class PowerSensorDiscoveryServiceTests
|
||||
|
||||
#region IsPowerSensor
|
||||
|
||||
[Fact]
|
||||
public void IsPowerSensor_WithPowerDeviceClassAndWattUnit_ShouldReturnTrue()
|
||||
[Theory]
|
||||
[InlineData("power device class with watts", "sensor.ac_power", "power", "W", true)]
|
||||
[InlineData("power device class with kilowatts", "sensor.boiler_power", "power", "kW", true)]
|
||||
[InlineData("lowercase unit", "sensor.tv_power", "power", "w", true)]
|
||||
[InlineData("uppercase device class", "sensor.pc_power", "POWER", "W", true)]
|
||||
[InlineData("power device class with megawatts", "sensor.plant_power", "power", "MW", false)]
|
||||
[InlineData("power device class without unit", "sensor.mystery_power", "power", null, false)]
|
||||
[InlineData("power device class with whitespace unit", "sensor.odd_power", "power", " ", false)]
|
||||
[InlineData("energy device class", "sensor.energy_usage", "energy", "kWh", false)]
|
||||
[InlineData("temperature device class", "sensor.hall_temp", "temperature", "°C", false)]
|
||||
[InlineData("missing device class", "sensor.mystery", null, "W", false)]
|
||||
[InlineData("non sensor domain", "switch.sonoff_100215c534_power", "power", "W", false)]
|
||||
[InlineData("binary sensor domain", "binary_sensor.washer_power", "power", "W", false)]
|
||||
public void IsPowerSensor_ShouldMatchOnlySensorEntitiesWithPowerDeviceClassAndPowerUnit(
|
||||
string scenario,
|
||||
string entityId,
|
||||
string? deviceClass,
|
||||
string? unit,
|
||||
bool expected)
|
||||
{
|
||||
SetupEntities(("sensor.sonoff_100215c534_power", "power", "W", "AC Power"));
|
||||
var entity = new Entity(_haContextMock.Object, "sensor.sonoff_100215c534_power");
|
||||
|
||||
// Arrange
|
||||
var entity = SetupEntity(entityId, deviceClass, unit, "Test");
|
||||
var sut = CreateSut();
|
||||
sut.IsPowerSensor(entity).Should().BeTrue();
|
||||
|
||||
// Act
|
||||
var result = sut.IsPowerSensor(entity);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expected, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsPowerSensor_WithNonSensorDomain_ShouldReturnFalse()
|
||||
public void IsPowerSensor_WhenEntityStateMissing_ShouldReturnFalse()
|
||||
{
|
||||
SetupEntities(("switch.sonoff_100215c534_power", "power", "W", "AC Power"));
|
||||
var entity = new Entity(_haContextMock.Object, "switch.sonoff_100215c534_power");
|
||||
|
||||
// Arrange
|
||||
SetupEntitiesWithoutState("sensor.ghost_power");
|
||||
var entity = new Entity(_haContextMock.Object, "sensor.ghost_power");
|
||||
var sut = CreateSut();
|
||||
sut.IsPowerSensor(entity).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsPowerSensor_WithTemperatureDeviceClass_ShouldReturnFalse()
|
||||
{
|
||||
SetupEntities(("sensor.sonoff_100215c534_temp", "temperature", "°C", "AC Temp"));
|
||||
var entity = new Entity(_haContextMock.Object, "sensor.sonoff_100215c534_temp");
|
||||
// Act
|
||||
var result = sut.IsPowerSensor(entity);
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.IsPowerSensor(entity).Should().BeFalse();
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user