Skip to content

Commit

Permalink
Code cleanup (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 8, 2020
1 parent 0d421d9 commit e5f4026
Show file tree
Hide file tree
Showing 46 changed files with 1,285 additions and 1,516 deletions.
92 changes: 46 additions & 46 deletions src/App/NetDaemon.App/Common/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@

namespace JoySoftware.HomeAssistant.NetDaemon.Common
{
/// <summary>
/// Type of log to supress
/// </summary>
public enum SupressLogType
{
/// <summary>
/// Supress Missing Execute Error in a method
/// </summary>
MissingExecute
}

/// <summary>
/// Attribute to mark function as callback for state changes
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class DisableLogAttribute : System.Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
private SupressLogType[] _logTypesToSupress;

/// <summary>
/// Default constructor
/// </summary>
/// <param name="logTypes">List of logtypes to supress</param>
public DisableLogAttribute(params SupressLogType[] logTypes)
{
_logTypesToSupress = logTypes;
}

/// <summary>
/// Log types to supress
/// </summary>
public IEnumerable<SupressLogType> LogTypesToSupress => _logTypesToSupress;
}

/// <summary>
/// Attribute to mark function as callback for service calls
/// </summary>
Expand All @@ -14,14 +50,16 @@ public sealed class HomeAssistantServiceCallAttribute : System.Attribute { }
[System.AttributeUsage(System.AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class HomeAssistantStateChangedAttribute : System.Attribute
{
private readonly bool _allChanges;

private readonly object? _from;

private readonly object? _to;

// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
private string _entityId;

private readonly object? _to;
private readonly object? _from;
private readonly bool _allChanges;

/// <summary>
/// Default constructor
/// </summary>
Expand All @@ -38,14 +76,14 @@ public HomeAssistantStateChangedAttribute(string entityId, object? to = null, ob
}

/// <summary>
/// Unique id of the entity
/// Get all changes, even if only attribute changes
/// </summary>
public string EntityId => _entityId;
public bool AllChanges => _allChanges;

/// <summary>
/// Get all changes, even if only attribute changes
/// Unique id of the entity
/// </summary>
public bool AllChanges => _allChanges;
public string EntityId => _entityId;

/// <summary>
/// From state filter
Expand All @@ -57,42 +95,4 @@ public HomeAssistantStateChangedAttribute(string entityId, object? to = null, ob
/// </summary>
public object? To => _to;
}

/// <summary>
/// Type of log to supress
/// </summary>
public enum SupressLogType
{
/// <summary>
/// Supress Missing Execute Error in a method
/// </summary>
MissingExecute
}


/// <summary>
/// Attribute to mark function as callback for state changes
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class DisableLogAttribute : System.Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
private SupressLogType[] _logTypesToSupress;


/// <summary>
/// Default constructor
/// </summary>
/// <param name="logTypes">List of logtypes to supress</param>
public DisableLogAttribute(params SupressLogType[] logTypes)
{
_logTypesToSupress = logTypes;
}

/// <summary>
/// Log types to supress
/// </summary>
public IEnumerable<SupressLogType> LogTypesToSupress => _logTypesToSupress;
}
}
3 changes: 1 addition & 2 deletions src/App/NetDaemon.App/Common/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
Expand Down Expand Up @@ -46,6 +44,7 @@ public static string ToSafeHomeAssistantEntityId(this string str)
case UnicodeCategory.DecimalDigitNumber:
stringBuilder.Append(c);
break;

case UnicodeCategory.SpaceSeparator:
case UnicodeCategory.ConnectorPunctuation:
case UnicodeCategory.DashPunctuation:
Expand Down
74 changes: 74 additions & 0 deletions src/App/NetDaemon.App/Common/Fluent/EntityBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JoySoftware.HomeAssistant.NetDaemon.Common
{
/// <summary>
/// Base class for entity fluent types
/// </summary>
public class EntityBase : EntityState
{
internal readonly ConcurrentQueue<FluentAction> _actions =
new ConcurrentQueue<FluentAction>();

internal FluentAction? _currentAction;

internal StateChangedInfo _currentState = new StateChangedInfo();

/// <summary>
/// The daemon used in the API
/// </summary>
protected readonly INetDaemonApp App;

/// <summary>
/// The daemon used in the API
/// </summary>
protected readonly INetDaemon Daemon;

/// <summary>
/// The EntityIds used
/// </summary>
protected readonly IEnumerable<string> EntityIds;

/// <summary>
/// Constructor
/// </summary>
/// <param name="entityIds">The unique ids of the entities managed</param>
/// <param name="daemon">The Daemon that will handle API calls to Home Assistant</param>
/// <param name="app">The Daemon App calling fluent API</param>
public EntityBase(IEnumerable<string> entityIds, INetDaemon daemon, INetDaemonApp app)
{
EntityIds = entityIds;
Daemon = daemon;
App = app;
}

/// <inheritdoc/>
protected static string GetDomainFromEntity(string entity)
{
var entityParts = entity.Split('.');
if (entityParts.Length != 2)
throw new ApplicationException($"entity_id is mal formatted {entity}");

return entityParts[0];
}

/// <inheritdoc/>
protected async Task CallServiceOnAllEntities(string service, dynamic? serviceData = null)
{
var taskList = new List<Task>();
foreach (var entityId in EntityIds)
{
var domain = GetDomainFromEntity(entityId);
serviceData ??= new FluentExpandoObject();
serviceData.entity_id = entityId;
var task = Daemon.CallService(domain, service, serviceData);
taskList.Add(task);
}

if (taskList.Count > 0) await Task.WhenAny(Task.WhenAll(taskList.ToArray()), Task.Delay(5000)).ConfigureAwait(false);
}
}
}
Loading

0 comments on commit e5f4026

Please sign in to comment.