Skip to content

Commit 3ace71f

Browse files
authored
Merge pull request #13 from bigdnf/develop
Turn off condition for TurnOnAndOffAutomation
2 parents a72530d + 97cad13 commit 3ace71f

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎Core/HA4IoT/Automations/TurnOnAndOffAutomation.cs‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using HA4IoT.Contracts.Components;
99
using HA4IoT.Contracts.Components.States;
1010
using HA4IoT.Contracts.Conditions;
11+
using HA4IoT.Contracts.Hardware;
1112
using HA4IoT.Contracts.Sensors;
1213
using HA4IoT.Contracts.Services.Daylight;
1314
using HA4IoT.Contracts.Services.Settings;
@@ -24,6 +25,7 @@ public class TurnOnAndOffAutomation : AutomationBase
2425

2526
private readonly ConditionsValidator _enablingConditionsValidator = new ConditionsValidator().WithDefaultState(ConditionState.NotFulfilled);
2627
private readonly ConditionsValidator _disablingConditionsValidator = new ConditionsValidator().WithDefaultState(ConditionState.NotFulfilled);
28+
private readonly ConditionsValidator _turnOffConditionsValidator = new ConditionsValidator().WithDefaultState(ConditionState.NotFulfilled);
2729

2830
private readonly IDateTimeService _dateTimeService;
2931
private readonly ISchedulerService _schedulerService;
@@ -35,7 +37,7 @@ public class TurnOnAndOffAutomation : AutomationBase
3537
private IDelayedAction _turnOffTimeout;
3638
private bool _turnOffIfButtonPressedWhileAlreadyOn;
3739
private bool _isOn;
38-
40+
3941
public TurnOnAndOffAutomation(string id, IDateTimeService dateTimeService, ISchedulerService schedulerService, ISettingsService settingsService, IDaylightService daylightService)
4042
: base(id)
4143
{
@@ -224,21 +226,25 @@ private void TurnOn()
224226
_turnOffTimeout?.Cancel();
225227

226228
_components.ForEach(c => c.TryTurnOn());
227-
229+
228230
_isOn = true;
229231
_lastTurnedOn.Restart();
230232
}
231233

232234
private void TurnOff()
233235
{
234236
_turnOffTimeout?.Cancel();
237+
if (GetTurnOffConditionsAreFulfilled())
238+
{
235239

236-
_components.ForEach(c => c.TryTurnOff());
240+
_components.ForEach(c => c.TryTurnOff());
237241

238-
_isOn = false;
239-
_lastTurnedOn.Stop();
242+
_isOn = false;
243+
_lastTurnedOn.Stop();
244+
}
240245
}
241246

247+
242248
private bool GetConditionsAreFulfilled()
243249
{
244250
if (_disablingConditionsValidator.Conditions.Any() && _disablingConditionsValidator.Validate() == ConditionState.Fulfilled)
@@ -253,5 +259,28 @@ private bool GetConditionsAreFulfilled()
253259

254260
return true;
255261
}
262+
263+
public TurnOnAndOffAutomation WithTurnOffCondition(ConditionRelation relation, ICondition condition)
264+
{
265+
if (condition == null) throw new ArgumentNullException(nameof(condition));
266+
267+
_turnOffConditionsValidator.WithCondition(relation, condition);
268+
return this;
269+
}
270+
271+
public TurnOnAndOffAutomation WithDisableTurnOffWhenBinaryStateEnabled(IBinaryInput input)
272+
{
273+
return WithTurnOffCondition(ConditionRelation.Or, new BinaryInputStateCondition(input, BinaryState.High));
274+
}
275+
276+
private bool GetTurnOffConditionsAreFulfilled()
277+
{
278+
if (_turnOffConditionsValidator.Conditions.Any() && _turnOffConditionsValidator.Validate() == ConditionState.Fulfilled)
279+
{
280+
return false;
281+
}
282+
283+
return true;
284+
}
256285
}
257286
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using HA4IoT.Conditions;
3+
using HA4IoT.Contracts.Hardware;
4+
5+
namespace HA4IoT.Conditions.Specialized
6+
{
7+
public class BinaryInputStateCondition : Condition
8+
{
9+
public BinaryInputStateCondition(IBinaryInput input, BinaryState state)
10+
{
11+
if (input == null) throw new ArgumentNullException(nameof(input));
12+
13+
WithExpression(() => input.Read() == state);
14+
}
15+
}
16+
}

‎Core/HA4IoT/HA4IoT.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
<Compile Include="Adapters\PortBasedWindowAdapter.cs" />
137137
<Compile Include="Adapters\VirtualButtonAdapter.cs" />
138138
<Compile Include="Api\ExceptionSerializer.cs" />
139+
<Compile Include="Conditions\Specialized\BinaryInputStateCondition.cs" />
139140
<Compile Include="Commands\CommandInvoker.cs" />
140141
<Compile Include="Commands\CommandResolver.cs" />
141142
<Compile Include="Components\CommandInvokerAction.cs" />

0 commit comments

Comments
 (0)