|
| 1 | +using System.Diagnostics.CodeAnalysis; |
1 | 2 | using System.Text.Json; |
2 | 3 | using NetDaemon.Client.HomeAssistant.Model; |
3 | 4 | using NetDaemon.HassModel.CodeGenerator; |
@@ -363,50 +364,100 @@ public void MultpileEntitySelector_ShouldGenerateArray() |
363 | 364 | { |
364 | 365 | var states = new HassState[] { new() { EntityId = "media_player.group1" } }; |
365 | 366 |
|
366 | | - var serviceMetaData = """ |
367 | | - { |
368 | | - "media_player": { |
369 | | - "join": { |
370 | | - "name": "Join", |
371 | | - "description": "Group players together. Only works on platforms with support for player groups.", |
372 | | - "fields": { |
373 | | - "group_members": { |
374 | | - "name": "Group members", |
375 | | - "description": "The players which will be synced with the target player.", |
376 | | - "required": true, |
377 | | - "example": "- media_player.multiroom_player2\n- media_player.multiroom_player3\n", |
378 | | - "selector": { |
379 | | - "entity": { |
380 | | - "multiple": true, |
381 | | - "domain": "media_player" |
382 | | - } |
383 | | - } |
384 | | - } |
385 | | - }, |
386 | | - "target": { |
387 | | - "entity": { |
388 | | - "domain": "media_player" |
389 | | - } |
390 | | - } |
391 | | - } |
392 | | - } |
393 | | - } |
394 | | - """; |
| 367 | + var hassServiceDomains = Parse(""" |
| 368 | + { |
| 369 | + "media_player": { |
| 370 | + "join": { |
| 371 | + "name": "Join", |
| 372 | + "description": "Group players together. Only works on platforms with support for player groups.", |
| 373 | + "fields": { |
| 374 | + "group_members": { |
| 375 | + "name": "Group members", |
| 376 | + "description": "The players which will be synced with the target player.", |
| 377 | + "required": true, |
| 378 | + "example": "- media_player.multiroom_player2\n- media_player.multiroom_player3\n", |
| 379 | + "selector": { |
| 380 | + "entity": { |
| 381 | + "multiple": true, |
| 382 | + "domain": "media_player" |
| 383 | + } |
| 384 | + } |
| 385 | + } |
| 386 | + }, |
| 387 | + "target": { |
| 388 | + "entity": { |
| 389 | + "domain": "media_player" |
| 390 | + } |
| 391 | + } |
| 392 | + } |
| 393 | + } |
| 394 | + } |
| 395 | + """); |
395 | 396 |
|
396 | 397 | var appCode = WrapMethodBody( |
397 | 398 | """ |
398 | 399 | entities.MediaPlayer.Group1.Join(groupMembers: new string [] {"media_player.multiroom_player1", "media_player.multiroom_player2"}); |
399 | 400 | services.MediaPlayer.Join(new ServiceTarget(), groupMembers: new string [] {"media_player.multiroom_player1", "media_player.multiroom_player2"}); |
400 | 401 | """); |
401 | 402 |
|
402 | | - var hassServiceDomains = Parse(serviceMetaData); |
403 | 403 |
|
404 | 404 | // Act: |
405 | 405 | var code = CodeGenTestHelper.GenerateCompilationUnit(_settings, states, hassServiceDomains); |
406 | 406 | CodeGenTestHelper.AssertCodeCompiles(code.ToString(), appCode); |
407 | 407 | } |
408 | 408 |
|
409 | 409 |
|
| 410 | + [Fact] |
| 411 | + public void MultipleTextSelector_ShouldGenerateArray() |
| 412 | + { |
| 413 | + var states = new HassState[] { new() { EntityId = "input_select.home_mode" } }; |
| 414 | + |
| 415 | + var hassServiceDomains = Parse(""" |
| 416 | + { |
| 417 | + "input_select": { |
| 418 | + "set_options": { |
| 419 | + "name": "Set options", |
| 420 | + "description": "Sets the options.", |
| 421 | + "fields": { |
| 422 | + "options": { |
| 423 | + "required": true, |
| 424 | + "example": "[\u0022Item A\u0022, \u0022Item B\u0022, \u0022Item C\u0022]", |
| 425 | + "selector": { |
| 426 | + "text": { |
| 427 | + "multiple": true |
| 428 | + } |
| 429 | + }, |
| 430 | + "name": "Options", |
| 431 | + "description": "List of options." |
| 432 | + } |
| 433 | + }, |
| 434 | + "target": { |
| 435 | + "entity": { |
| 436 | + "domain": "input_select" |
| 437 | + } |
| 438 | + } |
| 439 | + } |
| 440 | + } |
| 441 | + } |
| 442 | + """); |
| 443 | + |
| 444 | + var appCode = WrapMethodBody( |
| 445 | + """ |
| 446 | + entities.InputSelect.HomeMode.SetOptions(options: new string [] {"home", "away"}); |
| 447 | + entities.InputSelect.HomeMode.SetOptions(options: ["home", "away"]); |
| 448 | +
|
| 449 | + services.InputSelect.SetOptions(new ServiceTarget(), options: new string [] {"home", "away"}); |
| 450 | + services.InputSelect.SetOptions(new ServiceTarget(), options: ["home", "away"]); |
| 451 | + """); |
| 452 | + |
| 453 | +// Act: |
| 454 | + var code = CodeGenTestHelper.GenerateCompilationUnit(_settings, states, hassServiceDomains); |
| 455 | + CodeGenTestHelper.AssertCodeCompiles(code.ToString(), appCode); |
| 456 | + } |
| 457 | + |
| 458 | + |
| 459 | + |
| 460 | + |
410 | 461 | [Fact] |
411 | 462 | public void ValidateLightsArguments() |
412 | 463 | { |
@@ -460,7 +511,7 @@ public void Run(Entities entities, Services services) |
460 | 511 | } |
461 | 512 |
|
462 | 513 |
|
463 | | - private static IReadOnlyCollection<HassServiceDomain> Parse(string sample) |
| 514 | + private static IReadOnlyCollection<HassServiceDomain> Parse([StringSyntax("json")] string sample) |
464 | 515 | { |
465 | 516 | var element = JsonDocument.Parse(sample).RootElement; |
466 | 517 | var result = ServiceMetaDataParser.Parse(element, out var errors); |
|
0 commit comments