Skip to content

Commit

Permalink
Add advanced state change tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 13, 2020
1 parent bb1568c commit fb58559
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Fakes/NetDaemon.Fakes/DaemonHostTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ public void AddChangedEvent(string entityId, object fromState, object toState)
/// <summary>
/// Adds a full home assistant fake event
/// </summary>
/// <param name="hassEvent">Event to fake</param>
public void AddChangedEvent(HassEvent hassEvent)
/// <param name="fromState"></param>
/// <param name="toState"></param>
public void AddChangeEvent(HassState? fromState, HassState? toState)
{
DefaultHassClientMock.FakeEvents.Enqueue(hassEvent);
DefaultHassClientMock.AddChangeEventFull(fromState, toState);
}

/// <summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Fakes/NetDaemon.Fakes/HassClientMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,25 @@ public void AddChangedEvent(string entityId, object fromState, object toState)
});
}

/// <summary>
/// Adds a changed event for entity
/// </summary>
/// <param name="fromState">The from state advanced details</param>
/// <param name="toState">The to state advanced details</param>
public void AddChangeEventFull(HassState? fromState, HassState? toState)
{
FakeEvents.Enqueue(new HassEvent
{
EventType = "state_changed",
Data = new HassStateChangedEventData
{
EntityId = toState?.EntityId ?? fromState?.EntityId ?? "",
NewState = toState,
OldState = fromState
},
});
}

/// <summary>
/// Adds a custom event
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions tests/NetDaemon.Daemon.Tests/FakesTests/FakeApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public override void Initialize()
{
Entity("light.kitchen").TurnOn();
});

Entity("sensor.temperature")
.StateAllChanges
.Where(e => e.New?.Attribute?.battery_level < 15)
.Subscribe(s =>
{
this.CallService("notify", "notify", new { title = "Hello from Home Assistant" });
});
}
}
}
38 changes: 38 additions & 0 deletions tests/NetDaemon.Daemon.Tests/FakesTests/FakeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using JoySoftware.HomeAssistant.Client;
using Moq;
using NetDaemon.Common.Fluent;
using NetDaemon.Common.Reactive;
Expand Down Expand Up @@ -462,5 +463,42 @@ public async Task TestFakeAppTurnOnCorrectLight()
// Verify that netdaemon called light.turn_on
VerifyCallService("light", "turn_on", "light.kitchen");
}


[Fact]
public async Task TestFakeAppCallNoteWhenBatteryLevelBelowValue()
{
// Add the app to test
await AddAppInstance(new FakeApp());

// Init NetDaemon core runtime
await FakeDaemonInit().ConfigureAwait(false);

// Fake a changed event from en entity
AddChangeEvent(new()
{
EntityId = "sensor.temperature",
State = 10.0,
Attributes = new()
{
["battery_level"] = 18.2
}
}
, new()
{
EntityId = "sensor.temperature",
State = 10.0,
Attributes = new()
{
["battery_level"] = 12.0
}
});

// Run the NetDemon Core to process the messages
await FakeRunDaemonCoreUntilTimeout().ConfigureAwait(false);

// Verify that netdaemon called light.turn_on
VerifyCallService("notify", "notify", new { title = "Hello from Home Assistant" });
}
}
}

0 comments on commit fb58559

Please sign in to comment.