Skip to content

Commit

Permalink
Add context to state + a little moving around. (#157)
Browse files Browse the repository at this point in the history
* Context

* Post merge clean.

* Update client.
Add support for context.
Move things around a little.

* Fix name, also in an attempt to re-run tests on build server.
  • Loading branch information
asherw committed Jul 7, 2020
1 parent a2581e6 commit a75d60c
Show file tree
Hide file tree
Showing 21 changed files with 346 additions and 255 deletions.
22 changes: 22 additions & 0 deletions src/App/NetDaemon.App/Common/Fluent/Context.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace NetDaemon.Common.Fluent
{
/// <summary>
///
/// </summary>
public class Context
{
/// <summary>
/// Id
/// </summary>
public string Id { get; set; } = "";
/// <summary>
///
/// </summary>
public string? ParentId { get; set; }
/// <summary>
/// The id of the user who is responsible for the connected item.
/// </summary>
public string? UserId { get; set; }

}
}
2 changes: 1 addition & 1 deletion src/App/NetDaemon.App/Common/Fluent/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace NetDaemon.Common.Fluent
/// Implements interface for managing entities in the fluent API
/// </summary>
public class EntityManager : EntityBase, IEntity, IAction,
IStateEntity, IState, IStateAction, IScript, IDelayStateChange
IStateEntity, IState, IStateAction, IScript
{
/// <summary>
/// Constructor
Expand Down
40 changes: 2 additions & 38 deletions src/App/NetDaemon.App/Common/Fluent/Fluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,6 @@ public interface IEntity :
IStateChanged, ISetState<IAction>, IDelayStateChange
{ }

/// <summary>
/// Properties on entities that can be filtered in lambda expression
/// </summary>
public interface IEntityProperties
{
/// <summary>
/// Filter on area where the entity device are at
/// </summary>
string? Area { get; set; }

/// <summary>
/// Filter on attribute
/// </summary>
dynamic? Attribute { get; set; }

/// <summary>
/// Filter on unique id of the entity
/// </summary>
string EntityId { get; set; }

/// <summary>
/// Filter on last changed time
/// </summary>
DateTime LastChanged { get; set; }

/// <summary>
/// Filter on last updated time
/// </summary>
DateTime LastUpdated { get; set; }

/// <summary>
/// Filter on state
/// </summary>
dynamic? State { get; set; }
}

/// <summary>
/// Expressions that ends with execute
/// </summary>
Expand All @@ -107,7 +71,7 @@ public interface IExecute
}

/// <summary>
/// Expressions that ends with async excecution
/// Expressions that ends with async execution
/// </summary>
public interface IExecuteAsync
{
Expand All @@ -123,7 +87,7 @@ public interface IExecuteAsync
public interface IScript
{
/// <summary>
/// Excecutes scripts async
/// Executes scripts async
/// </summary>
Task ExecuteAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/NetDaemon.App/Common/Fluent/FluentEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace NetDaemon.Common.Fluent
public interface IFluentEvent
{
/// <summary>
/// The fuction to execute
/// The function to execute
/// </summary>
/// <param name="func"></param>
IExecute Call(Func<string, dynamic?, Task>? func);
Expand Down
45 changes: 45 additions & 0 deletions src/App/NetDaemon.App/Common/Fluent/IEntityProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;

namespace NetDaemon.Common.Fluent
{
/// <summary>
/// Properties on entities that can be filtered in lambda expression
/// </summary>
public interface IEntityProperties
{
/// <summary>
/// Filter on area where the entity device are at
/// </summary>
string? Area { get; set; }

/// <summary>
/// Filter on attribute
/// </summary>
dynamic? Attribute { get; set; }

/// <summary>
/// Filter on unique id of the entity
/// </summary>
string EntityId { get; set; }

/// <summary>
/// Filter on last changed time
/// </summary>
DateTime LastChanged { get; set; }

/// <summary>
/// Filter on last updated time
/// </summary>
DateTime LastUpdated { get; set; }

/// <summary>
/// Filter on state
/// </summary>
dynamic? State { get; set; }

/// <summary>
/// Context
/// </summary>
public Context? Context { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/App/NetDaemon.App/Common/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public class EntityState : IEntityProperties
/// The state
/// </summary>
public dynamic? State { get; set; } = "";

/// <summary>
/// Context
/// </summary>
public Context? Context { get; set; }
}

/// <summary>
Expand Down
42 changes: 21 additions & 21 deletions src/App/NetDaemon.App/Common/NetDaemonApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ public abstract class NetDaemonApp : NetDaemonAppBase, INetDaemonApp, INetDaemon
/// <inheritdoc/>
public Task CallService(string domain, string service, dynamic? data = null, bool waitForResponse = false)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.CallServiceAsync(domain, service, data, waitForResponse);
}

/// <inheritdoc/>
public ICamera Camera(params string[] entityIds)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.Camera(this, entityIds);
}

/// <inheritdoc/>
public ICamera Cameras(IEnumerable<string> entityIds)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.Cameras(this, entityIds);
}

/// <inheritdoc/>
public ICamera Cameras(Func<IEntityProperties, bool> func)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.Cameras(this, func);
}

Expand Down Expand Up @@ -171,21 +171,21 @@ public async override ValueTask DisposeAsync()
/// <inheritdoc/>
public IEntity Entities(Func<IEntityProperties, bool> func)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.Entities(this, func);
}

/// <inheritdoc/>
public IEntity Entities(IEnumerable<string> entityIds)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.Entities(this, entityIds);
}

