From ccdbeedb5d814bbda9abb7133a437beb388e4607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Sun, 28 Apr 2024 20:42:57 +0200 Subject: [PATCH] Added tests --- .../AppScopedHaContextProviderTest.cs | 54 +++++++++++++++++-- .../DebugHost/apps/HaRegistry/RegistryApp.cs | 2 +- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/HassModel/NetDaemon.HassModel.Tests/Internal/AppScopedHaContextProviderTest.cs b/src/HassModel/NetDaemon.HassModel.Tests/Internal/AppScopedHaContextProviderTest.cs index 0164d5433..c1cc1aa00 100644 --- a/src/HassModel/NetDaemon.HassModel.Tests/Internal/AppScopedHaContextProviderTest.cs +++ b/src/HassModel/NetDaemon.HassModel.Tests/Internal/AppScopedHaContextProviderTest.cs @@ -47,6 +47,54 @@ public async void TestCallService() It.IsAny()), Times.Once); } + [Fact] + public async void TestCallServiceWithFloor() + { + var haContext = await CreateTargetAsync(); + + var target = new ServiceTarget { FloorIds = ["floor1", "floor2"] }; + var data = new { Name = "value" }; + haContext.CallService("domain", "service", target, data); + + var expectedCommand = new CallServiceCommand + { + Domain = "domain", + Service = "service", + ServiceData = data, + Target = new HassTarget + { + FloorIds = target.FloorIds + } + }; + _hassConnectionMock.Verify( + c => c.SendCommandAsync(expectedCommand, + It.IsAny()), Times.Once); + } + + [Fact] + public async void TestCallServiceWithLabel() + { + var haContext = await CreateTargetAsync(); + + var target = new ServiceTarget { LabelIds = ["label1", "label2"] }; + var data = new { Name = "value" }; + haContext.CallService("domain", "service", target, data); + + var expectedCommand = new CallServiceCommand + { + Domain = "domain", + Service = "service", + ServiceData = data, + Target = new HassTarget + { + LabelIds = target.LabelIds + } + }; + _hassConnectionMock.Verify( + c => c.SendCommandAsync(expectedCommand, + It.IsAny()), Times.Once); + } + [Fact] public async Task TestCallServiceWithResponseAsync() { @@ -266,7 +314,7 @@ private async Task CreateServiceProvider() serviceCollection.AddScopedHaContext(); var backgroundTaskTrackerMock = new Mock(); - serviceCollection.AddScoped>(_=> backgroundTaskTrackerMock); + serviceCollection.AddScoped>(_ => backgroundTaskTrackerMock); serviceCollection.AddScoped(_ => backgroundTaskTrackerMock.Object); var provider = serviceCollection.BuildServiceProvider(); @@ -280,7 +328,7 @@ public record TestEventData(string command, int endpoint_id, string otherField); public void Dispose() { - _hassEventSubjectMock.Dispose(); - GC.SuppressFinalize(this); + _hassEventSubjectMock.Dispose(); + GC.SuppressFinalize(this); } } diff --git a/src/debug/DebugHost/apps/HaRegistry/RegistryApp.cs b/src/debug/DebugHost/apps/HaRegistry/RegistryApp.cs index 9e112ec4b..92d824ad6 100644 --- a/src/debug/DebugHost/apps/HaRegistry/RegistryApp.cs +++ b/src/debug/DebugHost/apps/HaRegistry/RegistryApp.cs @@ -18,6 +18,6 @@ public RegistryApp(IHaRegistry haRegistry, IHaContext ha) // .Where(x => x.EntityId.StartsWith("input_boolean."))); // // upstairsBooleans.ToList().ForEach(x => x.CallService("toggle")); - ha.CallService("input_boolean", "toggle", new ServiceTarget{ FloorIds = new string[] { "upstairs" } }); + ha.CallService("input_boolean", "toggle", new ServiceTarget{ FloorIds = ["upstairs"] }); } }