24729ee267
- 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
369 lines
13 KiB
C#
369 lines
13 KiB
C#
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
using PowerMonitorManager;
|
|
|
|
namespace PowerMonitor.Manager.Tests;
|
|
|
|
public class PowerSensorDiscoveryServiceTests
|
|
{
|
|
private readonly Mock<IHaContext> _haContextMock = new();
|
|
private readonly Mock<ILogger<PowerSensorDiscoveryService>> _loggerMock = new();
|
|
|
|
private PowerSensorDiscoveryService CreateSut(
|
|
string mainMeterEntityId = PowerMonitorTestData.MainMeterEntityId,
|
|
string[]? excludedPatterns = null)
|
|
{
|
|
var options = Options.Create(PowerMonitorTestData.CreateOptions(option =>
|
|
{
|
|
option.MainMeterEntityId = mainMeterEntityId;
|
|
option.ExcludedSensorPatterns = excludedPatterns ?? ["sensor.nordpool_*", "sensor.powermonitor_*"];
|
|
}));
|
|
return new PowerSensorDiscoveryService(_haContextMock.Object, options, _loggerMock.Object);
|
|
}
|
|
|
|
private Entity SetupEntity(string entityId, string? deviceClass, string? unit, string? friendlyName)
|
|
{
|
|
var entityState = new EntityState
|
|
{
|
|
AttributesJson = PowerMonitorTestData.CreateAttributesJson(deviceClass, unit, friendlyName)
|
|
};
|
|
|
|
_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))
|
|
]);
|
|
}
|
|
|
|
private void SetupEntitiesWithoutState(params string[] entityIds)
|
|
{
|
|
_haContextMock
|
|
.Setup(h => h.GetAllEntities())
|
|
.Returns([.. entityIds.Select(entityId => new Entity(_haContextMock.Object, entityId))]);
|
|
}
|
|
|
|
#region ExtractDeviceName
|
|
|
|
[Theory]
|
|
[InlineData("sensor.sonoff_1002230e32_power", "sonoff_1002230e32")]
|
|
[InlineData("sensor.sonoff_100215c534_power", "sonoff_100215c534")]
|
|
[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);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GlobToRegex
|
|
|
|
[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);
|
|
regex.IsMatch(input).Should().Be(expected);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Discovery
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_ShouldFindPowerSensors()
|
|
{
|
|
// Arrange
|
|
SetupEntities(
|
|
(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"));
|
|
|
|
var sut = CreateSut();
|
|
|
|
// Act
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
// Assert
|
|
result.Should().HaveCount(2);
|
|
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(
|
|
(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(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(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(
|
|
(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"));
|
|
|
|
var sut = CreateSut();
|
|
|
|
// Act
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
// Assert
|
|
result.Should().HaveCount(1);
|
|
result.Should().Contain(sensor => sensor.EntityId == "sensor.tv_power");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_WhenNoExclusionPatternsConfigured_ShouldKeepMatchingSensors()
|
|
{
|
|
// Arrange
|
|
SetupEntities(
|
|
("sensor.tv_power", "power", "W", "TV Power"),
|
|
("sensor.powermonitor_other_power", "power", "W", "PowerMonitor"));
|
|
|
|
var sut = CreateSut(excludedPatterns: []);
|
|
|
|
// Act
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
// 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"),
|
|
("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(
|
|
(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(
|
|
(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(
|
|
(PowerMonitorTestData.MainMeterEntityId, "power", "W", "Total Home Power Usage"),
|
|
("sensor.sonoff_100215c534_power", "power", "W", "AC Power"),
|
|
("sensor.sonoff_10018909cb_power", "power", "W", "Induction Power"));
|
|
|
|
// Act
|
|
sut.DiscoverDeviceSensors();
|
|
|
|
// Assert
|
|
sut.CurrentDeviceSensors.Should().HaveCount(2);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IsPowerSensor
|
|
|
|
[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)
|
|
{
|
|
// Arrange
|
|
var entity = SetupEntity(entityId, deviceClass, unit, "Test");
|
|
|
|
// Act
|
|
var result = PowerSensorDiscoveryService.IsPowerSensor(entity);
|
|
|
|
// Assert
|
|
result.Should().Be(expected, $"scenario: {scenario}");
|
|
}
|
|
|
|
[Fact]
|
|
public void IsPowerSensor_WhenEntityStateMissing_ShouldReturnFalse()
|
|
{
|
|
// Arrange
|
|
SetupEntitiesWithoutState("sensor.ghost_power");
|
|
var entity = new Entity(_haContextMock.Object, "sensor.ghost_power");
|
|
|
|
// Act
|
|
var result = PowerSensorDiscoveryService.IsPowerSensor(entity);
|
|
|
|
// Assert
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
#endregion
|
|
} |