Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard Sperry committed Nov 29, 2024
1 parent 8c8fa98 commit d20d467
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

internal class AutomationBuilder : IAutomationBuilder
{
readonly IHaServices _services;

public AutomationBuilder(IHaServices haServices)
{
_services = haServices;
}

public SimpleAutomationBuildingInfo CreateSimple(bool enabledAtStartup = true)
{
return new()
Expand All @@ -33,12 +26,12 @@ public ConditionalAutomationBuildingInfo CreateConditional(bool enabledAtStartup
};
}

public SchedulableAutomationBuildingInfo CreateSchedulable(bool reschdulable = false, bool enabledAtStartup = true)
public SchedulableAutomationBuildingInfo CreateSchedulable(bool reschedulable = false, bool enabledAtStartup = true)
{
return new()
{
EnabledAtStartup = enabledAtStartup,
IsReschedulable = reschdulable
IsReschedulable = reschedulable
};
}

Expand All @@ -50,7 +43,7 @@ public TypedConditionalBuildingInfo<Tstate, Tatt> CreateConditional<Tstate, Tatt
};
}

public SunAutommationBuildingInfo CreateSunAutomation(SunEventType sunEvent, bool enabledAtStartup = true)
public SunAutomationBuildingInfo CreateSunAutomation(SunEventType sunEvent, bool enabledAtStartup = true)
{
return new()
{
Expand All @@ -59,12 +52,12 @@ public SunAutommationBuildingInfo CreateSunAutomation(SunEventType sunEvent, bo
};
}

public TypedSchedulableAutomationBuildingInfo<Tstate, Tatt> CreateSchedulable<Tstate, Tatt>(bool reschdulable = false, bool enabledAtStartup = true)
public TypedSchedulableAutomationBuildingInfo<Tstate, Tatt> CreateSchedulable<Tstate, Tatt>(bool reschedulable = false, bool enabledAtStartup = true)
{
return new()
{
EnabledAtStartup = enabledAtStartup,
IsReschedulable = reschdulable
IsReschedulable = reschedulable
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private static GetNextEventFromEntityState<Tstate, Tatt> Get_GetNextScheduled<Ts
});
}

public static SunAutomation Build(this SunAutommationBuildingInfo info)
public static SunAutomation Build(this SunAutomationBuildingInfo info)
{
return info.SunEvent switch
{
Expand All @@ -220,19 +220,19 @@ public static SunAutomation Build(this SunAutommationBuildingInfo info)
};
}

public static SunAutommationBuildingInfo WithOffset(this SunAutommationBuildingInfo info, TimeSpan offset)
public static SunAutomationBuildingInfo WithOffset(this SunAutomationBuildingInfo info, TimeSpan offset)
{
info.Offset = offset;
return info;
}

public static SunAutommationBuildingInfo ExecutePastEvents(this SunAutommationBuildingInfo info, bool executePast)
public static SunAutomationBuildingInfo ExecutePastEvents(this SunAutomationBuildingInfo info, bool executePast)
{
info.ExecutePast = executePast;
return info;
}

