Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Apr 28, 2024
1 parent 59b8482 commit ccdbeed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,54 @@ public async void TestCallService()
It.IsAny<CancellationToken>()), 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<CallServiceCommand>(expectedCommand,
It.IsAny<CancellationToken>()), 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<CallServiceCommand>(expectedCommand,
It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task TestCallServiceWithResponseAsync()
{
Expand Down Expand Up @@ -266,7 +314,7 @@ private async Task<ServiceProvider> CreateServiceProvider()
serviceCollection.AddScopedHaContext();

var backgroundTaskTrackerMock = new Mock<IBackgroundTaskTracker>();
serviceCollection.AddScoped<Mock<IBackgroundTaskTracker>>(_=> backgroundTaskTrackerMock);
serviceCollection.AddScoped<Mock<IBackgroundTaskTracker>>(_ => backgroundTaskTrackerMock);
serviceCollection.AddScoped(_ => backgroundTaskTrackerMock.Object);

var provider = serviceCollection.BuildServiceProvider();
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/debug/DebugHost/apps/HaRegistry/RegistryApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"] });
}
}

0 comments on commit ccdbeed

Please sign in to comment.