Skip to content

Commit

Permalink
Add data to service extensions for services that lacks metadata (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 8, 2024
1 parent dff49c8 commit 0d4392d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ codecover_merged.info
# User-specific files
.idea/
appsettings.Development.json
appsettings.development.json

**/core

**/coverage.json

# remove HA integration test files from git
tests/Integration/HA/config/*
!tests/Integration/HA/config/*.yaml
!tests/Integration/HA/config/*.yaml

# remove codegenerated files
HomeAssistantGenerated.cs
EntityMetaData.json
ServicesMetaData.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateExtensionMethodsForS
private static MemberDeclarationSyntax ExtensionMethodWithoutArguments(HassService service, string serviceName, string entityTypeName)
{
return ParseMemberDeclaration($$"""
public static void {{GetServiceMethodName(serviceName)}}(this {{entityTypeName}} target)
public static void {{GetServiceMethodName(serviceName)}}(this {{entityTypeName}} target, object? data = null)
{
target.CallService("{{serviceName}}");
target.CallService("{{serviceName}}", data);
}
""")!
.WithSummaryComment(service.Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static SyntaxTrivia[] GetFileHeader()
// In the template projects we provided a convenience powershell script that will update
// the codegen and nugets to latest versions update_all_dependencies.ps1.
//
// For more information: https://netdaemon.xyz/docs/v3/hass_model/hass_model_codegen
// For more information: https://netdaemon.xyz/docs/user/hass_model/hass_model_codegen
// For more information about NetDaemon: https://netdaemon.xyz/
// </auto-generated>
//------------------------------------------------------------------------------";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateServiceMethod(string
{
// method without arguments
yield return ParseMemberDeclaration($$"""
void {{serviceMethodName}}({{targetParam}})
void {{serviceMethodName}}({{CommaSeparateNonEmpty(targetParam, "object? data = null")}})
{
{{haContextVariableName}}.CallService("{{domain}}", "{{serviceName}}", {{targetArg}});
{{haContextVariableName}}.CallService("{{domain}}", "{{serviceName}}", {{CommaSeparateNonEmpty(targetArg, "data")}});
}
""")!
.ToPublic()
Expand All @@ -112,7 +112,7 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateServiceMethod(string
{
// method using arguments object
yield return ParseMemberDeclaration($$"""
void {{serviceMethodName}}({{JoinList(targetParam, serviceArguments.TypeName)}} data)
void {{serviceMethodName}}({{CommaSeparateNonEmpty(targetParam, serviceArguments.TypeName)}} data)
{
{{haContextVariableName}}.CallService("{{domain}}", "{{serviceName}}", {{targetArg}}, data);
}
Expand All @@ -123,7 +123,7 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateServiceMethod(string

// method using arguments as separate parameters
yield return ParseMemberDeclaration($$"""
void {{serviceMethodName}}({{JoinList(targetParam, serviceArguments.GetParametersList())}})
void {{serviceMethodName}}({{CommaSeparateNonEmpty(targetParam, serviceArguments.GetParametersList())}})
{
{{haContextVariableName}}.CallService("{{domain}}", "{{serviceName}}", {{targetArg}}, {{serviceArguments.GetNewServiceArgumentsTypeExpression()}});
}
Expand All @@ -135,5 +135,5 @@ private static IEnumerable<MemberDeclarationSyntax> GenerateServiceMethod(string
}
}

private static string JoinList(params string?[] args) => string.Join(", ", args.Where(s => !string.IsNullOrEmpty(s)));
private static string CommaSeparateNonEmpty(params string?[] args) => string.Join(", ", args.Where(s => !string.IsNullOrEmpty(s)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,67 @@ public void Run(IHaContext ha)
CodeGenTestHelper.AssertCodeCompiles(code.ToString(), appCode);
}

[Fact]
public void TestServicesGenerationWithAndWithoutProvidingDataForServicesWithoutTargetOrFields()
{
var readOnlyCollection = new HassState[] {
new() { EntityId = "script.script1" },
};

var hassServiceDomains = new HassServiceDomain[] {
new() {
Domain = "script",
Services = [
new() {
Service = "turn_off",
Target = new TargetSelector
{
Entity = [new EntitySelector { Domain = ["script"] }]
}

},
new() {
Service = "reload",
},
]
}
};

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

var appCode = """
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using RootNameSpace;

public class Root
{
public void Run(IHaContext ha)
{
var s = new RootNameSpace.Services(ha);

s.Script.TurnOff(new ServiceTarget());
s.Script.TurnOff(new ServiceTarget(), new { });

s.Script.Reload();
s.Script.Reload(new { });

ScriptEntity script = new RootNameSpace.ScriptEntity(ha, "script.testScript");

script.TurnOff(new { });
script.TurnOff();

IScriptEntityCore scriptCore = script;
scriptCore.TurnOff();
scriptCore.TurnOff(new { });
}
}
""";

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

[Fact]
public void TestServiceWithoutAnyTargetEntity_ExtensionMethodSkipped()
{
Expand Down

0 comments on commit 0d4392d

Please sign in to comment.