mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 03:15:14 -06:00
DRY duration parsing
This commit is contained in:
3
app.go
3
app.go
@@ -3,7 +3,6 @@ package gomeassistant
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -133,7 +132,7 @@ func getSunriseSunset(a *app, sunrise bool, offset []DurationString) carbon.Carb
|
|||||||
if len(offset) == 1 {
|
if len(offset) == 1 {
|
||||||
t, err = time.ParseDuration(string(offset[0]))
|
t, err = time.ParseDuration(string(offset[0]))
|
||||||
if err != nil {
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
|
"github.com/saml-dev/gome-assistant/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EntityListener struct {
|
type EntityListener struct {
|
||||||
@@ -114,23 +114,13 @@ func (b elBuilder3) ToState(s string) elBuilder3 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b elBuilder3) Duration(s DurationString) 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
|
d := internal.ParseDuration(string(s))
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
b.entityListener.delay = d
|
b.entityListener.delay = d
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b elBuilder3) Throttle(s DurationString) elBuilder3 {
|
func (b elBuilder3) Throttle(s DurationString) elBuilder3 {
|
||||||
d, err := time.ParseDuration(string(s))
|
d := internal.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)
|
|
||||||
}
|
|
||||||
b.entityListener.throttle = d
|
b.entityListener.throttle = d
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package gomeassistant
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-module/carbon"
|
"github.com/golang-module/carbon"
|
||||||
|
"github.com/saml-dev/gome-assistant/internal"
|
||||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
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 {
|
func (b eventListenerBuilder3) Throttle(s DurationString) eventListenerBuilder3 {
|
||||||
d, err := time.ParseDuration(string(s))
|
d := internal.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)
|
|
||||||
}
|
|
||||||
b.eventListener.throttle = d
|
b.eventListener.throttle = d
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -21,3 +22,11 @@ func ParseTime(s string) carbon.Carbon {
|
|||||||
}
|
}
|
||||||
return carbon.Now().StartOfDay().SetHour(t.Hour()).SetMinute(t.Minute())
|
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 {
|
func (sb scheduleBuilderCall) Every(s DurationString) scheduleBuilderCustom {
|
||||||
d, err := time.ParseDuration(string(s))
|
d := internal.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)
|
|
||||||
}
|
|
||||||
sb.schedule.frequency = d
|
sb.schedule.frequency = d
|
||||||
return scheduleBuilderCustom(sb)
|
return scheduleBuilderCustom(sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb scheduleBuilderCustom) Offset(s DurationString) scheduleBuilderEnd {
|
func (sb scheduleBuilderCustom) Offset(s DurationString) scheduleBuilderEnd {
|
||||||
t, err := time.ParseDuration(string(s))
|
d := internal.ParseDuration(string(s))
|
||||||
if err != nil {
|
sb.schedule.offset = d
|
||||||
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)
|
return scheduleBuilderEnd(sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user