From ada3c7777816020cd30ac07c0964cbd44c466d95 Mon Sep 17 00:00:00 2001 From: ExQz Date: Sun, 29 Nov 2026 21:31:45 +0200 Subject: [PATCH] Fix culture-sensitive parsing, log swallowing and closure capture - DateTimeTypeConverter parses with the invariant culture instead of the ambient one (NordPool timestamps arrive as ISO-8601) - Shower last-shower-time state is formatted with the invariant culture - PresenceServiceTests no longer captures the loop variable in a Moq setup - CheapestIntervalsFinderTests compare prices with a tolerance instead of floating-point equality - MqttRecreateApp logs the unit_of_measurement read failure instead of silently swallowing every exception - LightMonitorOptions moves to LightMonitor.Models so the namespace matches the project and file location --- HomeAssistant.Server/MqttRecreateApp.cs | 3 ++- HomeAssistant.Server/program.cs | 2 +- .../LightControlServiceTests.cs | 1 - LightMonitor.Manager.Tests/LightMonitorInitTests.cs | 1 - LightMonitor.Manager.Tests/LightMonitorTestData.cs | 1 - LightMonitor.Manager.Tests/PresenceServiceTests.cs | 12 +++++++----- LightMonitor.Manager/LightControlService.cs | 1 - LightMonitor.Manager/LightMonitorInit.cs | 1 - .../LightMonitorServiceRegistration.cs | 2 +- LightMonitor.Manager/PresenceService.cs | 1 - LightMonitor.Models/LightMonitorOptions.cs | 2 +- Mappings/Converters/DateTimeTypeConverter.cs | 7 ++++--- .../CheapestIntervalsFinderTests.cs | 10 ++++++---- NordPool.Manager.Tests/NordPoolPriceServiceTests.cs | 3 ++- ShowerMonitor.Manager/ShowerMonitorEntityService.cs | 2 +- 15 files changed, 25 insertions(+), 24 deletions(-) diff --git a/HomeAssistant.Server/MqttRecreateApp.cs b/HomeAssistant.Server/MqttRecreateApp.cs index fcb5276..43e92f2 100644 --- a/HomeAssistant.Server/MqttRecreateApp.cs +++ b/HomeAssistant.Server/MqttRecreateApp.cs @@ -208,8 +208,9 @@ public sealed class MqttRecreateApp }; } } - catch + catch (Exception ex) { + _logger.LogDebug(ex, "Could not read unit_of_measurement for {EntityId}", NordPoolPriceEntityId); } return null; diff --git a/HomeAssistant.Server/program.cs b/HomeAssistant.Server/program.cs index 2550e67..bdd3cf0 100644 --- a/HomeAssistant.Server/program.cs +++ b/HomeAssistant.Server/program.cs @@ -4,6 +4,7 @@ using HomeAssistant.Entities; using HomeAssistant.Entities.Interfaces; using HomeAssistantGenerated; using HomeAssistantServer; +using LightMonitor.Models; using Mappings; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -21,7 +22,6 @@ using PowerMonitorManager.Models; using ShowerMonitorManager; using ShowerMonitorManager.Models; using LightMonitorManager; -using LightMonitorManager.Models; using BatteryMonitorManager; using BatteryMonitorManager.Models; using LeakMonitorManager; diff --git a/LightMonitor.Manager.Tests/LightControlServiceTests.cs b/LightMonitor.Manager.Tests/LightControlServiceTests.cs index 4f8c26d..e1c761e 100644 --- a/LightMonitor.Manager.Tests/LightControlServiceTests.cs +++ b/LightMonitor.Manager.Tests/LightControlServiceTests.cs @@ -5,7 +5,6 @@ using Moq; using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; using LightMonitorManager; -using LightMonitorManager.Models; namespace LightMonitor.Manager.Tests; diff --git a/LightMonitor.Manager.Tests/LightMonitorInitTests.cs b/LightMonitor.Manager.Tests/LightMonitorInitTests.cs index 21efaa2..dcd780e 100644 --- a/LightMonitor.Manager.Tests/LightMonitorInitTests.cs +++ b/LightMonitor.Manager.Tests/LightMonitorInitTests.cs @@ -8,7 +8,6 @@ using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; using LightMonitorManager; using LightMonitorManager.Interface; -using LightMonitorManager.Models; namespace LightMonitor.Manager.Tests; diff --git a/LightMonitor.Manager.Tests/LightMonitorTestData.cs b/LightMonitor.Manager.Tests/LightMonitorTestData.cs index 646294f..68bf425 100644 --- a/LightMonitor.Manager.Tests/LightMonitorTestData.cs +++ b/LightMonitor.Manager.Tests/LightMonitorTestData.cs @@ -1,5 +1,4 @@ using LightMonitor.Models; -using LightMonitorManager.Models; using NetDaemon.HassModel.Entities; namespace LightMonitor.Manager.Tests; diff --git a/LightMonitor.Manager.Tests/PresenceServiceTests.cs b/LightMonitor.Manager.Tests/PresenceServiceTests.cs index 8a19e62..2d77e3d 100644 --- a/LightMonitor.Manager.Tests/PresenceServiceTests.cs +++ b/LightMonitor.Manager.Tests/PresenceServiceTests.cs @@ -6,7 +6,6 @@ using Moq; using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; using LightMonitorManager; -using LightMonitorManager.Models; namespace LightMonitor.Manager.Tests; @@ -24,12 +23,15 @@ public class PresenceServiceTests Options.Create(options ?? _defaultOptions)); } - private void SetupStates(IReadOnlyList personStates) + private void SetupStates(string?[] personStates) { - for (var index = 0; index < personStates.Count; index++) + for (var index = 0; index < personStates.Length; index++) { - _haContextMock.Setup(x => x.GetState(LightMonitorTestData.PersonEntityIds[index])) - .Returns(LightMonitorTestData.CreateOptionalState(personStates[index])); + var entityId = LightMonitorTestData.PersonEntityIds[index]; + var state = LightMonitorTestData.CreateOptionalState(personStates[index]); + + _haContextMock.Setup(x => x.GetState(entityId)) + .Returns(state); } } diff --git a/LightMonitor.Manager/LightControlService.cs b/LightMonitor.Manager/LightControlService.cs index ceaf386..f70eae0 100644 --- a/LightMonitor.Manager/LightControlService.cs +++ b/LightMonitor.Manager/LightControlService.cs @@ -4,7 +4,6 @@ using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; using LightMonitor.Models; using LightMonitorManager.Interface; -using LightMonitorManager.Models; namespace LightMonitorManager; diff --git a/LightMonitor.Manager/LightMonitorInit.cs b/LightMonitor.Manager/LightMonitorInit.cs index ef43763..5b70ed1 100644 --- a/LightMonitor.Manager/LightMonitorInit.cs +++ b/LightMonitor.Manager/LightMonitorInit.cs @@ -5,7 +5,6 @@ using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; using LightMonitor.Models; using LightMonitorManager.Interface; -using LightMonitorManager.Models; namespace LightMonitorManager; diff --git a/LightMonitor.Manager/LightMonitorServiceRegistration.cs b/LightMonitor.Manager/LightMonitorServiceRegistration.cs index 6669c0f..1c698d0 100644 --- a/LightMonitor.Manager/LightMonitorServiceRegistration.cs +++ b/LightMonitor.Manager/LightMonitorServiceRegistration.cs @@ -1,8 +1,8 @@ +using LightMonitor.Models; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using NetDaemon.AppModel; using LightMonitorManager.Interface; -using LightMonitorManager.Models; namespace LightMonitorManager; diff --git a/LightMonitor.Manager/PresenceService.cs b/LightMonitor.Manager/PresenceService.cs index 696357f..be8fd2a 100644 --- a/LightMonitor.Manager/PresenceService.cs +++ b/LightMonitor.Manager/PresenceService.cs @@ -3,7 +3,6 @@ using Microsoft.Extensions.Options; using NetDaemon.HassModel; using LightMonitor.Models; using LightMonitorManager.Interface; -using LightMonitorManager.Models; namespace LightMonitorManager; diff --git a/LightMonitor.Models/LightMonitorOptions.cs b/LightMonitor.Models/LightMonitorOptions.cs index 829a41a..c4381a4 100644 --- a/LightMonitor.Models/LightMonitorOptions.cs +++ b/LightMonitor.Models/LightMonitorOptions.cs @@ -1,6 +1,6 @@ using static LightMonitor.Models.LightMonitorConsts; -namespace LightMonitorManager.Models; +namespace LightMonitor.Models; public sealed class LightMonitorOptions { diff --git a/Mappings/Converters/DateTimeTypeConverter.cs b/Mappings/Converters/DateTimeTypeConverter.cs index 83601f3..d306125 100644 --- a/Mappings/Converters/DateTimeTypeConverter.cs +++ b/Mappings/Converters/DateTimeTypeConverter.cs @@ -1,4 +1,5 @@ -using AutoMapper; +using System.Globalization; +using AutoMapper; namespace Mappings.Converters; @@ -6,6 +7,6 @@ public class DateTimeTypeConverter : ITypeConverter { public DateTime Convert(string source, DateTime destination, ResolutionContext context) { - return System.Convert.ToDateTime(source); + return DateTime.Parse(source, CultureInfo.InvariantCulture); } -} \ No newline at end of file +} diff --git a/NordPool.Manager.Tests/CheapestIntervalsFinderTests.cs b/NordPool.Manager.Tests/CheapestIntervalsFinderTests.cs index 86c7646..a6a4b87 100644 --- a/NordPool.Manager.Tests/CheapestIntervalsFinderTests.cs +++ b/NordPool.Manager.Tests/CheapestIntervalsFinderTests.cs @@ -15,6 +15,8 @@ public class CheapestIntervalsFinderTests private CheapestIntervalsFinder CreateSut() => new(_loggerMock.Object); + private static bool IsPrice(double actual, double expected) => Math.Abs(actual - expected) < 0.000_001; + private static NordPoolPrice CreatePricesForDay(DateTime dayStart, int intervalCount = NordPoolConsts.MaxDailyIntervals) { return new NordPoolPrice @@ -129,8 +131,8 @@ public class CheapestIntervalsFinderTests var result = sut.FindCheapestIntervals(prices, 4, double.MaxValue, DeadlineEndOfDay); // Assert - result.Single(r => r.Price == 4.93).Rank.Should().Be(1); - result.Single(r => r.Price == 8.96).Rank.Should().Be(2); + result.Single(r => IsPrice(r.Price, 4.93)).Rank.Should().Be(1); + result.Single(r => IsPrice(r.Price, 8.96)).Rank.Should().Be(2); } [Fact] @@ -219,8 +221,8 @@ public class CheapestIntervalsFinderTests // Assert — results are chronological, ranks still follow price order result.Should().HaveCount(2); result.Select(r => r.Price).Should().ContainInOrder(-2.10, -7.40); - result.Single(r => r.Price == -7.40).Rank.Should().Be(1); - result.Single(r => r.Price == -2.10).Rank.Should().Be(2); + result.Single(r => IsPrice(r.Price, -7.40)).Rank.Should().Be(1); + result.Single(r => IsPrice(r.Price, -2.10)).Rank.Should().Be(2); } [Fact] diff --git a/NordPool.Manager.Tests/NordPoolPriceServiceTests.cs b/NordPool.Manager.Tests/NordPoolPriceServiceTests.cs index 0ba1658..7d81198 100644 --- a/NordPool.Manager.Tests/NordPoolPriceServiceTests.cs +++ b/NordPool.Manager.Tests/NordPoolPriceServiceTests.cs @@ -1,3 +1,4 @@ +using System.Globalization; using System.Text.Json; using AutoMapper; using FluentAssertions; @@ -242,7 +243,7 @@ public class NordPoolPriceServiceTests { Prices = new List { - new() { Start = DateTime.Parse(dto.Prices[0].Start!), End = DateTime.Parse(dto.Prices[0].End!), Price = 100.0 } + new() { Start = DateTime.Parse(dto.Prices[0].Start!, CultureInfo.InvariantCulture), End = DateTime.Parse(dto.Prices[0].End!, CultureInfo.InvariantCulture), Price = 100.0 } } }; diff --git a/ShowerMonitor.Manager/ShowerMonitorEntityService.cs b/ShowerMonitor.Manager/ShowerMonitorEntityService.cs index add6b51..f3addf2 100644 --- a/ShowerMonitor.Manager/ShowerMonitorEntityService.cs +++ b/ShowerMonitor.Manager/ShowerMonitorEntityService.cs @@ -181,7 +181,7 @@ public sealed class ShowerMonitorEntityService( last_updated = timestamp }).ConfigureAwait(false); - await SetEntityStateSafe(LastShowerTimeEntityId, session.StartTime.ToString("yyyy-MM-dd HH:mm:ss")).ConfigureAwait(false); + await SetEntityStateSafe(LastShowerTimeEntityId, session.StartTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)).ConfigureAwait(false); await SetEntityAttributesSafe(LastShowerTimeEntityId, new { friendly_name = "Last shower time",