323 lines
11 KiB
C#
323 lines
11 KiB
C#
using System.Text.Json;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NetDaemon.HassModel.Entities;
|
|
using BatteryMonitorManager;
|
|
using BatteryMonitor.Models;
|
|
|
|
namespace BatteryMonitor.Manager.Tests;
|
|
|
|
public class BatterySensorDiscoveryServiceTests
|
|
{
|
|
private readonly Mock<IHaContext> _haContextMock;
|
|
private readonly Mock<ILogger<BatterySensorDiscoveryService>> _loggerMock;
|
|
|
|
public BatterySensorDiscoveryServiceTests()
|
|
{
|
|
_haContextMock = new Mock<IHaContext>();
|
|
_loggerMock = new Mock<ILogger<BatterySensorDiscoveryService>>();
|
|
}
|
|
|
|
private BatterySensorDiscoveryService CreateSut()
|
|
{
|
|
return new BatterySensorDiscoveryService(_haContextMock.Object, _loggerMock.Object);
|
|
}
|
|
|
|
private void SetupEntities(params (string entityId, string? deviceClass, string? state, string? friendlyName)[] entities)
|
|
{
|
|
var entityList = new List<Entity>();
|
|
|
|
foreach (var (entityId, deviceClass, state, friendlyName) in entities)
|
|
{
|
|
var attributes = new Dictionary<string, object?>();
|
|
if (deviceClass is not null)
|
|
{
|
|
attributes["device_class"] = deviceClass;
|
|
}
|
|
|
|
if (friendlyName is not null)
|
|
{
|
|
attributes["friendly_name"] = friendlyName;
|
|
}
|
|
|
|
var attributesJson = JsonSerializer.SerializeToElement(attributes);
|
|
var entityState = new EntityState { AttributesJson = attributesJson, State = state };
|
|
|
|
_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 DiscoverLowBatterySensors_ShouldFindLowBatteryDevices()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "3", "Bathroom Battery"),
|
|
("sensor.bedroom_battery", "battery", "80", "Bedroom Battery"),
|
|
("sensor.boiler_temp_battery", "battery", "2", "Boiler Temp Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(2);
|
|
result.Should().Contain(s => s.EntityId == "sensor.bathroom_battery");
|
|
result.Should().Contain(s => s.EntityId == "sensor.boiler_temp_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldExcludePhoneDevices()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
|
("sensor.erikas_iphone_battery_level", "battery", "1", "Erika's iPhone Battery"),
|
|
("sensor.home_phone_battery_state", "battery", "3", "Home Phone Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldExcludeWatchDevices()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
|
("sensor.marius_watch_battery_level", "battery", "1", "Marius Watch Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldExcludeGalaxyDevices()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
|
("sensor.sm_s936b_battery_level", "battery", "1", "Galaxy S24 Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldIgnoreNonSensorDomain()
|
|
{
|
|
SetupEntities(
|
|
("binary_sensor.bathroom_battery", "battery", "2", "Bathroom Battery"),
|
|
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldIgnoreNonBatteryDeviceClass()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_temperature", "temperature", "2", "Bathroom Temperature"),
|
|
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldSkipUnavailableState()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "unavailable", "Bathroom Battery"),
|
|
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldSkipUnknownState()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "unknown", "Bathroom Battery"),
|
|
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldSkipNonParsableState()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "not_a_number", "Bathroom Battery"),
|
|
("sensor.bedroom_battery", "battery", "3", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bedroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_WhenNoBatterySensors_ShouldReturnEmpty()
|
|
{
|
|
SetupEntities(
|
|
("sensor.temperature", "temperature", "22", "Temperature"),
|
|
("switch.some_switch", null, "on", "Switch")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_WhenAllAboveThreshold_ShouldReturnEmpty()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "50", "Bathroom Battery"),
|
|
("sensor.bedroom_battery", "battery", "80", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldRespectCustomThreshold()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "15", "Bathroom Battery"),
|
|
("sensor.bedroom_battery", "battery", "25", "Bedroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(20);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().EntityId.Should().Be("sensor.bathroom_battery");
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldPopulateFriendlyName()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "2", "Bathroom Battery")
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().FriendlyName.Should().Be("Bathroom Battery");
|
|
result.First().BatteryLevel.Should().Be(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoverLowBatterySensors_ShouldUseEntityIdWhenNoFriendlyName()
|
|
{
|
|
SetupEntities(
|
|
("sensor.bathroom_battery", "battery", "2", null)
|
|
);
|
|
|
|
var sut = CreateSut();
|
|
var result = sut.DiscoverLowBatterySensors(BatteryMonitorConsts.DefaultBatteryThresholdPercent);
|
|
|
|
result.Should().HaveCount(1);
|
|
result.First().FriendlyName.Should().Be("sensor.bathroom_battery");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IsBatterySensor
|
|
|
|
[Fact]
|
|
public void IsBatterySensor_WithBatteryDeviceClass_ShouldReturnTrue()
|
|
{
|
|
SetupEntities(("sensor.bathroom_battery", "battery", "50", "Bathroom Battery"));
|
|
var entity = new Entity(_haContextMock.Object, "sensor.bathroom_battery");
|
|
|
|
BatterySensorDiscoveryService.IsBatterySensor(entity).Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsBatterySensor_WithNonSensorDomain_ShouldReturnFalse()
|
|
{
|
|
SetupEntities(("binary_sensor.bathroom_battery", "battery", "50", "Bathroom Battery"));
|
|
var entity = new Entity(_haContextMock.Object, "binary_sensor.bathroom_battery");
|
|
|
|
BatterySensorDiscoveryService.IsBatterySensor(entity).Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsBatterySensor_WithNonBatteryDeviceClass_ShouldReturnFalse()
|
|
{
|
|
SetupEntities(("sensor.bathroom_temperature", "temperature", "22", "Bathroom Temperature"));
|
|
var entity = new Entity(_haContextMock.Object, "sensor.bathroom_temperature");
|
|
|
|
BatterySensorDiscoveryService.IsBatterySensor(entity).Should().BeFalse();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IsExcludedDevice
|
|
|
|
[Theory]
|
|
[InlineData("sensor.erikas_iphone_battery_level", true)]
|
|
[InlineData("sensor.home_phone_battery_state", true)]
|
|
[InlineData("sensor.marius_watch_battery_level", true)]
|
|
[InlineData("sensor.sm_s936b_battery_level", true)]
|
|
[InlineData("sensor.sm_a536b_battery_level", true)]
|
|
[InlineData("sensor.pixel_8_battery_level", true)]
|
|
[InlineData("sensor.oneplus_battery_level", true)]
|
|
[InlineData("sensor.ipad_battery_level", true)]
|
|
[InlineData("sensor.galaxy_s24_battery", true)]
|
|
[InlineData("sensor.bathroom_battery", false)]
|
|
[InlineData("sensor.boiler_temp_battery", false)]
|
|
[InlineData("sensor.aquarium_leak_sensor_battery", false)]
|
|
public void IsExcludedDevice_ShouldCorrectlyIdentifyExclusions(string entityId, bool expected)
|
|
{
|
|
BatterySensorDiscoveryService.IsExcludedDevice(entityId).Should().Be(expected);
|
|
}
|
|
|
|
#endregion
|
|
}
|