mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 01:15:10 -06:00
began refactor, added interface to set up for unit testing
This commit is contained in:
55
checkers.go
55
checkers.go
@@ -86,35 +86,60 @@ func checkExceptionRanges(eList []timeRange) conditionCheck {
|
|||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkEnabledEntity(s *State, eid, expectedState string, runOnNetworkError bool) conditionCheck {
|
func checkEnabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck {
|
||||||
cc := conditionCheck{fail: false}
|
cc := conditionCheck{fail: false}
|
||||||
if eid == "" || expectedState == "" {
|
if len(infos) == 0 {
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
matches, err := s.Equals(eid, expectedState)
|
for _, edi := range infos {
|
||||||
if err != nil {
|
matches, err := s.Equals(edi.Entity, edi.State)
|
||||||
cc.fail = !runOnNetworkError
|
|
||||||
return cc
|
|
||||||
}
|
|
||||||
|
|
||||||
cc.fail = !matches
|
if err != nil {
|
||||||
|
if edi.RunOnError {
|
||||||
|
// keep checking
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
// don't run this automation
|
||||||
|
cc.fail = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !matches {
|
||||||
|
cc.fail = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkDisabledEntity(s *State, eid, expectedState string, runOnNetworkError bool) conditionCheck {
|
func checkDisabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck {
|
||||||
cc := conditionCheck{fail: false}
|
cc := conditionCheck{fail: false}
|
||||||
if eid == "" || expectedState == "" {
|
if len(infos) == 0 {
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
matches, err := s.Equals(eid, expectedState)
|
for _, edi := range infos {
|
||||||
if err != nil {
|
matches, err := s.Equals(edi.Entity, edi.State)
|
||||||
cc.fail = !runOnNetworkError
|
|
||||||
return cc
|
if err != nil {
|
||||||
|
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
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,12 +29,8 @@ type EntityListener struct {
|
|||||||
runOnStartup bool
|
runOnStartup bool
|
||||||
runOnStartupCompleted bool
|
runOnStartupCompleted bool
|
||||||
|
|
||||||
enabledEntity string
|
enabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityState string
|
disabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityRunOnError bool
|
|
||||||
disabledEntity string
|
|
||||||
disabledEntityState string
|
|
||||||
disabledEntityRunOnError bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EntityListenerCallback func(*Service, *State, EntityData)
|
type EntityListenerCallback func(*Service, *State, EntityData)
|
||||||
|
|||||||
@@ -21,12 +21,8 @@ type EventListener struct {
|
|||||||
exceptionDates []time.Time
|
exceptionDates []time.Time
|
||||||
exceptionRanges []timeRange
|
exceptionRanges []timeRange
|
||||||
|
|
||||||
enabledEntity string
|
enabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityState string
|
disabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityRunOnError bool
|
|
||||||
disabledEntity string
|
|
||||||
disabledEntityState string
|
|
||||||
disabledEntityRunOnError bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventListenerCallback func(*Service, *State, EventData)
|
type EventListenerCallback func(*Service, *State, EventData)
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ import (
|
|||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type EnabledDisabledInfo struct {
|
||||||
|
Entity string
|
||||||
|
State string
|
||||||
|
RunOnError bool
|
||||||
|
}
|
||||||
|
|
||||||
var id int64 = 0
|
var id int64 = 0
|
||||||
|
|
||||||
func GetId() int64 {
|
func GetId() int64 {
|
||||||
|
|||||||
@@ -19,12 +19,8 @@ type Interval struct {
|
|||||||
exceptionDates []time.Time
|
exceptionDates []time.Time
|
||||||
exceptionRanges []timeRange
|
exceptionRanges []timeRange
|
||||||
|
|
||||||
enabledEntity string
|
enabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityState string
|
disabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityRunOnError bool
|
|
||||||
disabledEntity string
|
|
||||||
disabledEntityState string
|
|
||||||
disabledEntityRunOnError bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i Interval) Hash() string {
|
func (i Interval) Hash() string {
|
||||||
|
|||||||
34
schedule.go
34
schedule.go
@@ -27,12 +27,8 @@ type DailySchedule struct {
|
|||||||
exceptionDates []time.Time
|
exceptionDates []time.Time
|
||||||
allowlistDates []time.Time
|
allowlistDates []time.Time
|
||||||
|
|
||||||
enabledEntity string
|
enabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityState string
|
disabledEntities []internal.EnabledDisabledInfo
|
||||||
enabledEntityRunOnError bool
|
|
||||||
disabledEntity string
|
|
||||||
disabledEntityState string
|
|
||||||
disabledEntityRunOnError bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s DailySchedule) Hash() string {
|
func (s DailySchedule) Hash() string {
|
||||||
@@ -125,12 +121,15 @@ func (sb scheduleBuilderEnd) EnabledWhen(entityId, state string, runOnNetworkErr
|
|||||||
if entityId == "" {
|
if entityId == "" {
|
||||||
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
|
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))
|
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
|
i := internal.EnabledDisabledInfo{
|
||||||
sb.schedule.enabledEntityState = state
|
Entity: entityId,
|
||||||
sb.schedule.enabledEntityRunOnError = runOnNetworkError
|
State: state,
|
||||||
|
RunOnError: runOnNetworkError,
|
||||||
|
}
|
||||||
|
sb.schedule.enabledEntities = append(sb.schedule.enabledEntities, i)
|
||||||
return sb
|
return sb
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,12 +141,15 @@ func (sb scheduleBuilderEnd) DisabledWhen(entityId, state string, runOnNetworkEr
|
|||||||
if entityId == "" {
|
if entityId == "" {
|
||||||
panic(fmt.Sprintf("entityId is empty in EnabledWhen entityId='%s' state='%s'", entityId, state))
|
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))
|
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
|
i := internal.EnabledDisabledInfo{
|
||||||
sb.schedule.disabledEntityState = state
|
Entity: entityId,
|
||||||
sb.schedule.disabledEntityRunOnError = runOnNetworkError
|
State: state,
|
||||||
|
RunOnError: runOnNetworkError,
|
||||||
|
}
|
||||||
|
sb.schedule.disabledEntities = append(sb.schedule.disabledEntities, i)
|
||||||
return sb
|
return sb
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,10 +188,10 @@ func (s DailySchedule) maybeRunCallback(a *App) {
|
|||||||
if c := checkAllowlistDates(s.allowlistDates); c.fail {
|
if c := checkAllowlistDates(s.allowlistDates); c.fail {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c := checkEnabledEntity(a.state, s.enabledEntity, s.enabledEntityState, s.enabledEntityRunOnError); c.fail {
|
if c := checkEnabledEntity(a.state, s.enabledEntities); c.fail {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c := checkDisabledEntity(a.state, s.disabledEntity, s.disabledEntityState, s.disabledEntityRunOnError); c.fail {
|
if c := checkDisabledEntity(a.state, s.disabledEntities); c.fail {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go s.callback(a.service, a.state)
|
go s.callback(a.service, a.state)
|
||||||
|
|||||||
9
state.go
9
state.go
@@ -10,6 +10,15 @@ import (
|
|||||||
"saml.dev/gome-assistant/internal/http"
|
"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.
|
// State is used to retrieve state from Home Assistant.
|
||||||
type State struct {
|
type State struct {
|
||||||
httpClient *http.HttpClient
|
httpClient *http.HttpClient
|
||||||
|
|||||||
Reference in New Issue
Block a user