mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-07 20:07:13 -06:00
began refactor, added interface to set up for unit testing
This commit is contained in:
34
schedule.go
34
schedule.go
@@ -27,12 +27,8 @@ type DailySchedule struct {
|
||||
exceptionDates []time.Time
|
||||
allowlistDates []time.Time
|
||||
|
||||
enabledEntity string
|
||||
enabledEntityState string
|
||||
enabledEntityRunOnError bool
|
||||
disabledEntity string
|
||||
disabledEntityState string
|
||||
disabledEntityRunOnError bool
|
||||
enabledEntities []internal.EnabledDisabledInfo
|
||||
disabledEntities []internal.EnabledDisabledInfo
|
||||
}
|
||||
|
||||
func (s DailySchedule) Hash() string {
|
||||
@@ -125,12 +121,15 @@ func (sb scheduleBuilderEnd) EnabledWhen(entityId, state string, runOnNetworkErr
|
||||
if entityId == "" {
|
||||
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
|
||||
}
|
||||
if sb.schedule.disabledEntity != "" {
|
||||
if len(sb.schedule.disabledEntities) != 0 {
|
||||
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting EnabledWhen on a schedule with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
|
||||
}
|
||||
sb.schedule.enabledEntity = entityId
|
||||
sb.schedule.enabledEntityState = state
|
||||
sb.schedule.enabledEntityRunOnError = runOnNetworkError
|
||||
i := internal.EnabledDisabledInfo{
|
||||
Entity: entityId,
|
||||
State: state,
|
||||
RunOnError: runOnNetworkError,
|
||||
}
|
||||
sb.schedule.enabledEntities = append(sb.schedule.enabledEntities, i)
|
||||
return sb
|
||||
}
|
||||
|
||||
@@ -142,12 +141,15 @@ func (sb scheduleBuilderEnd) DisabledWhen(entityId, state string, runOnNetworkEr
|
||||
if entityId == "" {
|
||||
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
|
||||
}
|
||||
if sb.schedule.enabledEntity != "" {
|
||||
if len(sb.schedule.enabledEntities) != 0 {
|
||||
panic(fmt.Sprintf("You can't use EnabledWhen and DisabledWhen together. Error occurred while setting DisabledWhen on a schedule with params entityId=%s state=%s runOnNetworkError=%t", entityId, state, runOnNetworkError))
|
||||
}
|
||||
sb.schedule.disabledEntity = entityId
|
||||
sb.schedule.disabledEntityState = state
|
||||
sb.schedule.disabledEntityRunOnError = runOnNetworkError
|
||||
i := internal.EnabledDisabledInfo{
|
||||
Entity: entityId,
|
||||
State: state,
|
||||
RunOnError: runOnNetworkError,
|
||||
}
|
||||
sb.schedule.disabledEntities = append(sb.schedule.disabledEntities, i)
|
||||
return sb
|
||||
}
|
||||
|
||||
@@ -186,10 +188,10 @@ func (s DailySchedule) maybeRunCallback(a *App) {
|
||||
if c := checkAllowlistDates(s.allowlistDates); c.fail {
|
||||
return
|
||||
}
|
||||
if c := checkEnabledEntity(a.state, s.enabledEntity, s.enabledEntityState, s.enabledEntityRunOnError); c.fail {
|
||||
if c := checkEnabledEntity(a.state, s.enabledEntities); c.fail {
|
||||
return
|
||||
}
|
||||
if c := checkDisabledEntity(a.state, s.disabledEntity, s.disabledEntityState, s.disabledEntityRunOnError); c.fail {
|
||||
if c := checkDisabledEntity(a.state, s.disabledEntities); c.fail {
|
||||
return
|
||||
}
|
||||
go s.callback(a.service, a.state)
|
||||
|
||||
Reference in New Issue
Block a user