all builders now take duration strings

This commit is contained in:
Sam Lewis
2022-10-16 22:45:13 -04:00
parent 6ca78e88dd
commit 145c12dd25
5 changed files with 31 additions and 40 deletions

22
app.go
View File

@@ -29,12 +29,6 @@ type app struct {
entityListenersId int64
}
/*
Time is a 24-hr format string with hour and minute,
e.g. "07:00" for 7AM or "23:00" for 11PM.
*/
type Time string
/*
NewApp establishes the websocket connection and returns an object
you can use to register schedules and listeners.
@@ -67,10 +61,6 @@ func (a *app) Cleanup() {
}
func (a *app) RegisterSchedule(s schedule) {
if s.err != nil {
log.Fatalln(s.err) // something wasn't configured properly when the schedule was built
}
if s.frequency == 0 {
log.Fatalln("A schedule must call either Daily() or Every() when built.")
}
@@ -105,18 +95,18 @@ func (a *app) RegisterEntityListener(el entityListener) {
// Sunrise take an optional string that is passed to time.ParseDuration.
// Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration
// for full list.
func (a *app) Sunrise(offset ...string) Time {
func (a *app) Sunrise(offset ...string) string {
return getSunriseSunset(a, true, offset)
}
// Sunset take an optional string that is passed to time.ParseDuration.
// Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration
// for full list.
func (a *app) Sunset(offset ...string) Time {
func (a *app) Sunset(offset ...string) string {
return getSunriseSunset(a, false, offset)
}
func getSunriseSunset(a *app, sunrise bool, offset []string) Time {
func getSunriseSunset(a *app, sunrise bool, offset []string) string {
printString := "Sunset"
attrKey := "next_setting"
if sunrise {
@@ -138,19 +128,17 @@ func getSunriseSunset(a *app, sunrise bool, offset []string) Time {
}
nextSetOrRise := carbon.Parse(state.Attributes[attrKey].(string))
log.Default().Println(nextSetOrRise)
// add offset if set, this code works for negative values too
if t.Microseconds() != 0 {
nextSetOrRise = nextSetOrRise.AddMinutes(int(t.Minutes()))
log.Default().Println(nextSetOrRise)
}
return carbon2TimeString(nextSetOrRise)
}
func carbon2TimeString(c carbon.Carbon) Time {
return Time(fmt.Sprintf("%02d:%02d", c.Hour(), c.Minute()))
func carbon2TimeString(c carbon.Carbon) string {
return fmt.Sprintf("%02d:%02d", c.Hour(), c.Minute())
}
type subEvent struct {

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
ga "github.com/saml-dev/gome-assistant"
@@ -10,7 +11,10 @@ func main() {
app := ga.NewApp("192.168.86.67:8123")
defer app.Cleanup()
s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(app.Sunset("1h")).Build()
s2 := ga.ScheduleBuilder().Call(lightsOut).Every("2h").Offset("10m").Build()
fmt.Println(s2)
app.RegisterSchedule(s)
app.RegisterSchedule(s2)
simpleListener := ga.EntityListenerBuilder().
EntityIds("group.office_ceiling_lights").
Call(listenerCB).

View File

@@ -14,8 +14,8 @@ type entityListener struct {
callback entityListenerCallback
fromState string
toState string
betweenStart Time
betweenEnd Time
betweenStart string
betweenEnd string
err error
}
@@ -119,7 +119,7 @@ type elBuilder3 struct {
entityListener
}
func (b elBuilder3) OnlyBetween(start Time, end Time) elBuilder3 {
func (b elBuilder3) OnlyBetween(start string, end string) elBuilder3 {
b.entityListener.betweenStart = start
b.entityListener.betweenEnd = end
return b

View File

@@ -14,8 +14,8 @@ func GetId() int64 {
return id
}
func ParseTime[T ~string](s T) carbon.Carbon {
t, err := time.Parse("15:04", string(s))
func ParseTime(s string) carbon.Carbon {
t, err := time.Parse("15:04", s)
if err != nil {
log.Fatalf("Failed to parse time string \"%s\"; format must be HH:MM.", s)
}

View File

@@ -2,6 +2,7 @@ package gomeassistant
import (
"fmt"
"log"
"reflect"
"runtime"
"time"
@@ -24,23 +25,13 @@ type schedule struct {
frequency time.Duration
callback scheduleCallback
/*
offset is 4 character string representing hours and minutes
in a 24-hr format.
It is the base that your frequency will be added to.
Defaults to "0000" (which is probably fine for most cases).
offset is the base that your frequency will be added to.
Defaults to 0 (which is probably fine for most cases).
Example: Run in the 3rd minute of every hour.
Schedule{
frequency: gomeassistant.Hourly // helper const for time.Hour
offset: "0003"
}
ScheduleBuilder().Call(myFunc).Every("1h").Offset("3m")
*/
offset time.Duration
/*
err will be set rather than returning an error to avoid checking err for nil on every schedule :)
RegisterSchedule will exit if the error is set.
*/
err error
realStartTime time.Time
}
@@ -110,18 +101,26 @@ func (sb scheduleBuilderCall) Daily() scheduleBuilderDaily {
}
// At takes a string 24hr format time like "15:30".
func (sb scheduleBuilderDaily) At(s Time) scheduleBuilderEnd {
func (sb scheduleBuilderDaily) At(s string) scheduleBuilderEnd {
t := internal.ParseTime(s)
sb.schedule.offset = time.Duration(t.Hour())*time.Hour + time.Duration(t.Minute())*time.Minute
return scheduleBuilderEnd(sb)
}
func (sb scheduleBuilderCall) Every(duration time.Duration) scheduleBuilderCustom {
sb.schedule.frequency = duration
func (sb scheduleBuilderCall) Every(s string) scheduleBuilderCustom {
d, err := time.ParseDuration(s)
if err != nil {
log.Fatalf("couldn't parse string duration passed to Every(): \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s)
}
sb.schedule.frequency = d
return scheduleBuilderCustom(sb)
}
func (sb scheduleBuilderCustom) Offset(t time.Duration) scheduleBuilderEnd {
func (sb scheduleBuilderCustom) Offset(s string) scheduleBuilderEnd {
t, err := time.ParseDuration(s)
if err != nil {
log.Fatalf("Couldn't parse string duration passed to Offset(): \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s)
}
sb.schedule.offset = t
return scheduleBuilderEnd(sb)
}