Skip to content

Commit

Permalink
Merge 9d86743 into faf048f
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 9, 2020
2 parents faf048f + 9d86743 commit 1303d33
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tags_nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ jobs:
- name: pack Daemon
run: dotnet pack src/Daemon/NetDaemon.Daemon/NetDaemon.Daemon.csproj --configuration Release -p:PackageVersion=${GITHUB_REF#refs/tags/}-beta
- name: pack Test
run: dotnet pack tests/NetDaemon.Test/NetDaemon.Test.csproj --configuration Release -p:PackageVersion=${GITHUB_REF#refs/tags/}-beta
run: dotnet pack src/Fakes/NetDaemon.Fakes/NetDaemon.Fakes.csproj --configuration Release -p:PackageVersion=${GITHUB_REF#refs/tags/}-beta
- name: push to nuget
run: dotnet nuget push **/*.nupkg --api-key ${{secrets.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json --no-symbols true
2 changes: 1 addition & 1 deletion NetDaemon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DaemonRunner", "DaemonRunne
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{32E20E48-2F62-447B-83A2-DB2EA7F61AB3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetDaemon.Test", "tests\NetDaemon.Test\NetDaemon.Test.csproj", "{E1844E4F-2711-4A04-B70C-D67D5D2C8F31}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetDaemon.Fakes", "src/Fakes/NetDaemon.Fakes/NetDaemon.Fakes.csproj", "{E1844E4F-2711-4A04-B70C-D67D5D2C8F31}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
10 changes: 5 additions & 5 deletions src/App/NetDaemon.App/NetDaemon.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
<Nullable>enable</Nullable>
<RootNamespace>NetDaemon</RootNamespace>
<PackageId>JoySoftware.NetDaemon.App</PackageId>
<Version>0.1.29-alpha</Version>
<Version>20.49.2-beta</Version>
<Authors>helto4real</Authors>
<Company>JoySoftware</Company>
<description>A .net core appdaemon for Home Assistant</description>
<projectUrl>https://github.com/helto4real/net-hassclient</projectUrl>
<projectUrl>https://github.com/net-daemon/netdaemon</projectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageDescription>
A application daemon for writing automations for the free open source home automations software Home Assisstant written in .net core 3.
Please see https://github.com/helto4real/netdaemon/blob/main/README.md for docs.
A application daemon for writing automations for the free open source home automations software Home Assisstant written in .net 5.
This is the base app capabilities.
</PackageDescription>
<PackageReleaseNotes>First alpha version, expect things to change!</PackageReleaseNotes>
<PackageReleaseNotes>Please advice this is still in beta</PackageReleaseNotes>
<tags>Home Assistant</tags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
using NetDaemon.Mapping;

[assembly: InternalsVisibleTo("NetDaemon.Daemon.Tests")]
[assembly: InternalsVisibleTo("NetDaemon.Test")]
[assembly: InternalsVisibleTo("NetDaemon.Fakes")]

namespace NetDaemon.Daemon
{
Expand Down
10 changes: 5 additions & 5 deletions src/Daemon/NetDaemon.Daemon/NetDaemon.Daemon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
<Nullable>enable</Nullable>
<RootNamespace>NetDaemon</RootNamespace>
<PackageId>JoySoftware.NetDaemon.Daemon</PackageId>
<Version>0.1.29-alpha</Version>
<Version>20.49.2-beta</Version>
<Authors>helto4real</Authors>
<Company>JoySoftware</Company>
<description>A .net core appdaemon for Home Assistant</description>
<projectUrl>https://github.com/net-daemon/netdaemon</projectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageDescription>
A client for manage the free open source home automations software Home Assisstant written in .net core 3.
Please see https://github.com/net-daemon/netdaemon/blob/dev/README.md for docs.
A application daemon for writing automations for the free open source home automations software Home Assisstant written in .net 5.
This is the NetDaemon core capabilities.
</PackageDescription>
<PackageReleaseNotes>First alpha version, expect things to change!</PackageReleaseNotes>
<PackageReleaseNotes>Please advice this is still in beta</PackageReleaseNotes>
<tags>Home Assistant</tags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="20.49.1-beta" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
<PackageReference Include="YamlDotNet" Version="9.1.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>

Expand Down
12 changes: 6 additions & 6 deletions src/DaemonRunner/DaemonRunner/DaemonRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<Nullable>enable</Nullable>
<RootNamespace>NetDaemon</RootNamespace>
<PackageId>JoySoftware.NetDaemon.DaemonRunner</PackageId>
<Version>0.1.29-alpha</Version>
<Version>20.49.2-beta</Version>
<Authors>helto4real</Authors>
<Company>JoySoftware</Company>
<description>A .net core appdaemon for Home Assistant</description>
<projectUrl>https://github.com/helto4real/net-hassclient</projectUrl>
<projectUrl>https://github.com/net-daemon/netdaemon</projectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageDescription>
A client for manage the free open source home automations software Home Assisstant written in .net core 3.
Please see https://github.com/helto4real/net-hassclient/blob/main/README.md for docs.
Please see https://github.com/net-daemon/netdaemon/blob/main/README.md for docs.
</PackageDescription>
<PackageReleaseNotes>First alpha version, expect things to change!</PackageReleaseNotes>
<tags>Home Assistant</tags>
Expand All @@ -25,12 +25,12 @@
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
<PackageReference Include="YamlDotNet" Version="9.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
using NetDaemon.Daemon.Storage;
using Xunit;

namespace NetDaemon.Daemon.Test
namespace NetDaemon.Daemon.Fakes
{

/// <summary>
/// Base class for test classes
/// </summary>
public partial class DaemonHostTestBase : IAsyncLifetime
{
private readonly NetDaemonHost _defaultDaemonHost;
Expand All @@ -37,10 +40,25 @@ internal DaemonHostTestBase()

}

/// <summary>
/// Returns default DaemonHost mock
/// </summary>
public NetDaemonHost DefaultDaemonHost => _defaultDaemonHost;
/// <summary>
/// Returns default data repository mock
/// </summary>
public Mock<IDataRepository> DefaultDataRepositoryMock => _defaultDataRepositoryMock;
/// <summary>
/// Returns default HassClient mock
/// </summary>
public HassClientMock DefaultHassClientMock => _defaultHassClientMock;
/// <summary>
/// Returns default HttpHandler mock
/// </summary>
public HttpHandlerMock DefaultHttpHandlerMock => _defaultHttpHandlerMock;
/// <summary>
/// Returns default logger mock
/// </summary>
public LoggerMock LoggerMock => _loggerMock;

Task IAsyncLifetime.DisposeAsync()
Expand Down Expand Up @@ -162,6 +180,11 @@ public void VerifyCallServiceTimes(string service, Times times)
DefaultHassClientMock.VerifyCallServiceTimes(service, times);
}

/// <summary>
/// Returns default DaemonHost
/// </summary>
/// <param name="milliSeconds">Timeout in ms</param>
/// <param name="overrideDebugNotCancel">True if use timeout while debug test</param>
public async Task<(Task, CancellationTokenSource)> ReturnRunningDefauldDaemonHostTask(short milliSeconds = 100, bool overrideDebugNotCancel = false)
{
await InitApps();
Expand All @@ -171,7 +194,14 @@ public async Task<(Task, CancellationTokenSource)> ReturnRunningDefauldDaemonHos
return (_defaultDaemonHost.Run("host", 8123, false, "token", cancelSource.Token), cancelSource);
}

public async Task RunDefauldDaemonUntilCanceled(short milliSeconds = 100, bool overrideDebugNotCancel = false)

/// <summary>
/// Runs the default daemon to process messages until canceled
/// </summary>
/// <param name="milliSeconds">Timeout time</param>
/// <param name="overrideDebugNotCancel">True if debugging should cancel</param>
/// <returns></returns>
public async Task RunDefaultDaemonUntilCanceled(short milliSeconds = 100, bool overrideDebugNotCancel = false)
{
var cancelSource = Debugger.IsAttached && !overrideDebugNotCancel
? new CancellationTokenSource()
Expand All @@ -187,6 +217,11 @@ public async Task RunDefauldDaemonUntilCanceled(short milliSeconds = 100, bool o
}
}

/// <summary>
/// Wait for task until canceled
/// </summary>
/// <param name="task">Task to wait for</param>
/// <returns></returns>
public async Task WaitUntilCanceled(Task task)
{
try
Expand All @@ -199,6 +234,9 @@ public async Task WaitUntilCanceled(Task task)
}
}

/// <summary>
/// Initialize applications
/// </summary>
public async Task InitApps()
{
foreach (var inst in DefaultDaemonHost.InternalAllAppInstances)
Expand All @@ -213,6 +251,12 @@ public async Task InitApps()
}
}

/// <summary>
/// Get already pre-connected mock NetDaemon object
/// </summary>
/// <param name="milliSeconds">Timeout in milliseconds</param>
/// <param name="overrideDebugNotCancel">True to use timeout while debugging</param>
/// <returns></returns>
protected async Task<Task> GetConnectedNetDaemonTask(short milliSeconds = 100, bool overrideDebugNotCancel = false)
{
var cancelSource = Debugger.IsAttached && !overrideDebugNotCancel
Expand All @@ -226,13 +270,19 @@ protected async Task<Task> GetConnectedNetDaemonTask(short milliSeconds = 100, b
return daemonTask;
}

protected async Task WaitForDefaultDaemonToConnect(NetDaemonHost daemonHost, CancellationToken stoppingToken)
/// <summary>
/// Wait until default NetDaemon has connected
/// </summary>
/// <param name="daemonHost">Daemon object</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns></returns>
protected async Task WaitForDefaultDaemonToConnect(NetDaemonHost daemonHost, CancellationToken cancellationToken)
{
var nrOfTimesCheckForConnectedState = 0;

while (!daemonHost.Connected && !stoppingToken.IsCancellationRequested)
while (!daemonHost.Connected && !cancellationToken.IsCancellationRequested)
{
await Task.Delay(50, stoppingToken).ConfigureAwait(false);
await Task.Delay(50, cancellationToken).ConfigureAwait(false);
if (nrOfTimesCheckForConnectedState++ > 100)
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
using NetDaemon.Common.Fluent;
using Xunit;

namespace NetDaemon.Daemon.Test
namespace NetDaemon.Daemon.Fakes
{
/// <summary>
/// Mock for HassClient
/// </summary>
public class HassClientMock : Mock<IHassClient>
{
internal HassAreas Areas = new HassAreas();
Expand Down Expand Up @@ -75,6 +78,9 @@ public HassClientMock()
});
}

/// <summary>
/// Default instance of mock
/// </summary>
public static HassClientMock DefaultMock => new HassClientMock();

/// <summary>
Expand All @@ -92,6 +98,12 @@ public static HassClientMock MockConnectFalse
}
}

/// <summary>
/// Adds a new call_service event to mock
/// </summary>
/// <param name="domain">Domain of service</param>
/// <param name="service">Service to fake</param>
/// <param name="data">Data sent by service</param>
public void AddCallServiceEvent(string domain, string service, dynamic data)
{
// Todo: Refactor to smth smarter
Expand All @@ -107,6 +119,14 @@ public void AddCallServiceEvent(string domain, string service, dynamic data)
});
}

/// <summary>
/// Adds a new changed event
/// </summary>
/// <param name="entityId">Id of entity that has changes in state</param>
/// <param name="fromState">Old state</param>
/// <param name="toState">New state</param>
/// <param name="lastUpdated">Last updated</param>
/// <param name="lastChanged">Last changed</param>
public void AddChangedEvent(string entityId, object fromState, object toState, DateTime lastUpdated, DateTime lastChanged)
{
// Todo: Refactor to smth smarter
Expand Down Expand Up @@ -138,6 +158,12 @@ public void AddChangedEvent(string entityId, object fromState, object toState, D
});
}

/// <summary>
/// Adds a changed event for entity
/// </summary>
/// <param name="entityId">Id of entity</param>
/// <param name="fromState">From state</param>
/// <param name="toState">To state</param>
public void AddChangedEvent(string entityId, object fromState, object toState)
{
FakeEvents.Enqueue(new HassEvent
Expand Down Expand Up @@ -170,6 +196,11 @@ public void AddChangedEvent(string entityId, object fromState, object toState)
});
}

/// <summary>
/// Adds a custom event
/// </summary>
/// <param name="eventType">Type of event</param>
/// <param name="data">Data sent by event</param>
public void AddCustomEvent(string eventType, dynamic? data)
{
FakeEvents.Enqueue(new HassEvent
Expand All @@ -179,6 +210,11 @@ public void AddCustomEvent(string eventType, dynamic? data)
});
}

/// <summary>
/// Assert if the HassClient entity state is equeal to NetDaemon entity state
/// </summary>
/// <param name="hassState">HassClient state instance</param>
/// <param name="entity">NetDaemon state instance</param>
public void AssertEqual(HassState hassState, EntityState entity)
{
Assert.Equal(hassState.EntityId, entity.EntityId);
Expand Down Expand Up @@ -212,6 +248,12 @@ public CancellationTokenSource GetSourceWithTimeout(int milliSeconds = 100)
: new CancellationTokenSource(milliSeconds);
}

/// <summary>
/// Verifies that call_service is called
/// </summary>
/// <param name="domain">Service domain</param>
/// <param name="service">Service to verify</param>
/// <param name="attributesTuples">Attributes sent by service</param>
public void VerifyCallService(string domain, string service,
params (string attribute, object value)[] attributesTuples)
{
Expand All @@ -222,11 +264,22 @@ public CancellationTokenSource GetSourceWithTimeout(int milliSeconds = 100)
Verify(n => n.CallService(domain, service, attributes, It.IsAny<bool>()), Times.AtLeastOnce);
}

/// <summary>
/// Verifies service being sent "times" times
/// </summary>
/// <param name="service">The service to check</param>
/// <param name="times">The number of times it should been called</param>
public void VerifyCallServiceTimes(string service, Times times)
{
Verify(n => n.CallService(It.IsAny<string>(), service, It.IsAny<FluentExpandoObject>(), It.IsAny<bool>()), times);
}

/// <summary>
/// Verify state if entity
/// </summary>
/// <param name="entity">Entity to verify</param>
/// <param name="state">The state to verify</param>
/// <param name="attributesTuples">Attributes to verify</param>
public void VerifySetState(string entity, string state,
params (string attribute, object value)[] attributesTuples)
{
Expand All @@ -237,6 +290,11 @@ public void VerifyCallServiceTimes(string service, Times times)
Verify(n => n.SetState(entity, state, attributes), Times.AtLeastOnce);
}

/// <summary>
/// Verify that the state been set number of times
/// </summary>
/// <param name="entity">Entity that the state being set</param>
/// <param name="times">Number of times set</param>
public void VerifySetStateTimes(string entity, Times times)
{
Verify(n => n.SetState(entity, It.IsAny<string>(), It.IsAny<FluentExpandoObject>()), times);
Expand Down
Loading

0 comments on commit 1303d33

Please sign in to comment.