diff --git a/app.go b/app.go index 40b7931..372b3ad 100644 --- a/app.go +++ b/app.go @@ -131,7 +131,7 @@ func (a *App) RegisterEventListeners(evls ...EventListener) { } } -func getSunriseSunset(a *App, sunrise bool, offset []DurationString) carbon.Carbon { +func getSunriseSunsetFromState(s *State, sunrise bool, offset ...DurationString) carbon.Carbon { printString := "Sunset" attrKey := "next_setting" if sunrise { @@ -149,7 +149,7 @@ func getSunriseSunset(a *App, sunrise bool, offset []DurationString) carbon.Carb } // get next sunrise/sunset time from HA - state, err := a.state.Get("sun.sun") + state, err := s.Get("sun.sun") if err != nil { panic(fmt.Sprintf("Couldn't get sun.sun state from HA to calculate %s", printString)) } @@ -164,6 +164,10 @@ func getSunriseSunset(a *App, sunrise bool, offset []DurationString) carbon.Carb return nextSetOrRise } +func getSunriseSunsetFromApp(a *App, sunrise bool, offset ...DurationString) carbon.Carbon { + return getSunriseSunsetFromState(a.state, sunrise, offset...) +} + func (a *App) Start() { log.Default().Println("Starting", a.schedules.Len(), "schedules") log.Default().Println("Starting", len(a.entityListeners), "entity listeners") diff --git a/schedule.go b/schedule.go index b2e415a..47fdc49 100644 --- a/schedule.go +++ b/schedule.go @@ -101,7 +101,7 @@ func (sb scheduleBuilderDaily) At(s string) scheduleBuilderEnd { // Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration // for full list. func (sb scheduleBuilderDaily) Sunrise(a *App, offset ...DurationString) scheduleBuilderEnd { - sb.schedule.realStartTime = getSunriseSunset(a, true, offset).Carbon2Time() + sb.schedule.realStartTime = getSunriseSunsetFromApp(a, true, offset...).Carbon2Time() sb.schedule.isSunrise = true return scheduleBuilderEnd(sb) } @@ -110,7 +110,7 @@ func (sb scheduleBuilderDaily) Sunrise(a *App, offset ...DurationString) schedul // Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration // for full list. func (sb scheduleBuilderDaily) Sunset(a *App, offset ...DurationString) scheduleBuilderEnd { - sb.schedule.realStartTime = getSunriseSunset(a, false, offset).Carbon2Time() + sb.schedule.realStartTime = getSunriseSunsetFromApp(a, false, offset...).Carbon2Time() sb.schedule.isSunset = true return scheduleBuilderEnd(sb) } @@ -200,7 +200,7 @@ func popSchedule(a *App) Schedule { func requeueSchedule(a *App, s Schedule) { if s.isSunrise || s.isSunset { - nextSunTime := getSunriseSunset(a, s.isSunrise, []DurationString{s.sunOffset}) + nextSunTime := getSunriseSunsetFromApp(a, s.isSunrise, s.sunOffset) // this is true when there is a negative offset, so schedule runs before sunset/sunrise and // HA still shows today's sunset as next sunset. Just add 24h as a default handler diff --git a/state.go b/state.go index 9e6f02b..17a0ae7 100644 --- a/state.go +++ b/state.go @@ -40,3 +40,21 @@ func (s *State) Equals(entityId string, expectedState string) (bool, error) { } return currentState.State == expectedState, nil } + +func (s *State) BeforeSunrise(offset ...DurationString) bool { + sunrise := getSunriseSunsetFromState(s, true, offset...) + return sunrise.IsToday() +} + +func (s *State) AfterSunrise(offset ...DurationString) bool { + return !s.BeforeSunrise(offset...) +} + +func (s *State) BeforeSunset(offset ...DurationString) bool { + sunset := getSunriseSunsetFromState(s, false, offset...) + return sunset.IsToday() +} + +func (s *State) AfterSunset(offset ...DurationString) bool { + return !s.BeforeSunset(offset...) +}