mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-10 04:07:28 -06:00
all builders now take duration strings
This commit is contained in:
22
app.go
22
app.go
@@ -29,12 +29,6 @@ type app struct {
|
|||||||
entityListenersId int64
|
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
|
NewApp establishes the websocket connection and returns an object
|
||||||
you can use to register schedules and listeners.
|
you can use to register schedules and listeners.
|
||||||
@@ -67,10 +61,6 @@ func (a *app) Cleanup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) RegisterSchedule(s schedule) {
|
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 {
|
if s.frequency == 0 {
|
||||||
log.Fatalln("A schedule must call either Daily() or Every() when built.")
|
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.
|
// Sunrise take an optional string that is passed to time.ParseDuration.
|
||||||
// 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 (a *app) Sunrise(offset ...string) Time {
|
func (a *app) Sunrise(offset ...string) string {
|
||||||
return getSunriseSunset(a, true, offset)
|
return getSunriseSunset(a, true, offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sunset take an optional string that is passed to time.ParseDuration.
|
// Sunset take an optional string that is passed to time.ParseDuration.
|
||||||
// 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 (a *app) Sunset(offset ...string) Time {
|
func (a *app) Sunset(offset ...string) string {
|
||||||
return getSunriseSunset(a, false, offset)
|
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"
|
printString := "Sunset"
|
||||||
attrKey := "next_setting"
|
attrKey := "next_setting"
|
||||||
if sunrise {
|
if sunrise {
|
||||||
@@ -138,19 +128,17 @@ func getSunriseSunset(a *app, sunrise bool, offset []string) Time {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nextSetOrRise := carbon.Parse(state.Attributes[attrKey].(string))
|
nextSetOrRise := carbon.Parse(state.Attributes[attrKey].(string))
|
||||||
log.Default().Println(nextSetOrRise)
|
|
||||||
|
|
||||||
// add offset if set, this code works for negative values too
|
// add offset if set, this code works for negative values too
|
||||||
if t.Microseconds() != 0 {
|
if t.Microseconds() != 0 {
|
||||||
nextSetOrRise = nextSetOrRise.AddMinutes(int(t.Minutes()))
|
nextSetOrRise = nextSetOrRise.AddMinutes(int(t.Minutes()))
|
||||||
log.Default().Println(nextSetOrRise)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return carbon2TimeString(nextSetOrRise)
|
return carbon2TimeString(nextSetOrRise)
|
||||||
}
|
}
|
||||||
|
|
||||||
func carbon2TimeString(c carbon.Carbon) Time {
|
func carbon2TimeString(c carbon.Carbon) string {
|
||||||
return Time(fmt.Sprintf("%02d:%02d", c.Hour(), c.Minute()))
|
return fmt.Sprintf("%02d:%02d", c.Hour(), c.Minute())
|
||||||
}
|
}
|
||||||
|
|
||||||
type subEvent struct {
|
type subEvent struct {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
ga "github.com/saml-dev/gome-assistant"
|
ga "github.com/saml-dev/gome-assistant"
|
||||||
@@ -10,7 +11,10 @@ func main() {
|
|||||||
app := ga.NewApp("192.168.86.67:8123")
|
app := ga.NewApp("192.168.86.67:8123")
|
||||||
defer app.Cleanup()
|
defer app.Cleanup()
|
||||||
s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(app.Sunset("1h")).Build()
|
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(s)
|
||||||
|
app.RegisterSchedule(s2)
|
||||||
simpleListener := ga.EntityListenerBuilder().
|
simpleListener := ga.EntityListenerBuilder().
|
||||||
EntityIds("group.office_ceiling_lights").
|
EntityIds("group.office_ceiling_lights").
|
||||||
Call(listenerCB).
|
Call(listenerCB).
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ type entityListener struct {
|
|||||||
callback entityListenerCallback
|
callback entityListenerCallback
|
||||||
fromState string
|
fromState string
|
||||||
toState string
|
toState string
|
||||||
betweenStart Time
|
betweenStart string
|
||||||
betweenEnd Time
|
betweenEnd string
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ type elBuilder3 struct {
|
|||||||
entityListener
|
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.betweenStart = start
|
||||||
b.entityListener.betweenEnd = end
|
b.entityListener.betweenEnd = end
|
||||||
return b
|
return b
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ func GetId() int64 {
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseTime[T ~string](s T) carbon.Carbon {
|
func ParseTime(s string) carbon.Carbon {
|
||||||
t, err := time.Parse("15:04", string(s))
|
t, err := time.Parse("15:04", s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to parse time string \"%s\"; format must be HH:MM.", s)
|
log.Fatalf("Failed to parse time string \"%s\"; format must be HH:MM.", s)
|
||||||
}
|
}
|
||||||
|
|||||||
33
schedule.go
33
schedule.go
@@ -2,6 +2,7 @@ package gomeassistant
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
@@ -24,23 +25,13 @@ type schedule struct {
|
|||||||
frequency time.Duration
|
frequency time.Duration
|
||||||
callback scheduleCallback
|
callback scheduleCallback
|
||||||
/*
|
/*
|
||||||
offset is 4 character string representing hours and minutes
|
offset is the base that your frequency will be added to.
|
||||||
in a 24-hr format.
|
Defaults to 0 (which is probably fine for most cases).
|
||||||
It is the base that your frequency will be added to.
|
|
||||||
Defaults to "0000" (which is probably fine for most cases).
|
|
||||||
|
|
||||||
Example: Run in the 3rd minute of every hour.
|
Example: Run in the 3rd minute of every hour.
|
||||||
Schedule{
|
ScheduleBuilder().Call(myFunc).Every("1h").Offset("3m")
|
||||||
frequency: gomeassistant.Hourly // helper const for time.Hour
|
|
||||||
offset: "0003"
|
|
||||||
}
|
|
||||||
*/
|
*/
|
||||||
offset time.Duration
|
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
|
realStartTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,18 +101,26 @@ func (sb scheduleBuilderCall) Daily() scheduleBuilderDaily {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// At takes a string 24hr format time like "15:30".
|
// 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)
|
t := internal.ParseTime(s)
|
||||||
sb.schedule.offset = time.Duration(t.Hour())*time.Hour + time.Duration(t.Minute())*time.Minute
|
sb.schedule.offset = time.Duration(t.Hour())*time.Hour + time.Duration(t.Minute())*time.Minute
|
||||||
return scheduleBuilderEnd(sb)
|
return scheduleBuilderEnd(sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb scheduleBuilderCall) Every(duration time.Duration) scheduleBuilderCustom {
|
func (sb scheduleBuilderCall) Every(s string) scheduleBuilderCustom {
|
||||||
sb.schedule.frequency = duration
|
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)
|
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
|
sb.schedule.offset = t
|
||||||
return scheduleBuilderEnd(sb)
|
return scheduleBuilderEnd(sb)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user