Skip to content

Commit

Permalink
Added support for floors and labels to the ServiceTarget class (#1084)
Browse files Browse the repository at this point in the history
* Added support for floors and labels to the ServiceTarget class

* Added tests

* Removed all Focus attributes from debug project

* Removed the focus attribute for registry app as well
  • Loading branch information
helto4real committed Apr 28, 2024
1 parent 917adf5 commit f3c00df
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ public record HassTarget
/// </summary>
[JsonPropertyName("area_id")]
public IReadOnlyCollection<string>? AreaIds { get; init; }
}

/// <summary>
/// Zero or more floor ids to target with the service call
/// </summary>
[JsonPropertyName("floor_id")]
public IReadOnlyCollection<string>? FloorIds { get; init; }

/// <summary>
/// Zero or more floor ids to target with the service call
/// </summary>
[JsonPropertyName("label_id")]
public IReadOnlyCollection<string>? LabelIds { get; init; }
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static StateChange Map(this HassStateChangedEventData source, IHaContext
{
AreaIds = target.AreaIds,
DeviceIds = target.DeviceIds,
EntityIds = target.EntityIds
EntityIds = target.EntityIds,
FloorIds = target.FloorIds,
LabelIds = target.LabelIds
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/HassModel/NetDeamon.HassModel/ServiceTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ public ServiceTarget()
/// Ids of Areas to invoke a service on
/// </summary>
public IReadOnlyCollection<string>? AreaIds { get; init; }
}

/// <summary>
/// Ids of floors to invoke a service on
/// </summary>
public IReadOnlyCollection<string>? FloorIds { get; init; }

/// <summary>
/// Ids of labels to invoke a service on
/// </summary>
public IReadOnlyCollection<string>? LabelIds { get; init; }
}
1 change: 0 additions & 1 deletion src/debug/DebugHost/apps/Client/ClientDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Apps;

[NetDaemonApp]
// [Focus]
public sealed class ClientApp : IAsyncDisposable
{
private readonly ILogger<ClientApp> _logger;
Expand Down
1 change: 0 additions & 1 deletion src/debug/DebugHost/apps/Client/LabelDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Apps;

[NetDaemonApp]
[Focus]
public sealed class LabelApp : IAsyncInitializable
{
private readonly ILogger<LabelApp> _logger;
Expand Down
1 change: 0 additions & 1 deletion src/debug/DebugHost/apps/Config/ConfigApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Apps;

[NetDaemonApp]
// [Focus]
public class ConfigApp
{
public ConfigApp(IAppConfig<AppConfig> config)
Expand Down
22 changes: 22 additions & 0 deletions src/debug/DebugHost/apps/HaRegistry/RegistryApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Reactive.Linq;
using Microsoft.Extensions.Logging;
using NetDaemon.AppModel;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
namespace Apps;

[NetDaemonApp]
public sealed class RegistryApp
{
public RegistryApp(IHaRegistry haRegistry, IHaContext ha)
{
// var floor = haRegistry.GetFloor("upstairs");
// var upstairsAreas = floor.Areas;
// var upstairsBooleans = upstairsAreas
// .SelectMany(n => n.Entities
// .Where(x => x.EntityId.StartsWith("input_boolean.")));
//
// upstairsBooleans.ToList().ForEach(x => x.CallService("toggle"));
ha.CallService("input_boolean", "toggle", new ServiceTarget{ FloorIds = ["upstairs"] });
}
}
1 change: 0 additions & 1 deletion src/debug/DebugHost/apps/HelloApp/HelloApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Apps;

[NetDaemonApp]
[Focus]
public sealed class HelloApp : IAsyncDisposable
{
private readonly ILogger<HelloApp> _logger;
Expand Down
1 change: 0 additions & 1 deletion src/debug/DebugHost/apps_src/hellow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Apps;

[NetDaemonApp]
[Focus]
public sealed class HelloAppSrc : IAsyncDisposable
{
private readonly ILogger<HelloAppSrc> _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Debug.apps.HassModel.MyInterfaceAutomation;

[NetDaemonApp]
[Focus]
//[Focus]
public class InterfaceUsage
{
public InterfaceUsage(IHaContext haContext, ILogger<InterfaceUsage> logger, IScheduler scheduler)
Expand Down

0 comments on commit f3c00df

Please sign in to comment.