254 lines
7.9 KiB
C#
254 lines
7.9 KiB
C#
using System.Text.Json;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
using PowerMonitorManager;
|
|
using PowerMonitorManager.Models;
|
|
|
|
namespace PowerMonitor.Manager.Tests;
|
|
|
|
public class PowerSensorDiscoveryServiceTests
|
|
{
|
|
private readonly Mock<IHaContext> _haContextMock;
|
|
private readonly Mock<ILogger<PowerSensorDiscoveryService>> _loggerMock;
|
|
|
|
public PowerSensorDiscoveryServiceTests()
|
|
{
|
|
_haContextMock = new Mock<IHaContext>();
|
|
_loggerMock = new Mock<ILogger<PowerSensorDiscoveryService>>();
|
|
}
|
|
|
|
private PowerSensorDiscoveryService CreateSut(
|
|
string mainMeterEntityId = "sensor.p1_meter_power",
|
|
string[]? excludedPatterns = null)
|
|
{
|
|
var options = Options.Create(new PowerMonitorOptions
|
|
{
|
|
MainMeterEntityId = mainMeterEntityId,
|
|
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)
|
|
{
|
|
var entityList = new List<Entity>();
|
|
|
|
foreach (var (entityId, deviceClass, unit, friendlyName) in entities)
|
|
{
|
|
var attributes = new Dictionary<string, object?>();
|
|
if (deviceClass is not null)
|
|
{
|
|
attributes["device_class"] = deviceClass;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
#region Discovery
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_ShouldFindPowerSensors()
|
|
{
|
|
SetupEntities(
|
|
("sensor.p1_meter_power", "power", "W", "P1 Meter"),
|
|
("sensor.sonoff_power", "power", "W", "Sonoff Power"),
|
|
("sensor.zigbee_power", "power", "W", "Zigbee Power"),
|
|
("sensor.temperature", "temperature", "°C", "Temperature")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
result.Should().HaveCount(2);
|
|
result.Should().Contain(s => s.EntityId == "sensor.sonoff_power");
|
|
result.Should().Contain(s => s.EntityId == "sensor.zigbee_power");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_ShouldExcludeMainMeter()
|
|
{
|
|
SetupEntities(
|
|
("sensor.p1_meter_power", "power", "W", "P1 Meter"),
|
|
("sensor.device_power", "power", "W", "Device")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
result.Should().HaveCount(1);
|
|
result.Should().NotContain(s => s.EntityId == "sensor.p1_meter_power");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_ShouldApplyExclusionPatterns()
|
|
{
|
|
SetupEntities(
|
|
("sensor.p1_meter_power", "power", "W", "P1"),
|
|
("sensor.device_power", "power", "W", "Device"),
|
|
("sensor.nordpool_some_power", "power", "W", "NordPool"),
|
|
("sensor.powermonitor_other_power", "power", "W", "PowerMonitor")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
result.Should().HaveCount(1);
|
|
result.Should().Contain(s => s.EntityId == "sensor.device_power");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_ShouldHandleKilowattUnit()
|
|
{
|
|
SetupEntities(
|
|
("sensor.p1_meter_power", "power", "W", "P1"),
|
|
("sensor.device_kw", "power", "kW", "Device kW")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().UnitOfMeasurement.Should().Be("kW");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_WhenNoPowerSensors_ShouldReturnEmpty()
|
|
{
|
|
SetupEntities(
|
|
("sensor.temperature", "temperature", "°C", "Temp"),
|
|
("switch.some_switch", null, null, "Switch")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverDeviceSensors();
|
|
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_ShouldUpdateCurrentDeviceSensors()
|
|
{
|
|
SetupEntities(
|
|
("sensor.p1_meter_power", "power", "W", "P1"),
|
|
("sensor.device_power", "power", "W", "Device")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
sut.CurrentDeviceSensors.Should().BeEmpty();
|
|
|
|
sut.DiscoverDeviceSensors();
|
|
|
|
sut.CurrentDeviceSensors.Should().HaveCount(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverDeviceSensors_ReDiscovery_ShouldReplacePreviousList()
|
|
{
|
|
SetupEntities(
|
|
("sensor.p1_meter_power", "power", "W", "P1"),
|
|
("sensor.device_a_power", "power", "W", "Device A")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
sut.DiscoverDeviceSensors();
|
|
sut.CurrentDeviceSensors.Should().HaveCount(1);
|
|
|
|
SetupEntities(
|
|
("sensor.p1_meter_power", "power", "W", "P1"),
|
|
("sensor.device_a_power", "power", "W", "Device A"),
|
|
("sensor.device_b_power", "power", "W", "Device B")
|
|
);
|
|
|
|
sut.DiscoverDeviceSensors();
|
|
sut.CurrentDeviceSensors.Should().HaveCount(2);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ExtractDeviceName
|
|
|
|
[Theory]
|
|
[InlineData("sensor.sonoff_1002230e32_power", "sonoff_1002230e32")]
|
|
[InlineData("sensor.zigbee_device_power", "zigbee_device")]
|
|
[InlineData("sensor.some_sensor", "some_sensor")]
|
|
[InlineData("sensor.power", "power")]
|
|
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.device_power", false)]
|
|
[InlineData("sensor.powermonitor_*", "sensor.powermonitor_other_power", true)]
|
|
[InlineData("*_power", "sensor.sonoff_power", true)]
|
|
public void GlobToRegex_ShouldMatchCorrectly(string glob, string input, bool expected)
|
|
{
|
|
var regex = PowerSensorDiscoveryService.GlobToRegex(glob);
|
|
regex.IsMatch(input).Should().Be(expected);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IsPowerSensor
|
|
|
|
[Fact]
|
|
public void IsPowerSensor_WithPowerDeviceClassAndWattUnit_ShouldReturnTrue()
|
|
{
|
|
SetupEntities(("sensor.test_power", "power", "W", "Test"));
|
|
var entity = new Entity(_haContextMock.Object, "sensor.test_power");
|
|
|
|
var sut = CreateSut();
|
|
sut.IsPowerSensor(entity).Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsPowerSensor_WithNonSensorDomain_ShouldReturnFalse()
|
|
{
|
|
SetupEntities(("switch.test_power", "power", "W", "Test"));
|
|
var entity = new Entity(_haContextMock.Object, "switch.test_power");
|
|
|
|
var sut = CreateSut();
|
|
sut.IsPowerSensor(entity).Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsPowerSensor_WithTemperatureDeviceClass_ShouldReturnFalse()
|
|
{
|
|
SetupEntities(("sensor.test_temp", "temperature", "°C", "Test"));
|
|
var entity = new Entity(_haContextMock.Object, "sensor.test_temp");
|
|
|
|
var sut = CreateSut();
|
|
sut.IsPowerSensor(entity).Should().BeFalse();
|
|
}
|
|
|
|
#endregion
|
|
}
|