Skip to content

Commit

Permalink
Handle selector for Multiple entities (specifically for media_player.…
Browse files Browse the repository at this point in the history
…join (#929)
  • Loading branch information
FrankBakkerNl authored Aug 26, 2023
1 parent ecf8c2e commit 68848b4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static ServiceArgument Map(HassServiceField field)
}
private static Type GetClrTypeFromSelector(Selector? selectorObject)
{
return selectorObject switch
var clrType = selectorObject switch
{
null => typeof(object),
NumberSelector s when (s.Step ?? 1) % 1 != 0 => typeof(double),
Expand All @@ -31,5 +31,7 @@ NumberSelector s when (s.Step ?? 1) % 1 != 0 => typeof(double),
_ => typeof(object)
},
};

return selectorObject?.Multiple ?? false ? typeof(IEnumerable<>).MakeGenericType(clrType) : clrType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace NetDaemon.HassModel.CodeGenerator.Model;

internal record Selector()
{
public bool Multiple { get; init; }

public string? Type { get; init; }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Text.Json;
using NetDaemon.Client.HomeAssistant.Model;
using NetDaemon.HassModel.CodeGenerator;
using NetDaemon.HassModel.CodeGenerator.Model;
Expand Down Expand Up @@ -227,6 +229,72 @@ public void Run(Entities entities, Services services)
""";

CodeGenTestHelper.AssertCodeCompiles(code.ToString(), appCode);
}
}

[Fact]
public void MultpileEntitySelector_ShouldGenerateArray()
{
var states = new HassState[] {
new() { EntityId = "media_player.group1" },
};

var serviceMetaData = """
{
"media_player": {
"join": {
"name": "Join",
"description": "Group players together. Only works on platforms with support for player groups.",
"fields": {
"group_members": {
"name": "Group members",
"description": "The players which will be synced with the target player.",
"required": true,
"example": "- media_player.multiroom_player2\n- media_player.multiroom_player3\n",
"selector": {
"entity": {
"multiple": true,
"domain": "media_player"
}
}
}
},
"target": {
"entity": {
"domain": "media_player"
}
}
}
}
}
""";

var appCode = """
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using RootNameSpace;
public class Root
{
public void Run(Entities entities, Services services)
{
entities.MediaPlayer.Group1.Join(groupMembers: new string [] {"media_player.multiroom_player1", "media_player.multiroom_player2"});
services.MediaPlayer.Join(new ServiceTarget(), groupMembers: new string [] {"media_player.multiroom_player1", "media_player.multiroom_player2"});
}
}
""";

var hassServiceDomains = Parse(serviceMetaData);

// Act:
var code = CodeGenTestHelper.GenerateCompilationUnit(_settings, states, hassServiceDomains);

CodeGenTestHelper.AssertCodeCompiles(code.ToString(), appCode);
}

private static IReadOnlyCollection<HassServiceDomain> Parse(string sample)
{
var element = JsonDocument.Parse(sample).RootElement;
return ServiceMetaDataParser.Parse(element);
}
}

0 comments on commit 68848b4

Please sign in to comment.