Skip to content

Commit

Permalink
New integration and async fix
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Jan 3, 2021
1 parent 1a55d75 commit fc22cee
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 61 deletions.
4 changes: 3 additions & 1 deletion src/App/NetDaemon.App/Common/INetDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public interface INetDaemon : INetDaemonCommon
/// <param name="entityId">Entity unique id</param>
/// <param name="state">The state that being set, only primitives are supported</param>
/// <param name="attributes">Attributes, use anonomous types and lowercase letters</param>
void SetState(string entityId, dynamic state, dynamic? attributes = null);
/// <param name="waitForResponse">If true, waits for object to set and returns the new EntityState else returns null</param>
/// <returns>Returns entitystate if waitForResponse is true</returns>
EntityState? SetState(string entityId, dynamic state, dynamic? attributes = null, bool waitForResponse = false);

/// <summary>
/// Access the underlying service provider for IOT access to services
Expand Down
3 changes: 2 additions & 1 deletion src/App/NetDaemon.App/Common/Reactive/INetDaemonReactive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public interface INetDaemonRxApp : INetDaemonAppBase, IRxEntity
/// <param name="entityId">EntityId</param>
/// <param name="state">The state to set</param>
/// <param name="attributes">The attributes, use anonomous type like new {attr="someattr", attr2=25}</param>
void SetState(string entityId, dynamic state, dynamic? attributes);
/// <param name="waitForResponse">If true it waits for response and returns new state</param>
EntityState? SetState(string entityId, dynamic state, dynamic? attributes, bool waitForResponse = false);

/// <summary>
/// Get state for a single entity
Expand Down
4 changes: 2 additions & 2 deletions src/App/NetDaemon.App/Common/Reactive/NetDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ public void SaveData<T>(string id, T data)
}

/// <inheritdoc/>
public void SetState(string entityId, dynamic state, dynamic? attributes = null)
public EntityState? SetState(string entityId, dynamic state, dynamic? attributes = null, bool waitForResponse = false)
{
_ = Daemon ?? throw new NetDaemonNullReferenceException($"{nameof(Daemon)} cant be null!");
Daemon.SetState(entityId, state, attributes);
return Daemon.SetState(entityId, state, attributes, waitForResponse);
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/App/NetDaemon.App/NetDaemon.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="20.52.1-beta" />
<PackageReference Include="JoySoftware.HassClient" Version="21.00.1-beta" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Roslynator.Analyzers" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
6 changes: 5 additions & 1 deletion src/Daemon/NetDaemon.Daemon/Daemon/INetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public interface INetDaemonHost : INetDaemon
/// </summary>
/// <param name="numberOfLoadedApps"> The number of loaded apps. </param>
/// <param name="numberOfRunningApps"> The number of running apps. </param>
/// <returns></returns>
Task SetDaemonStateAsync(int numberOfLoadedApps, int numberOfRunningApps);

/// <summary>
Expand All @@ -66,5 +65,10 @@ public interface INetDaemonHost : INetDaemon
/// </summary>
/// <param name="func">callback function</param>
void SubscribeToExternalEvents(Func<ExternalEventBase, Task> func);

/// <summary>
/// Returns true of the NetDaemon custom component is installed
/// </summary>
bool HomeAssistantHasNetDaemonIntegration();
}
}
199 changes: 150 additions & 49 deletions src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Daemon/NetDaemon.Daemon/NetDaemon.Daemon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<tags>Home Assistant</tags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="20.52.1-beta" />
<PackageReference Include="JoySoftware.HassClient" Version="21.00.1-beta" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="YamlDotNet" Version="9.1.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/DaemonRunner/DaemonRunner/DaemonRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JoySoftware.HassClient" Version="20.52.1-beta" />
<PackageReference Include="JoySoftware.HassClient" Version="21.00.1-beta" />
<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" />
Expand Down
8 changes: 4 additions & 4 deletions src/Fakes/NetDaemon.Fakes/RxAppMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ public void VerifySetState(string entityId, dynamic? state = null, dynamic? attr
Verify(x => x.SetState(
entityId,
(object)state,
(object)attributes),
(object)attributes, false),
t);
}
else
{
Verify(x => x.SetState(
entityId,
(object)state,
It.IsAny<object>()),
It.IsAny<object>(), false),
t);
}
}
Expand All @@ -349,15 +349,15 @@ public void VerifySetState(string entityId, dynamic? state = null, dynamic? attr
Verify(x => x.SetState(
entityId,
It.IsAny<object>(),
(object)attributes),
(object)attributes, false),
t);
}
else
{
Verify(x => x.SetState(
entityId,
It.IsAny<object>(),
It.IsAny<object>()),
It.IsAny<object>(), false),
t);
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/NetDaemon.Daemon.Tests/Daemon/NetDaemonHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,20 @@ public async Task SetStateShouldKeepSameArea()
Assert.Equal("Area", state?.Area);
}

[Fact]
public async Task ConnectToHAIntegrationShouldCallCorrectFunction()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
await DefaultDaemonHost.ConnectToHAIntegration().ConfigureAwait(false);

// ACT
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

// ASSERT
DefaultHassClientMock.Verify(n => n.GetApiCall<object>("netdaemon/info"));
}

[Theory]
[InlineData(false, null, null, null, null)]
[InlineData(true, null, 10, null, 10)]
Expand Down

0 comments on commit fc22cee

Please sign in to comment.