add before/after sunset/sunrise to state

This commit is contained in:
Sam Lewis
2022-11-06 16:10:05 -05:00
parent ba9132745e
commit 5afa301f36
3 changed files with 27 additions and 5 deletions

8
app.go
View File

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

View File

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

View File

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