-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add main light automation
- Loading branch information
Showing
13 changed files
with
247 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Reactive.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using NetDaemon.HassModel; | ||
using NetDaemon.HassModel.Entities; | ||
using NetEntityAutomation.Core.Configs; | ||
using NetEntityAutomation.Core.Fsm; | ||
using NetEntityAutomation.Core.Triggers; | ||
using NetEntityAutomation.Extensions.Events; | ||
using NetEntityAutomation.Extensions.ExtensionMethods; | ||
|
||
namespace NetEntityAutomation.Core.Automations; | ||
|
||
public class MainLightAutomationBase: AutomationBase<ILightEntityCore, MainLightFsmBase> | ||
{ | ||
private readonly IEnumerable<ILightEntityCore> _lights; | ||
private readonly IEnumerable<MotionSensor> _motionSensors; | ||
private readonly CustomTimer _timer; | ||
|
||
public MainLightAutomationBase(IHaContext context, AutomationConfig config, ILogger logger) : base(context, config, logger) | ||
{ | ||
_lights = config.Entities.OfType<ILightEntityCore>(); | ||
_motionSensors = config.Triggers.OfType<MotionSensor>(); | ||
_timer = new CustomTimer(Logger); | ||
CreateFsm(); | ||
ConfigureAutomation(); | ||
} | ||
|
||
/// <summary> | ||
/// The automation scenario is to turn off the main light in specific time frame specified by | ||
/// <c>Config.WaitTime</c> parameter. | ||
/// On the timeout, if there are no triggers in on state (usually, this is a motion sensor), the light will be turned off. | ||
/// </summary> | ||
private void ConfigureAutomation() | ||
{ | ||
foreach (var light in _lights) | ||
{ | ||
light.OnEvent().Subscribe(e => | ||
{ | ||
Observable | ||
.Timer(Config.WaitTime) | ||
.Subscribe(_ => ResetTimerOrAction(_timer, Config.WaitTime, light.TurnOff, _motionSensors.IsAnyOn)); | ||
}); | ||
light.OffEvent() | ||
.Subscribe(e => _timer.Dispose()); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// <para> | ||
/// For main light there is only two states: On and Off. The state of the light is fully controlled by the user. | ||
/// </para> | ||
/// </summary> | ||
/// <param name="entity"></param> | ||
/// <returns></returns> | ||
protected override MainLightFsmBase ConfigureFsm(ILightEntityCore entity) | ||
{ | ||
var lightFsm = new MainLightFsmBase(entity, Config, Logger).Configure(ActionForLight()); | ||
entity.OnEvent().Subscribe(_ => lightFsm.FireOn()); | ||
entity.OffEvent().Subscribe(_ => lightFsm.FireOff()); | ||
return lightFsm; | ||
} | ||
|
||
/// <summary> | ||
/// For main automation there is no sense to check any conditions to turn off/on the light as the control over main | ||
/// light is fully on the user. | ||
/// </summary> | ||
/// <returns></returns> | ||
private static MainLightActivateAction ActionForLight() | ||
{ | ||
return new MainLightActivateAction | ||
{ | ||
OffAction = l => l.Entity.TurnOff(), | ||
OnAction = l => l.Entity.TurnOn() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.Extensions.Logging; | ||
using NetDaemon.HassModel.Entities; | ||
using NetEntityAutomation.Core.Configs; | ||
using NetEntityAutomation.Extensions.Events; | ||
|
||
namespace NetEntityAutomation.Core.Fsm; | ||
|
||
public enum MainLightState | ||
{ | ||
Off, | ||
On | ||
} | ||
|
||
public enum MainLightTrigger | ||
{ | ||
SwitchOnTrigger, | ||
SwitchOffTrigger, | ||
TimerElapsed, | ||
AllOff | ||
} | ||
|
||
public struct MainLightActivateAction | ||
{ | ||
public Action<MainLightFsmBase> OffAction { get; init; } | ||
public Action<MainLightFsmBase> OnAction { get; init; } | ||
} | ||
|
||
public class MainLightFsmBase : FsmBase<MainLightState, MainLightTrigger> | ||
{ | ||
public new ILightEntityCore Entity { get; set; } | ||
public MainLightFsmBase(ILightEntityCore light, AutomationConfig config, ILogger logger) : base(light, config, logger) | ||
{ | ||
DefaultState = MainLightState.Off; | ||
Entity = light; | ||
InitFsm(); | ||
} | ||
|
||
public MainLightFsmBase Configure(MainLightActivateAction actions) | ||
{ | ||
_fsm.Configure(MainLightState.Off) | ||
.OnActivate(() => actions.OffAction(this)) | ||
.Ignore(MainLightTrigger.TimerElapsed) | ||
.PermitReentry(MainLightTrigger.SwitchOffTrigger) | ||
.PermitReentry(MainLightTrigger.AllOff) | ||
.Permit(MainLightTrigger.SwitchOnTrigger, MainLightState.On); | ||
|
||
_fsm.Configure(MainLightState.On) | ||
.OnActivate(() => actions.OnAction(this)) | ||
.Ignore(MainLightTrigger.TimerElapsed) | ||
.PermitReentry(MainLightTrigger.SwitchOnTrigger) | ||
.Permit(MainLightTrigger.AllOff, MainLightState.Off) | ||
.Permit(MainLightTrigger.SwitchOffTrigger, MainLightState.Off); | ||
|
||
return this; | ||
} | ||
|
||
public void FireOn() => _fsm.Fire(MainLightTrigger.SwitchOnTrigger); | ||
|
||
public void FireOff() => _fsm.Fire(MainLightTrigger.SwitchOffTrigger); | ||
|
||
public void FireTimerElapsed() => _fsm.Fire(MainLightTrigger.TimerElapsed); | ||
|
||
public override void FireAllOff() => _fsm.Fire(MainLightTrigger.AllOff); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.