/// <inheritdoc/>
public IEntity Entity(params string[] entityId)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.Entity(this, entityId);
}

Expand All @@ -201,7 +201,7 @@ public IEntity Entity(params string[] entityId)
/// <inheritdoc/>
public async ValueTask<T?> GetDataAsync<T>(string id) where T : class
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return await _daemon!.GetDataAsync<T>(id).ConfigureAwait(false);
}

Expand All @@ -211,21 +211,21 @@ public IEntity Entity(params string[] entityId)
/// <inheritdoc/>
public IFluentInputSelect InputSelect(params string[] inputSelectParams)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.InputSelect(this, inputSelectParams);
}

/// <inheritdoc/>
public IFluentInputSelect InputSelects(IEnumerable<string> inputSelectParams)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.InputSelects(this, inputSelectParams);
}

/// <inheritdoc/>
public IFluentInputSelect InputSelects(Func<IEntityProperties, bool> func)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.InputSelects(this, func);
}

Expand All @@ -239,59 +239,59 @@ public IFluentInputSelect InputSelects(Func<IEntityProperties, bool> func)
public string? ListenState(string pattern,
Func<string, EntityState?, EntityState?, Task> action)
{
// Use guid as uniqe id but will externally use string so
// The design can change incase guild wont cut it
// Use guid as unique id but will externally use string so
// The design can change in-case guid won't cut it.
var uniqueId = Guid.NewGuid().ToString();
_stateCallbacks[uniqueId] = (pattern, action);
return uniqueId.ToString();
return uniqueId;
}

/// <inheritdoc/>
public IMediaPlayer MediaPlayer(params string[] entityIds)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.MediaPlayer(this, entityIds);
}

/// <inheritdoc/>
public IMediaPlayer MediaPlayers(IEnumerable<string> entityIds)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.MediaPlayers(this, entityIds);
}

/// <inheritdoc/>
public IMediaPlayer MediaPlayers(Func<IEntityProperties, bool> func)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.MediaPlayers(this, func);
}

/// <inheritdoc/>
public IScript RunScript(params string[] entityIds)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.RunScript(this, entityIds);
}

/// <inheritdoc/>
public Task SaveDataAsync<T>(string id, T data)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.SaveDataAsync<T>(id, data);
}

/// <inheritdoc/>
public async Task<bool> SendEvent(string eventId, dynamic? data = null)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return await _daemon!.SendEvent(eventId, data).ConfigureAwait(false);
}

/// <inheritdoc/>
public async Task<EntityState?> SetStateAsync(string entityId, dynamic state, params (string name, object val)[] attributes)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return await _daemon!.SetStateAsync(entityId, state, attributes).ConfigureAwait(false);
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/App/NetDaemon.App/Common/NetDaemonAppBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public IHttpHandler Http
{
get
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.Http;
}
}
Expand Down Expand Up @@ -133,9 +133,7 @@ public async Task RestoreAppStateAsync()
expStore.CopyFrom(obj);
}

var appInfo = _daemon!.State
.Where(s => s.EntityId == EntityId)
.FirstOrDefault();
var appInfo = _daemon!.State.FirstOrDefault(s => s.EntityId == EntityId);

var appState = appInfo?.State as string;
if (appState == null || (appState != "on" && appState != "off"))
Expand Down Expand Up @@ -183,9 +181,9 @@ public virtual Task StartUpAsync(INetDaemon daemon)

private async Task HandleLazyStorage()
{
_ = _storageObject as FluentExpandoObject ??
_ = _storageObject ??
throw new NullReferenceException($"{nameof(_storageObject)} cant be null!");
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");

while (!_cancelSource.IsCancellationRequested)
{
Expand Down Expand Up @@ -228,7 +226,7 @@ public async virtual ValueTask DisposeAsync()
/// <inheritdoc/>
public INetDaemonAppBase? GetApp(string appInstanceId)
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon!.GetApp(appInstanceId);
}

Expand Down Expand Up @@ -412,7 +410,7 @@ private async Task ManageRuntimeInformationUpdates()

private async Task HandleUpdateRuntimeInformation()
{
_ = _daemon as INetDaemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");

var runtimeInfo = new AppRuntimeInfo
{
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="0.5.5-beta" />
<PackageReference Include="JoySoftware.HassClient" Version="0.6.0-beta" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.5" />
<PackageReference Include="System.Reactive" Version="4.4.1" />
</ItemGroup>
Expand Down
Loading

0 comments on commit a75d60c

Please sign in to comment.