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 _haContextMock; private readonly Mock> _loggerMock; public PowerSensorDiscoveryServiceTests() { _haContextMock = new Mock(); _loggerMock = new Mock>(); } private PowerSensorDiscoveryService CreateSut( string mainMeterEntityId = "sensor.sonoff_100267d63f_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(); foreach (var (entityId, deviceClass, unit, friendlyName) in entities) { var attributes = new Dictionary(); 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.sonoff_100267d63f_power", "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(); var result = sut.DiscoverDeviceSensors(); result.Should().HaveCount(2); result.Should().Contain(s => s.EntityId == "sensor.sonoff_100215c534_power"); result.Should().Contain(s => s.EntityId == "sensor.sonoff_10018909cb_power"); } [Fact] public void DiscoverDeviceSensors_ShouldExcludeMainMeter() { SetupEntities( ("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"), ("sensor.tv_power", "power", "W", "TV Power") ); var sut = CreateSut(); var result = sut.DiscoverDeviceSensors(); result.Should().HaveCount(1); result.Should().NotContain(s => s.EntityId == "sensor.sonoff_100267d63f_power"); } [Fact] public void DiscoverDeviceSensors_ShouldApplyExclusionPatterns() { SetupEntities( ("sensor.sonoff_100267d63f_power", "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(); var result = sut.DiscoverDeviceSensors(); result.Should().HaveCount(1); result.Should().Contain(s => s.EntityId == "sensor.tv_power"); } [Fact] public void DiscoverDeviceSensors_ShouldHandleKilowattUnit() { SetupEntities( ("sensor.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"), ("sensor.sonoff_1002230e32_power", "power", "kW", "Boiler Power") ); var sut = CreateSut(); var result = sut.DiscoverDeviceSensors(); result.Should().HaveCount(1); result.First().EntityId.Should().Be("sensor.sonoff_1002230e32_power"); 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.sonoff_100267d63f_power", "power", "W", "Total Home Power Usage"), ("sensor.akvariumas_power", "power", "W", "Akvariumas Power") ); var sut = CreateSut(); sut.CurrentDeviceSensors.Should().BeEmpty(); sut.DiscoverDeviceSensors(); sut.CurrentDeviceSensors.Should().HaveCount(1); } [Fact] public void DiscoverDeviceSensors_ReDiscovery_ShouldReplacePreviousList() { SetupEntities( ("sensor.sonoff_100267d63f_power", "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"), ("sensor.sonoff_100215c534_power", "power", "W", "AC Power"), ("sensor.sonoff_10018909cb_power", "power", "W", "Induction Power") ); sut.DiscoverDeviceSensors(); sut.CurrentDeviceSensors.Should().HaveCount(2); } #endregion #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")] 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.powermonitor_*", "sensor.powermonitor_other_power", true)] [InlineData("*_power", "sensor.sonoff_100215c534_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.sonoff_100215c534_power", "power", "W", "AC Power")); var entity = new Entity(_haContextMock.Object, "sensor.sonoff_100215c534_power"); var sut = CreateSut(); sut.IsPowerSensor(entity).Should().BeTrue(); } [Fact] public void IsPowerSensor_WithNonSensorDomain_ShouldReturnFalse() { SetupEntities(("switch.sonoff_100215c534_power", "power", "W", "AC Power")); var entity = new Entity(_haContextMock.Object, "switch.sonoff_100215c534_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"); var sut = CreateSut(); sut.IsPowerSensor(entity).Should().BeFalse(); } #endregion }