mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 01:15:10 -06:00
DRY duration parsing
This commit is contained in:
3
app.go
3
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]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
12
schedule.go
12
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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user