diff --git a/app.go b/app.go index 826dba8..a3b0577 100644 --- a/app.go +++ b/app.go @@ -3,7 +3,6 @@ package gomeassistant import ( "context" "fmt" - "log" "os" "time" @@ -133,7 +132,7 @@ func getSunriseSunset(a *app, sunrise bool, offset []DurationString) carbon.Carb if len(offset) == 1 { t, err = time.ParseDuration(string(offset[0])) if err != nil { - log.Fatalf("Could not parse offset passed to %s: \"%s\"", printString, offset[0]) + panic(fmt.Sprintf("Could not parse offset passed to %s: \"%s\"", printString, offset[0])) } } diff --git a/entitylistener.go b/entitylistener.go index ff1f1ae..3c3d281 100644 --- a/entitylistener.go +++ b/entitylistener.go @@ -2,10 +2,10 @@ package gomeassistant import ( "encoding/json" - "log" "time" "github.com/golang-module/carbon" + "github.com/saml-dev/gome-assistant/internal" ) type EntityListener struct { @@ -114,23 +114,13 @@ func (b elBuilder3) ToState(s string) elBuilder3 { } func (b elBuilder3) Duration(s DurationString) elBuilder3 { - // TODO: test this, should rename duration? not sure if Delay implies that state change cancels the callback - // if change name to Duration then enforce being used with ToState, should FromState be allowed? - // if FromState set to 3, then state changes to 2 and changes again to 1 halfway through delay, should the - // delay reset? - d, err := time.ParseDuration(string(s)) - if err != nil { - log.Fatalf("Couldn't parse string duration passed to For(): \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s) - } + d := internal.ParseDuration(string(s)) b.entityListener.delay = d return b } func (b elBuilder3) Throttle(s DurationString) elBuilder3 { - d, err := time.ParseDuration(string(s)) - if err != nil { - log.Fatalf("Couldn't parse string duration passed to Throttle(): \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s) - } + d := internal.ParseDuration(string(s)) b.entityListener.throttle = d return b } diff --git a/eventListener.go b/eventListener.go index 915c515..5821f0d 100644 --- a/eventListener.go +++ b/eventListener.go @@ -2,10 +2,10 @@ package gomeassistant import ( "encoding/json" - "log" "time" "github.com/golang-module/carbon" + "github.com/saml-dev/gome-assistant/internal" ws "github.com/saml-dev/gome-assistant/internal/websocket" ) @@ -72,10 +72,7 @@ func (b eventListenerBuilder3) OnlyBefore(end string) eventListenerBuilder3 { } func (b eventListenerBuilder3) Throttle(s DurationString) eventListenerBuilder3 { - d, err := time.ParseDuration(string(s)) - if err != nil { - log.Fatalf("Couldn't parse string duration passed to Throttle(): \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s) - } + d := internal.ParseDuration(string(s)) b.eventListener.throttle = d return b } diff --git a/internal/internal.go b/internal/internal.go index 0ad2913..92812ea 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -1,6 +1,7 @@ package internal import ( + "fmt" "log" "time" @@ -21,3 +22,11 @@ func ParseTime(s string) carbon.Carbon { } return carbon.Now().StartOfDay().SetHour(t.Hour()).SetMinute(t.Minute()) } + +func ParseDuration(s string) time.Duration { + d, err := time.ParseDuration(s) + if err != nil { + panic(fmt.Sprintf("Couldn't parse string duration: \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s)) + } + return d +} diff --git a/schedule.go b/schedule.go index 45943d8..825bdad 100644 --- a/schedule.go +++ b/schedule.go @@ -114,20 +114,14 @@ func (sb scheduleBuilderDaily) Sunset(a *app, offset ...DurationString) schedule } func (sb scheduleBuilderCall) Every(s DurationString) scheduleBuilderCustom { - d, err := time.ParseDuration(string(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) - } + d := internal.ParseDuration(string(s)) sb.schedule.frequency = d return scheduleBuilderCustom(sb) } func (sb scheduleBuilderCustom) Offset(s DurationString) scheduleBuilderEnd { - t, err := time.ParseDuration(string(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 + d := internal.ParseDuration(string(s)) + sb.schedule.offset = d return scheduleBuilderEnd(sb) }