Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Entity Registration Conversation Options #1167

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
{
"id": 3,
"type": "result",
"success": true,
"result": [
{
"config_entry_id": null,
"device_id": "42cdda32a2a3428e86c2e27699d79ead",
"disabled_by": null,
"entity_id": "media_player.tv_uppe2",
"area_id": "42cdda1212a3428e86c2e27699d79ead",
"name": null,
"icon": null,
"platform": "cast"
},
{
"config_entry_id": "4b85129c61c74b27bd90e593f6b7482e",
"device_id": "6e17380a6d2744d18045fe4f627db706",
"disabled_by": null,
"entity_id": "sensor.plex_plex",
"name": null,
"icon": null,
"platform": "plex"
}
]
}
"id": 3,
"type": "result",
"success": true,
"result": [
{
"config_entry_id": null,
"device_id": "42cdda32a2a3428e86c2e27699d79ead",
"disabled_by": null,
"entity_id": "media_player.tv_uppe2",
"area_id": "42cdda1212a3428e86c2e27699d79ead",
"name": null,
"icon": null,
"platform": "cast",
"options": {
"conversation": {
"should_expose": true
}
}

},
{
"config_entry_id": "4b85129c61c74b27bd90e593f6b7482e",
"device_id": "6e17380a6d2744d18045fe4f627db706",
"disabled_by": null,
"entity_id": "sensor.plex_plex",
"name": null,
"icon": null,
"platform": "plex",
"options": {
"conversation": {
"should_expose": false
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ public async Task TestGetEntitiesShouldHaveCorrectCounts()
entities.Should().HaveCount(2);
}


[Fact]
public async Task TestGetEntitiesShouldHaveConversationOptions()
{
await using var ctx = await GetConnectedClientContext().ConfigureAwait(false);
var entities = await ctx.HomeAssistantConnection
.GetEntitiesAsync(TokenSource.Token)
.ConfigureAwait(false);

bool? should_expose = entities?.ElementAt(0).Options?.Conversation?.ShouldExpose;
should_expose.Should().BeTrue();
}

[Fact]
public async Task TestGetConfigShouldReturnCorrectInformation()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NetDaemon.Client.HomeAssistant.Model
{
public record HassEntityConversationOptions
{
[JsonPropertyName("should_expose")] public bool ShouldExpose { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public record HassEntity
[JsonPropertyName("platform")] public string? Platform { get; init; }

[JsonPropertyName("labels")] public IReadOnlyList<string> Labels { get; init; } = [];

[JsonPropertyName("options")]public HassEntityOptions? Options { get; init; }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NetDaemon.Client.HomeAssistant.Model
{
public record HassEntityOptions
{
[JsonPropertyName("conversation")] public HassEntityConversationOptions? Conversation { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ private async Task<HaRegistry> InitializeCacheAndBuildRegistry()
return new HaRegistry(haContextMock.Object, cache);
}

[Fact]
public async Task TestNavigateModel()
private void InitializeDataModel()
{
SetupCommandResult("config/entity_registry/list",
[
new HassEntity { EntityId = "light.mb_nightlight", AreaId = "master_bedroom"},
new HassEntity { EntityId = "light.babyroom_nightlight", AreaId = "baby_room", Labels = ["stay_on"] },
new HassEntity { EntityId = "sensor.babyroom_humidity", DeviceId = "24:AB:1B:9A"},
new HassEntity { EntityId = "light.desk_lamp", AreaId = "study", Options = new HassEntityOptions()
{
Conversation = new HassEntityConversationOptions()
{
ShouldExpose = true
}
}
},
]);

SetupCommandResult("config/device_registry/list",
Expand Down Expand Up @@ -65,7 +72,13 @@ public async Task TestNavigateModel()
new HassLabel { Id = "bedroom", Name = "Bedroom", Description = "Areas that serve as bedrooms" },
new HassLabel { Id = "stay_on", Name = "Stay On", Description = "Lights that should stay on at night" },
]);
}

[Fact]
public async Task TestNavigateModel()
{
// Setup:
InitializeDataModel();

// Act:
var registry = await InitializeCacheAndBuildRegistry();
Expand All @@ -77,6 +90,7 @@ public async Task TestNavigateModel()
new { Id = "light.mb_nightlight" },
new { Id = "light.babyroom_nightlight" },
new { Id = "sensor.babyroom_humidity" },
new { Id = "light.desk_lamp"}
]);

registry.Devices.Should().BeEquivalentTo(
Expand Down Expand Up @@ -137,7 +151,24 @@ public async Task TestNavigateModel()

}

private void SetupCommandResult<TResult>(string command, IReadOnlyCollection<TResult> result)
[Fact]
public async Task TestConversationOptions()
{
// Setup:
InitializeDataModel();

// Act:
var registry = await InitializeCacheAndBuildRegistry();

registry.GetEntityRegistration("light.desk_lamp")!
.Options!
.ConversationOptions!
.ShouldExpose
.Should()
.BeTrue();
}

private void SetupCommandResult<TResult>(string command, IReadOnlyCollection<TResult> result)
{
_connectionMock.Setup(m => m.SendCommandAndReturnResponseAsync<SimpleCommand, IReadOnlyCollection<TResult>>(
new SimpleCommand(command), It.IsAny<CancellationToken>())).ReturnsAsync(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static EntityRegistration Map(this HassEntity hassEntity, IHaRegistryNavi
Device = device,
Labels = hassEntity.Labels.Select(registry.GetLabel).OfType<Label>().ToList(),
Platform = hassEntity.Platform,
Options = new EntityOptions(hassEntity.Options)
};
}

Expand Down
28 changes: 28 additions & 0 deletions src/HassModel/NetDeamon.HassModel/Registry/ConversationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace NetDaemon.HassModel.Entities
{
/// <summary>
/// The options related to Assist/Conversation Assistants
/// </summary>
public record ConversationOptions
{
/// <summary>
/// If the entity should be available to voice/ai assistants
/// </summary>
public bool? ShouldExpose { get; init; }

/// <summary>
/// Default Constructor
/// </summary>
public ConversationOptions() { }

/// <summary>
/// Conversion from Websocket/API Model
/// </summary>
/// <param name="entityConversationOpts"></param>
public ConversationOptions(HassEntityConversationOptions entityConversationOpts)
{
ShouldExpose = entityConversationOpts.ShouldExpose;
}
}

}
26 changes: 26 additions & 0 deletions src/HassModel/NetDeamon.HassModel/Registry/EntityOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace NetDaemon.HassModel.Entities;

/// <summary>
/// Details of Options in the Home Assistant Registry
/// </summary>
public record EntityOptions
{
/// <summary>
/// The Assist/Conversation options for a registration entity
/// </summary>
public ConversationOptions? ConversationOptions { get; init; }

/// <summary>
/// Conversion from Websocket/API Model
/// </summary>
/// <param name="entityOpts"></param>
public EntityOptions(HassEntityOptions? entityOpts)
{
if (entityOpts?.Conversation == null)
ConversationOptions = new ConversationOptions();
else
ConversationOptions = new ConversationOptions(entityOpts.Conversation);

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public record EntityRegistration
/// An identifier for the integration that created this Entity
/// </summary>
public string? Platform { get; init; }

/// <summary>
/// The options set for this Entity
/// </summary>
public EntityOptions? Options {get; init;}
}
Loading