refactor: move event_types into types/ pkg, make methods public, small fixes

This commit is contained in:
2025-08-01 18:16:00 -05:00
parent 21358b73e1
commit 26b8892ff6
7 changed files with 63 additions and 60 deletions

View File

@@ -9,12 +9,12 @@ import (
"github.com/golang-module/carbon"
)
type conditionCheck struct {
type ConditionCheck struct {
fail bool
}
func checkWithinTimeRange(startTime, endTime string) conditionCheck {
cc := conditionCheck{fail: false}
func CheckWithinTimeRange(startTime, endTime string) ConditionCheck {
cc := ConditionCheck{fail: false}
// if betweenStart and betweenEnd both set, first account for midnight
// overlap, then check if between those times.
if startTime != "" && endTime != "" {
@@ -44,8 +44,8 @@ func checkWithinTimeRange(startTime, endTime string) conditionCheck {
return cc
}
func checkStatesMatch(listenerState, s string) conditionCheck {
cc := conditionCheck{fail: false}
func CheckStatesMatch(listenerState, s string) ConditionCheck {
cc := ConditionCheck{fail: false}
// check if fromState or toState are set and don't match
if listenerState != "" && listenerState != s {
cc.fail = true
@@ -53,8 +53,8 @@ func checkStatesMatch(listenerState, s string) conditionCheck {
return cc
}
func checkThrottle(throttle time.Duration, lastRan carbon.Carbon) conditionCheck {
cc := conditionCheck{fail: false}
func CheckThrottle(throttle time.Duration, lastRan carbon.Carbon) ConditionCheck {
cc := ConditionCheck{fail: false}
// check if Throttle is set and that duration hasn't passed since lastRan
if throttle.Seconds() > 0 &&
lastRan.DiffAbsInSeconds(carbon.Now()) < int64(throttle.Seconds()) {
@@ -63,8 +63,8 @@ func checkThrottle(throttle time.Duration, lastRan carbon.Carbon) conditionCheck
return cc
}
func checkExceptionDates(eList []time.Time) conditionCheck {
cc := conditionCheck{fail: false}
func CheckExceptionDates(eList []time.Time) ConditionCheck {
cc := ConditionCheck{fail: false}
for _, e := range eList {
y1, m1, d1 := e.Date()
y2, m2, d2 := time.Now().Date()
@@ -76,8 +76,8 @@ func checkExceptionDates(eList []time.Time) conditionCheck {
return cc
}
func checkExceptionRanges(eList []types.TimeRange) conditionCheck {
cc := conditionCheck{fail: false}
func CheckExceptionRanges(eList []types.TimeRange) ConditionCheck {
cc := ConditionCheck{fail: false}
now := time.Now()
for _, eRange := range eList {
if now.After(eRange.Start) && now.Before(eRange.End) {
@@ -88,8 +88,8 @@ func checkExceptionRanges(eList []types.TimeRange) conditionCheck {
return cc
}
func checkEnabledEntity(s State, infos []internal.EnabledDisabledInfo) conditionCheck {
cc := conditionCheck{fail: false}
func CheckEnabledEntity(s State, infos []internal.EnabledDisabledInfo) ConditionCheck {
cc := ConditionCheck{fail: false}
if len(infos) == 0 {
return cc
}
@@ -116,8 +116,8 @@ func checkEnabledEntity(s State, infos []internal.EnabledDisabledInfo) condition
return cc
}
func checkDisabledEntity(s State, infos []internal.EnabledDisabledInfo) conditionCheck {
cc := conditionCheck{fail: false}
func CheckDisabledEntity(s State, infos []internal.EnabledDisabledInfo) ConditionCheck {
cc := ConditionCheck{fail: false}
if len(infos) == 0 {
return cc
}
@@ -145,12 +145,12 @@ func checkDisabledEntity(s State, infos []internal.EnabledDisabledInfo) conditio
return cc
}
func checkAllowlistDates(eList []time.Time) conditionCheck {
func CheckAllowlistDates(eList []time.Time) ConditionCheck {
if len(eList) == 0 {
return conditionCheck{fail: false}
return ConditionCheck{fail: false}
}
cc := conditionCheck{fail: true}
cc := ConditionCheck{fail: true}
for _, e := range eList {
y1, m1, d1 := e.Date()
y2, m2, d2 := time.Now().Date()
@@ -162,8 +162,8 @@ func checkAllowlistDates(eList []time.Time) conditionCheck {
return cc
}
func checkStartEndTime(s types.TimeString, isStart bool) conditionCheck {
cc := conditionCheck{fail: false}
func CheckStartEndTime(s types.TimeString, isStart bool) ConditionCheck {
cc := ConditionCheck{fail: false}
// pass immediately if default
if s == "00:00" {
return cc

View File

@@ -66,7 +66,7 @@ func TestEnabledEntity_StateEqual_Passes(t *testing.T) {
state := MockState{
EqualsReturn: true,
}
c := checkEnabledEntity(state, list(runOnError))
c := CheckEnabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should pass")
}
@@ -74,7 +74,7 @@ func TestEnabledEntity_StateNotEqual_Fails(t *testing.T) {
state := MockState{
EqualsReturn: false,
}
c := checkEnabledEntity(state, list(runOnError))
c := CheckEnabledEntity(state, list(runOnError))
assert.True(t, c.fail, "should fail")
}
@@ -82,7 +82,7 @@ func TestEnabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkEnabledEntity(state, list(dontRunOnError))
c := CheckEnabledEntity(state, list(dontRunOnError))
assert.True(t, c.fail, "should fail")
}
@@ -90,7 +90,7 @@ func TestEnabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkEnabledEntity(state, list(runOnError))
c := CheckEnabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should fail")
}
@@ -98,7 +98,7 @@ func TestDisabledEntity_StateEqual_Fails(t *testing.T) {
state := MockState{
EqualsReturn: true,
}
c := checkDisabledEntity(state, list(runOnError))
c := CheckDisabledEntity(state, list(runOnError))
assert.True(t, c.fail, "should pass")
}
@@ -106,7 +106,7 @@ func TestDisabledEntity_StateNotEqual_Passes(t *testing.T) {
state := MockState{
EqualsReturn: false,
}
c := checkDisabledEntity(state, list(runOnError))
c := CheckDisabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should fail")
}
@@ -114,7 +114,7 @@ func TestDisabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkDisabledEntity(state, list(dontRunOnError))
c := CheckDisabledEntity(state, list(dontRunOnError))
assert.True(t, c.fail, "should fail")
}
@@ -122,16 +122,16 @@ func TestDisabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkDisabledEntity(state, list(runOnError))
c := CheckDisabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should fail")
}
func TestStatesMatch(t *testing.T) {
c := checkStatesMatch("hey", "hey")
c := CheckStatesMatch("hey", "hey")
assert.False(t, c.fail, "should pass")
}
func TestStatesDontMatch(t *testing.T) {
c := checkStatesMatch("hey", "bye")
c := CheckStatesMatch("hey", "bye")
assert.True(t, c.fail, "should fail")
}

View File

@@ -146,7 +146,7 @@ func (b elBuilder3) ExceptionDates(t time.Time, tl ...time.Time) elBuilder3 {
}
func (b elBuilder3) ExceptionRange(start, end time.Time) elBuilder3 {
b.entityListener.exceptionRanges = append(b.entityListener.exceptionRanges, types.TimeRange{start, end})
b.entityListener.exceptionRanges = append(b.entityListener.exceptionRanges, types.TimeRange{Start: start, End: end})
return b
}
@@ -215,31 +215,31 @@ func callEntityListeners(app *App, msgBytes []byte) {
for _, l := range listeners {
// Check conditions
if c := checkWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
if c := CheckWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
continue
}
if c := checkStatesMatch(l.fromState, data.OldState.State); c.fail {
if c := CheckStatesMatch(l.fromState, data.OldState.State); c.fail {
continue
}
if c := checkStatesMatch(l.toState, data.NewState.State); c.fail {
if c := CheckStatesMatch(l.toState, data.NewState.State); c.fail {
if l.delayTimer != nil {
l.delayTimer.Stop()
}
continue
}
if c := checkThrottle(l.throttle, l.lastRan); c.fail {
if c := CheckThrottle(l.throttle, l.lastRan); c.fail {
continue
}
if c := checkExceptionDates(l.exceptionDates); c.fail {
if c := CheckExceptionDates(l.exceptionDates); c.fail {
continue
}
if c := checkExceptionRanges(l.exceptionRanges); c.fail {
if c := CheckExceptionRanges(l.exceptionRanges); c.fail {
continue
}
if c := checkEnabledEntity(app.state, l.enabledEntities); c.fail {
if c := CheckEnabledEntity(app.state, l.enabledEntities); c.fail {
continue
}
if c := checkDisabledEntity(app.state, l.disabledEntities); c.fail {
if c := CheckDisabledEntity(app.state, l.disabledEntities); c.fail {
continue
}

View File

@@ -93,7 +93,7 @@ func (b eventListenerBuilder3) ExceptionDates(t time.Time, tl ...time.Time) even
}
func (b eventListenerBuilder3) ExceptionRange(start, end time.Time) eventListenerBuilder3 {
b.eventListener.exceptionRanges = append(b.eventListener.exceptionRanges, types.TimeRange{start, end})
b.eventListener.exceptionRanges = append(b.eventListener.exceptionRanges, types.TimeRange{Start: start, End: end})
return b
}
@@ -153,22 +153,22 @@ func callEventListeners(app *App, msg ws.ChanMsg) {
for _, l := range listeners {
// Check conditions
if c := checkWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
if c := CheckWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
continue
}
if c := checkThrottle(l.throttle, l.lastRan); c.fail {
if c := CheckThrottle(l.throttle, l.lastRan); c.fail {
continue
}
if c := checkExceptionDates(l.exceptionDates); c.fail {
if c := CheckExceptionDates(l.exceptionDates); c.fail {
continue
}
if c := checkExceptionRanges(l.exceptionRanges); c.fail {
if c := CheckExceptionRanges(l.exceptionRanges); c.fail {
continue
}
if c := checkEnabledEntity(app.state, l.enabledEntities); c.fail {
if c := CheckEnabledEntity(app.state, l.enabledEntities); c.fail {
continue
}
if c := checkDisabledEntity(app.state, l.disabledEntities); c.fail {
if c := CheckDisabledEntity(app.state, l.disabledEntities); c.fail {
continue
}

View File

@@ -59,8 +59,8 @@ func (i Interval) String() string {
return fmt.Sprintf("Interval{ call %q every %s%s%s }",
internal.GetFunctionName(i.callback),
i.frequency,
formatStartOrEndString(i.startTime /* isStart = */, true),
formatStartOrEndString(i.endTime /* isStart = */, false),
formatStartOrEndString(i.startTime, true),
formatStartOrEndString(i.endTime, false),
)
}
@@ -105,7 +105,10 @@ func (ib intervalBuilderEnd) ExceptionDates(t time.Time, tl ...time.Time) interv
}
func (ib intervalBuilderEnd) ExceptionRange(start, end time.Time) intervalBuilderEnd {
ib.interval.exceptionRanges = append(ib.interval.exceptionRanges, types.TimeRange{start, end})
ib.interval.exceptionRanges = append(
ib.interval.exceptionRanges,
types.TimeRange{Start: start, End: end},
)
return ib
}
@@ -186,22 +189,22 @@ func runIntervals(a *App) {
}
func (i Interval) maybeRunCallback(a *App) {
if c := checkStartEndTime(i.startTime /* isStart = */, true); c.fail {
if c := CheckStartEndTime(i.startTime /* isStart = */, true); c.fail {
return
}
if c := checkStartEndTime(i.endTime /* isStart = */, false); c.fail {
if c := CheckStartEndTime(i.endTime /* isStart = */, false); c.fail {
return
}
if c := checkExceptionDates(i.exceptionDates); c.fail {
if c := CheckExceptionDates(i.exceptionDates); c.fail {
return
}
if c := checkExceptionRanges(i.exceptionRanges); c.fail {
if c := CheckExceptionRanges(i.exceptionRanges); c.fail {
return
}
if c := checkEnabledEntity(a.state, i.enabledEntities); c.fail {
if c := CheckEnabledEntity(a.state, i.enabledEntities); c.fail {
return
}
if c := checkDisabledEntity(a.state, i.disabledEntities); c.fail {
if c := CheckDisabledEntity(a.state, i.disabledEntities); c.fail {
return
}
go i.callback(a.service, a.state)

View File

@@ -194,16 +194,16 @@ func runSchedules(a *App) {
}
func (s DailySchedule) maybeRunCallback(a *App) {
if c := checkExceptionDates(s.exceptionDates); c.fail {
if c := CheckExceptionDates(s.exceptionDates); c.fail {
return
}
if c := checkAllowlistDates(s.allowlistDates); c.fail {
if c := CheckAllowlistDates(s.allowlistDates); c.fail {
return
}
if c := checkEnabledEntity(a.state, s.enabledEntities); c.fail {
if c := CheckEnabledEntity(a.state, s.enabledEntities); c.fail {
return
}
if c := checkDisabledEntity(a.state, s.disabledEntities); c.fail {
if c := CheckDisabledEntity(a.state, s.disabledEntities); c.fail {
return
}
go s.callback(a.service, a.state)

View File

@@ -1,4 +1,4 @@
package gomeassistant
package types
import "time"