public static SunAutommationBuildingInfo WithExecution(this SunAutommationBuildingInfo info, Func<CancellationToken, Task> execution)
public static SunAutomationBuildingInfo WithExecution(this SunAutomationBuildingInfo info, Func<CancellationToken, Task> execution)
{
info.Execution = execution;
return info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class TypedSchedulableAutomationBuildingInfo<Tstate, Tatt> : SchedulableA



public class SunAutommationBuildingInfo : AutomationBuildingInfo
public class SunAutomationBuildingInfo : AutomationBuildingInfo
{
internal bool ExecutePast { get; set; } = true;
internal SunEventType SunEvent { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static SchedulableAutomationBuildingInfo SetReschedulable(this Schedulabl
}

/// <summary>
/// Tells the automation how to get the next scheuled time
/// Tells the automation how to get the next scheduled time
/// </summary>
/// <param name="info"></param>
/// <param name="getNextFromState">Asynchronous method that should return a DateTime? based on input state change. Return null if you do not want to schedule</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public static class AutomationExtensions
{
public static T WithMeta<T>(this T auto, string name, string? description = null, bool enabledAtStartup = true, Guid? id = null)
public static T WithMeta<T>(this T auto, string name, string? description = null, bool enabledAtStartup = true)
where T: ISetAutomationMeta
{
AutomationMetaData meta = new AutomationMetaData()
AutomationMetaData meta = new()
{
Name = name,
Description = description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override sealed async Task<bool> ContinuesToBeTrue(HaEntityStateChange ha

if (this._nextExecution != nextEvent)
{
await _lock.WaitAsync();
await _lock.WaitAsync(cancellationToken);
try
{
_nextExecution = nextEvent;
Expand Down Expand Up @@ -83,11 +83,11 @@ public SchedulableAutomation(
Func<CancellationToken, Task> execution,
bool shouldExecutePastEvents = false,
bool shouldExecuteOnError = false,
bool reschedudulable = false) : base(triggerIds, shouldExecutePastEvents, shouldExecuteOnError)
bool reschedulable = false) : base(triggerIds, shouldExecutePastEvents, shouldExecuteOnError)
{
_getNext = getNextEvent;
_execution = execution;
IsReschedulable = reschedudulable;
IsReschedulable = reschedulable;
}

public override Task<DateTime?> CalculateNext(HaEntityStateChange stateChange, CancellationToken cancellationToken)
Expand Down Expand Up @@ -143,7 +143,7 @@ public override async Task<bool> ContinuesToBeTrue(HaEntityStateChange<HaEntityS

if (this._nextExecution != nextEvent)
{
await _lock.WaitAsync();
await _lock.WaitAsync(ct);
try
{
_nextExecution = nextEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ public void SetMeta(AutomationMetaData meta)
public class SimpleAutomation : SimpleAutomationBase
{
private readonly Func<HaEntityStateChange, CancellationToken, Task> _execute;
private readonly EventTiming _eventTimings;

public SimpleAutomation(IEnumerable<string> triggerEntities, Func<HaEntityStateChange, CancellationToken, Task> execute, EventTiming eventTimings)
:base(triggerEntities,eventTimings)
{
this._execute = execute;
this._eventTimings = eventTimings;
}

public override Task Execute(HaEntityStateChange stateChange, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SunSetAutomation(Func<CancellationToken, Task> execution, TimeSpan? offse
public sealed class SunDawnAutomation : SunAutomation
{
public SunDawnAutomation(Func<CancellationToken, Task> execution, TimeSpan? offset = null, EventTiming timings = EventTiming.Durable, bool executePast = true)
: base(execution, offset, timings, true) { }
: base(execution, offset, timings, executePast) { }

protected override DateTime GetNextSunEvent(SunAttributes atts) => atts.NextDawn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class QueuedExecutor : IAutomationExecutor

public async Task Execute(Func<CancellationToken, Task> action, CancellationToken cancellationToken)
{
await _sem.WaitAsync();
await _sem.WaitAsync(cancellationToken);
try
{
await action(cancellationToken);
Expand All @@ -62,7 +62,7 @@ class RestartExecutor : IAutomationExecutor
public async Task Execute(Func<CancellationToken, Task> action, CancellationToken cancellationToken)
{
CancellationToken token;
await _sem.WaitAsync();
await _sem.WaitAsync(cancellationToken);
try
{
if (_source is not null)
Expand All @@ -81,7 +81,7 @@ public async Task Execute(Func<CancellationToken, Task> action, CancellationToke

await action(token);

await _sem.WaitAsync();
await _sem.WaitAsync(cancellationToken);
try
{
_source = null;
Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task Execute(Func<CancellationToken, Task> action, CancellationToke
{
if (_running)
{
await _sem.WaitAsync();
await _sem.WaitAsync(cancellationToken);
try
{
_lastAction = action;
Expand All @@ -159,7 +159,7 @@ public async Task Execute(Func<CancellationToken, Task> action, CancellationToke
while (_lastAction is not null)
{

await _sem.WaitAsync();
await _sem.WaitAsync(cancellationToken);
try
{
act = _lastAction;
Expand Down
6 changes: 3 additions & 3 deletions src/HaKafkaNet/Implementations/Core/TraceLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public void AddLog(string renderedMessage, LogEventInfo logEvent, IDictionary<st
{
bool writtenToTrace = false;

ExecptionInfo? exInfo = null;
ExceptionInfo? exInfo = null;
if (logEvent.Exception is not null)
{
exInfo = ExecptionInfo.Create(logEvent.Exception);
exInfo = ExceptionInfo.Create(logEvent.Exception);
}
LogInfo info = new LogInfo()
{
Expand Down Expand Up @@ -225,7 +225,7 @@ public async Task Trace(TraceEvent evt, AutomationMetaData meta, Func<Task> trac
_logger.LogError(ex, automation_fault);

_observer.OnUnhandledException(meta, ex);
evt.Exception = ExecptionInfo.Create(ex);
evt.Exception = ExceptionInfo.Create(ex);
//give any last logs a chance to make it in
_ = Task.Delay(500).ContinueWith(t => WriteTraceToCache(new TraceData()
{
Expand Down
8 changes: 4 additions & 4 deletions src/HaKafkaNet/Implementations/Services/HaApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace HaKafkaNet;
public static class HaApiExtensions
{
/// <summary>
/// Sometimes and entity is non-reponsive, but HA does not report an error.
/// Sometimes and entity is non-responsive, but HA does not report an error.
/// This method turns on an entity then verifies it turned on
/// </summary>
/// <param name="api"></param>
Expand All @@ -19,10 +19,10 @@ public static async Task<bool> TurnOnAndVerify(this IHaApiProvider api, string e
await api.TurnOn(entityId,cancellationToken);
var apiResponse = await api.GetEntity<HaEntityState<OnOff, JsonElement>>(entityId, cancellationToken);
return !apiResponse.entityState.Bad() && apiResponse.entityState?.State == OnOff.On;
}
}

/// <summary>
/// Sometimes and entity is non-reponsive, but HA does not report an error.
/// Sometimes and entity is non-responsive, but HA does not report an error.
/// This method turns on an entity then verifies it turned off
/// </summary>
/// <param name="api"></param>
Expand Down
2 changes: 1 addition & 1 deletion src/HaKafkaNet/Models/TraceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public record LogInfo
public string? RenderedMessage { get;set; }
public IDictionary<string,object>? Scopes { get; set; }
public required IDictionary<string,object> Properties { get; set; }
public ExecptionInfo? Exception { get; set; }
public ExceptionInfo? Exception { get; set; }
public DateTime? TimeStamp { get; set; }
}
12 changes: 6 additions & 6 deletions src/HaKafkaNet/Models/TraceEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ public record TraceEvent
public required string EventType { get; init; }
public required string AutomationKey { get; init; }
public HaEntityStateChange? StateChange { get; init; }
public ExecptionInfo? Exception {get; set; }
public ExceptionInfo? Exception {get; set; }
}

public record ExecptionInfo
public record ExceptionInfo
{
public required string Type { get; init; }
public required string Message { get; init; }
public string? StackTrace { get; init; }
public ExecptionInfo? InnerException { get; init; }
public IEnumerable<ExecptionInfo>? InnerExceptions { get; init; }
public ExceptionInfo? InnerException { get; init; }
public IEnumerable<ExceptionInfo>? InnerExceptions { get; init; }

public static ExecptionInfo Create(Exception ex)
public static ExceptionInfo Create(Exception ex)
{
return new ExecptionInfo()
return new ExceptionInfo()
{
Type = ex.GetType().FullName ?? ex.GetType().Name,
Message = ex.Message,
Expand Down
2 changes: 1 addition & 1 deletion src/HaKafkaNet/PublicInterfaces/IAutomation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface IAutomation : IAutomationBase, IAutomation<HaEntityStateChange,
public interface IDelayableAutomationBase
{
/// <summary>
/// In some cases, especially if handling prestartup events, this determines if executions should run if it was scheduled to run previous to DateTime.Now
/// In some cases, especially if handling pre-startup events, this determines if executions should run if it was scheduled to run previous to DateTime.Now
/// </summary>
bool ShouldExecutePastEvents { get => false; }

Expand Down
10 changes: 5 additions & 5 deletions src/HaKafkaNet/PublicInterfaces/IAutomationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ TypedAutomationBuildingInfo<Tstate, JsonElement> CreateSimple<Tstate>(bool enabl
TypedConditionalBuildingInfo<Tstate, JsonElement> CreateConditional<Tstate>(bool enabledAtStartup = true)
=> CreateConditional<Tstate, JsonElement>(enabledAtStartup);

SchedulableAutomationBuildingInfo CreateSchedulable(bool reschdulable = false, bool enabledAtStartup = true);
TypedSchedulableAutomationBuildingInfo<Tstate, Tatt> CreateSchedulable<Tstate, Tatt>(bool reschdulable = false, bool enabledAtStartup = true);
TypedSchedulableAutomationBuildingInfo<Tstate, JsonElement> CreateSchedulable<Tstate>(bool reschdulable = false, bool enabledAtStartup = true)
=> CreateSchedulable<Tstate, JsonElement>(reschdulable, enabledAtStartup);
SchedulableAutomationBuildingInfo CreateSchedulable(bool reschedulable = false, bool enabledAtStartup = true);
TypedSchedulableAutomationBuildingInfo<Tstate, Tatt> CreateSchedulable<Tstate, Tatt>(bool reschedulable = false, bool enabledAtStartup = true);
TypedSchedulableAutomationBuildingInfo<Tstate, JsonElement> CreateSchedulable<Tstate>(bool reschedulable = false, bool enabledAtStartup = true)
=> CreateSchedulable<Tstate, JsonElement>(reschedulable, enabledAtStartup);

SunAutommationBuildingInfo CreateSunAutomation(SunEventType sunEvent, bool enabledAtStartup = true);
SunAutomationBuildingInfo CreateSunAutomation(SunEventType sunEvent, bool enabledAtStartup = true);
}
6 changes: 3 additions & 3 deletions src/HaKafkaNet/PublicInterfaces/IAutomationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ SchedulableAutomation CreateScheduled(
Func<CancellationToken, Task> execution,
bool shouldExecutePastEvents = false,
bool shouldExecuteOnError = false,
EventTiming timngs = EventTiming.PostStartup,
bool reschedudulable = false
EventTiming timings = EventTiming.PostStartup,
bool reschedulable = false
);

SchedulableAutomation CreateDurable(
Expand All @@ -30,7 +30,7 @@ SchedulableAutomation CreateDurable(
Func<CancellationToken, Task> execution,
bool shouldExecutePastEvents = true,
bool shouldExecuteOnError = false,
bool reschedudulable = false
bool reschedulable = false
);

SimpleAutomation<Tstate, Tatt> CreateSimple<Tstate, Tatt>(
Expand Down
4 changes: 2 additions & 2 deletions src/HaKafkaNet/PublicInterfaces/IHaApiProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IHaApiProvider
Task<(HttpResponseMessage response, HaEntityState? entityState)> GetEntityState(string entity_id, CancellationToken cancellationToken = default);

/// <summary>
/// Gets the state of an entity with stronly type attributes
/// Gets the state of an entity with strongly type attributes
/// </summary>
/// <typeparam name="T">The type to construct from the attributes</typeparam>
/// <param name="entity_id"></param>
Expand All @@ -40,7 +40,7 @@ public interface IHaApiProvider
Task<(HttpResponseMessage? response, bool ApiAvailable)> CheckApi();

/// <summary>
/// Warning: This endpoint sets the representation of a device within Home Assistant and will not communicate with the actual device. To communicate with the device, use the CallService method or other extentions methods
/// Warning: This endpoint sets the representation of a device within Home Assistant and will not communicate with the actual device. To communicate with the device, use the CallService method or other extensions methods
/// </summary>
/// <typeparam name="Tstate"></typeparam>
/// <typeparam name="Tatt"></typeparam>
Expand Down
4 changes: 2 additions & 2 deletions src/HaKafkaNet/PublicInterfaces/IHaStateCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IHaStateCache : IEntityStateProvider
Task<bool> SetUserDefinedObject<T>(string key, T item, CancellationToken cancellationToken = default) where T: class;

/// <summary>
/// Gets a user saved item. Usefull for things like DateTime, int, etc.
/// Gets a user saved item. Useful for things like DateTime, int, etc.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
Expand All @@ -33,7 +33,7 @@ public interface IHaStateCache : IEntityStateProvider
Task<T?> GetUserDefinedItem<T>(string key, bool throwOnParseException = false, CancellationToken cancellationToken = default) where T : IParsable<T>;

/// <summary>
/// Storage for user defined items. Usefull for things like DateTime, int, etc.
/// Storage for user defined items. Useful for things like DateTime, int, etc.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
Expand Down

0 comments on commit d20d467

Please sign in to comment.