mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 03:15:14 -06:00
add before/after sunset/sunrise to state
This commit is contained in:
8
app.go
8
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"
|
printString := "Sunset"
|
||||||
attrKey := "next_setting"
|
attrKey := "next_setting"
|
||||||
if sunrise {
|
if sunrise {
|
||||||
@@ -149,7 +149,7 @@ func getSunriseSunset(a *App, sunrise bool, offset []DurationString) carbon.Carb
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get next sunrise/sunset time from HA
|
// get next sunrise/sunset time from HA
|
||||||
state, err := a.state.Get("sun.sun")
|
state, err := s.Get("sun.sun")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("Couldn't get sun.sun state from HA to calculate %s", printString))
|
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
|
return nextSetOrRise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSunriseSunsetFromApp(a *App, sunrise bool, offset ...DurationString) carbon.Carbon {
|
||||||
|
return getSunriseSunsetFromState(a.state, sunrise, offset...)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) Start() {
|
func (a *App) Start() {
|
||||||
log.Default().Println("Starting", a.schedules.Len(), "schedules")
|
log.Default().Println("Starting", a.schedules.Len(), "schedules")
|
||||||
log.Default().Println("Starting", len(a.entityListeners), "entity listeners")
|
log.Default().Println("Starting", len(a.entityListeners), "entity listeners")
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ func (sb scheduleBuilderDaily) At(s string) scheduleBuilderEnd {
|
|||||||
// Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration
|
// Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration
|
||||||
// for full list.
|
// for full list.
|
||||||
func (sb scheduleBuilderDaily) Sunrise(a *App, offset ...DurationString) scheduleBuilderEnd {
|
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
|
sb.schedule.isSunrise = true
|
||||||
return scheduleBuilderEnd(sb)
|
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
|
// Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration
|
||||||
// for full list.
|
// for full list.
|
||||||
func (sb scheduleBuilderDaily) Sunset(a *App, offset ...DurationString) scheduleBuilderEnd {
|
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
|
sb.schedule.isSunset = true
|
||||||
return scheduleBuilderEnd(sb)
|
return scheduleBuilderEnd(sb)
|
||||||
}
|
}
|
||||||
@@ -200,7 +200,7 @@ func popSchedule(a *App) Schedule {
|
|||||||
|
|
||||||
func requeueSchedule(a *App, s Schedule) {
|
func requeueSchedule(a *App, s Schedule) {
|
||||||
if s.isSunrise || s.isSunset {
|
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
|
// 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
|
// HA still shows today's sunset as next sunset. Just add 24h as a default handler
|
||||||
|
|||||||
18
state.go
18
state.go
@@ -40,3 +40,21 @@ func (s *State) Equals(entityId string, expectedState string) (bool, error) {
|
|||||||
}
|
}
|
||||||
return currentState.State == expectedState, nil
|
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...)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user