began refactor, added interface to set up for unit testing

This commit is contained in:
Sam Lewis
2023-10-22 00:56:12 -04:00
parent 025e7115fa
commit 3ec6608714
7 changed files with 79 additions and 49 deletions

View File

@@ -86,35 +86,60 @@ func checkExceptionRanges(eList []timeRange) conditionCheck {
return cc
}
func checkEnabledEntity(s *State, eid, expectedState string, runOnNetworkError bool) conditionCheck {
func checkEnabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck {
cc := conditionCheck{fail: false}
if eid == "" || expectedState == "" {
if len(infos) == 0 {
return cc
}
matches, err := s.Equals(eid, expectedState)
for _, edi := range infos {
matches, err := s.Equals(edi.Entity, edi.State)
if err != nil {
cc.fail = !runOnNetworkError
return cc
if edi.RunOnError {
// keep checking
continue
} else {
// don't run this automation
cc.fail = true
break
}
}
cc.fail = !matches
if !matches {
cc.fail = true
break
}
}
return cc
}
func checkDisabledEntity(s *State, eid, expectedState string, runOnNetworkError bool) conditionCheck {
func checkDisabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck {
cc := conditionCheck{fail: false}
if eid == "" || expectedState == "" {
if len(infos) == 0 {
return cc
}
matches, err := s.Equals(eid, expectedState)
for _, edi := range infos {
matches, err := s.Equals(edi.Entity, edi.State)
if err != nil {
cc.fail = !runOnNetworkError
return cc
if edi.RunOnError {
// keep checking
continue
} else {
// don't run this automation
cc.fail = true
break
}
}
if matches {
cc.fail = true
break
}
}
cc.fail = matches
return cc
}

View File

@@ -29,12 +29,8 @@ type EntityListener struct {
runOnStartup bool
runOnStartupCompleted bool
enabledEntity string
enabledEntityState string
enabledEntityRunOnError bool
disabledEntity string
disabledEntityState string
disabledEntityRunOnError bool
enabledEntities []internal.EnabledDisabledInfo
disabledEntities []internal.EnabledDisabledInfo
}
type EntityListenerCallback func(*Service, *State, EntityData)

View File

@@ -21,12 +21,8 @@ type EventListener struct {
exceptionDates []time.Time
exceptionRanges []timeRange
enabledEntity string
enabledEntityState string
enabledEntityRunOnError bool
disabledEntity string
disabledEntityState string
disabledEntityRunOnError bool
enabledEntities []internal.EnabledDisabledInfo
disabledEntities []internal.EnabledDisabledInfo
}
type EventListenerCallback func(*Service, *State, EventData)

View File

@@ -10,6 +10,12 @@ import (
"github.com/golang-module/carbon"
)
type EnabledDisabledInfo struct {
Entity string
State string
RunOnError bool
}
var id int64 = 0
func GetId() int64 {

View File

@@ -19,12 +19,8 @@ type Interval struct {
exceptionDates []time.Time
exceptionRanges []timeRange
enabledEntity string
enabledEntityState string
enabledEntityRunOnError bool
disabledEntity string
disabledEntityState string
disabledEntityRunOnError bool
enabledEntities []internal.EnabledDisabledInfo
disabledEntities []internal.EnabledDisabledInfo
}
func (i Interval) Hash() string {

View File

@@ -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)

View File

@@ -10,6 +10,15 @@ import (
"saml.dev/gome-assistant/internal/http"
)
type StateInterface interface {
AfterSunrise() bool
BeforeSunrise() bool
AfterSunset() bool
BeforeSunset() bool
Get() (EntityState, error)
Equals() (bool, error)
}
// State is used to retrieve state from Home Assistant.
type State struct {
httpClient *http.HttpClient