Skip to content

Commit

Permalink
Fix expando object converted to fluent
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 24, 2020
1 parent 13838c2 commit 8c57f99
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,13 @@ protected virtual async Task HandleNewEvent(HassEvent hassEvent, CancellationTok
{
try
{
// Convert ExpandoObject to FluentExpandoObject
// We need to do this so not existing attributes
// is returning null
if (hassEvent.Data is ExpandoObject exObject)
{
hassEvent.Data = new FluentExpandoObject(true, true, exObject);
}
var tasks = new List<Task>();
foreach (var app in _runningAppInstances)
{
Expand Down
65 changes: 65 additions & 0 deletions tests/NetDaemon.Daemon.Tests/Reactive/RxAppTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ public async Task NewEventShouldCallFunction()
Assert.True(called);
}

[Fact]
public async Task NewEventMissingDataAttributeShouldReturnNull()
{
// ARRANGE
var daemonTask = await GetConnectedNetDaemonTask();
string? missingAttribute = "has initial value";

// ACT
DefaultDaemonRxApp.EventChanges
.Subscribe(s =>
{
missingAttribute = s.Data?.missing_data;
});

var expandoObj = new ExpandoObject();
dynamic dynExpObject = expandoObj;
dynExpObject.a_parameter = "hello";

DefaultHassClientMock.AddCustomEvent("AN_EVENT", dynExpObject);

await daemonTask;

// ASSERT
Assert.Null(missingAttribute);
}

[Fact]
public async Task NewStateEventShouldCallFunction()
{
Expand Down Expand Up @@ -199,6 +225,22 @@ public async Task StateShouldReturnCorrectEntity()
// ASSERT
Assert.NotNull(entity);
}

[Fact]
public async Task StateShouldReturnNullIfAttributeNotExist()
{
// ARRANGE
var daemonTask = await GetConnectedNetDaemonTask();

// ACT
var entity = DefaultDaemonRxApp.State("binary_sensor.pir");

await daemonTask;

// ASSERT
Assert.Null(entity?.Attribute?.not_exists);
}

[Fact]
public async Task StatesShouldReturnCorrectEntity()
{
Expand Down Expand Up @@ -236,6 +278,29 @@ public async Task UsingEntitiesLambdaNewEventShouldCallFunction()
Assert.True(called);
}

[Fact]
public async Task CallbackObserverAttributeMissingShouldReturnNull()
{
// ARRANGE
var daemonTask = await GetConnectedNetDaemonTask();
string? missingString = "has initial value";

// ACT
DefaultDaemonRxApp.Entities(n => n.EntityId.StartsWith("binary_sensor.pir"))
.StateChanges
.Subscribe(s =>
{
missingString = s.New.Attribute?.missing_attribute;
});

DefaultHassClientMock.AddChangedEvent("binary_sensor.pir_2", "off", "on");

await daemonTask;

// ASSERT
Assert.Null(missingString);
}

[Fact]
public async Task UsingEntitiesNewEventShouldCallFunction()
{
Expand Down

0 comments on commit 8c57f99

Please sign in to comment.