88using HA4IoT . Contracts . Components ;
99using HA4IoT . Contracts . Components . States ;
1010using HA4IoT . Contracts . Conditions ;
11+ using HA4IoT . Contracts . Hardware ;
1112using HA4IoT . Contracts . Sensors ;
1213using HA4IoT . Contracts . Services . Daylight ;
1314using 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}
0 commit comments