mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-07 20:07:13 -06:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a8d4cefaab | |||
| 7081d06001 | |||
| 55a390e69c |
5
app.go
5
app.go
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/Workiva/go-datastructures/queue"
|
||||
"github.com/Xevion/gome-assistant/internal"
|
||||
"github.com/Xevion/gome-assistant/internal/http"
|
||||
"github.com/Xevion/gome-assistant/internal/parse"
|
||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
||||
)
|
||||
|
||||
@@ -273,7 +274,7 @@ func (a *App) RegisterIntervals(intervals ...Interval) {
|
||||
panic(ErrInvalidArgs)
|
||||
}
|
||||
|
||||
i.nextRunTime = internal.ParseTime(string(i.startTime)).Carbon2Time()
|
||||
i.nextRunTime = parse.ParseTime(string(i.startTime)).Carbon2Time()
|
||||
now := time.Now()
|
||||
for i.nextRunTime.Before(now) {
|
||||
i.nextRunTime = i.nextRunTime.Add(i.frequency)
|
||||
@@ -369,7 +370,7 @@ func (a *App) Start() {
|
||||
go runIntervals(a)
|
||||
|
||||
// subscribe to state_changed events
|
||||
id := internal.GetId()
|
||||
id := internal.NextId()
|
||||
ws.SubscribeToStateChangedEvents(id, a.wsWriter, a.ctx)
|
||||
a.entityListenersId = id
|
||||
|
||||
|
||||
11
checkers.go
11
checkers.go
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Xevion/gome-assistant/internal"
|
||||
"github.com/Xevion/gome-assistant/internal/parse"
|
||||
"github.com/golang-module/carbon"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,8 @@ func checkWithinTimeRange(startTime, endTime string) conditionCheck {
|
||||
// if betweenStart and betweenEnd both set, first account for midnight
|
||||
// overlap, then check if between those times.
|
||||
if startTime != "" && endTime != "" {
|
||||
parsedStart := internal.ParseTime(startTime)
|
||||
parsedEnd := internal.ParseTime(endTime)
|
||||
parsedStart := parse.ParseTime(startTime)
|
||||
parsedEnd := parse.ParseTime(endTime)
|
||||
|
||||
// check for midnight overlap
|
||||
if parsedEnd.Lt(parsedStart) { // example turn on night lights when motion from 23:00 to 07:00
|
||||
@@ -34,9 +35,9 @@ func checkWithinTimeRange(startTime, endTime string) conditionCheck {
|
||||
}
|
||||
|
||||
// otherwise just check individual before/after
|
||||
} else if startTime != "" && internal.ParseTime(startTime).IsFuture() {
|
||||
} else if startTime != "" && parse.ParseTime(startTime).IsFuture() {
|
||||
cc.fail = true
|
||||
} else if endTime != "" && internal.ParseTime(endTime).IsPast() {
|
||||
} else if endTime != "" && parse.ParseTime(endTime).IsPast() {
|
||||
cc.fail = true
|
||||
}
|
||||
return cc
|
||||
@@ -168,7 +169,7 @@ func checkStartEndTime(s TimeString, isStart bool) conditionCheck {
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
parsedTime := internal.ParseTime(string(s)).Carbon2Time()
|
||||
parsedTime := parse.ParseTime(string(s)).Carbon2Time()
|
||||
if isStart {
|
||||
if parsedTime.After(now) {
|
||||
cc.fail = true
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/golang-module/carbon"
|
||||
|
||||
"github.com/Xevion/gome-assistant/internal"
|
||||
"github.com/Xevion/gome-assistant/internal/parse"
|
||||
)
|
||||
|
||||
type EntityListener struct {
|
||||
@@ -127,13 +128,13 @@ func (b elBuilder3) ToState(s string) elBuilder3 {
|
||||
}
|
||||
|
||||
func (b elBuilder3) Duration(s DurationString) elBuilder3 {
|
||||
d := internal.ParseDuration(string(s))
|
||||
d := parse.ParseDuration(string(s))
|
||||
b.entityListener.delay = d
|
||||
return b
|
||||
}
|
||||
|
||||
func (b elBuilder3) Throttle(s DurationString) elBuilder3 {
|
||||
d := internal.ParseDuration(string(s))
|
||||
d := parse.ParseDuration(string(s))
|
||||
b.entityListener.throttle = d
|
||||
return b
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/golang-module/carbon"
|
||||
|
||||
"github.com/Xevion/gome-assistant/internal"
|
||||
"github.com/Xevion/gome-assistant/internal/parse"
|
||||
ws "github.com/Xevion/gome-assistant/internal/websocket"
|
||||
)
|
||||
|
||||
@@ -80,7 +81,7 @@ func (b eventListenerBuilder3) OnlyBefore(end string) eventListenerBuilder3 {
|
||||
}
|
||||
|
||||
func (b eventListenerBuilder3) Throttle(s DurationString) eventListenerBuilder3 {
|
||||
d := internal.ParseDuration(string(s))
|
||||
d := parse.ParseDuration(string(s))
|
||||
b.eventListener.throttle = d
|
||||
return b
|
||||
}
|
||||
28
internal/misc.go
Normal file
28
internal/misc.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type EnabledDisabledInfo struct {
|
||||
Entity string
|
||||
State string
|
||||
RunOnError bool
|
||||
}
|
||||
|
||||
var (
|
||||
id atomic.Int64 // default value is 0
|
||||
)
|
||||
|
||||
// NextId returns a unique integer (for the given process), often used for providing a uniquely identifiable
|
||||
// id for a request. This function is thread-safe.
|
||||
func NextId() int64 {
|
||||
return id.Add(1)
|
||||
}
|
||||
|
||||
// GetFunctionName returns the name of the function that the interface is a pointer to.
|
||||
func GetFunctionName(i interface{}) string {
|
||||
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
||||
}
|
||||
@@ -1,30 +1,13 @@
|
||||
package internal
|
||||
package parse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/golang-module/carbon"
|
||||
)
|
||||
|
||||
type EnabledDisabledInfo struct {
|
||||
Entity string
|
||||
State string
|
||||
RunOnError bool
|
||||
}
|
||||
|
||||
var (
|
||||
id atomic.Int64 // default value is 0
|
||||
)
|
||||
|
||||
func GetId() int64 {
|
||||
return id.Add(1)
|
||||
}
|
||||
|
||||
// Parses a HH:MM string.
|
||||
func ParseTime(s string) carbon.Carbon {
|
||||
t, err := time.Parse("15:04", s)
|
||||
@@ -45,7 +28,3 @@ func ParseDuration(s string) time.Duration {
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func GetFunctionName(i interface{}) string {
|
||||
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
||||
}
|
||||
@@ -23,7 +23,7 @@ type FireEventRequest struct {
|
||||
// as `event_data`.
|
||||
func (e Event) Fire(eventType string, eventData ...map[string]any) error {
|
||||
req := FireEventRequest{
|
||||
Id: internal.GetId(),
|
||||
Id: internal.NextId(),
|
||||
Type: "fire_event",
|
||||
}
|
||||
|
||||
|
||||
@@ -45,13 +45,15 @@ type BaseServiceRequest struct {
|
||||
}
|
||||
|
||||
func NewBaseServiceRequest(entityId string) BaseServiceRequest {
|
||||
id := internal.GetId()
|
||||
bsr := BaseServiceRequest{
|
||||
id := internal.NextId()
|
||||
request := BaseServiceRequest{
|
||||
Id: id,
|
||||
RequestType: "call_service",
|
||||
}
|
||||
|
||||
if entityId != "" {
|
||||
bsr.Target.EntityId = entityId
|
||||
request.Target.EntityId = entityId
|
||||
}
|
||||
return bsr
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ func SubscribeToStateChangedEvents(id int64, conn *WebsocketWriter, ctx context.
|
||||
func SubscribeToEventType(eventType string, conn *WebsocketWriter, ctx context.Context, id ...int64) {
|
||||
var finalId int64
|
||||
if len(id) == 0 {
|
||||
finalId = internal.GetId()
|
||||
finalId = internal.NextId()
|
||||
} else {
|
||||
finalId = id[0]
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Xevion/gome-assistant/internal"
|
||||
"github.com/Xevion/gome-assistant/internal/parse"
|
||||
)
|
||||
|
||||
type IntervalCallback func(*Service, State)
|
||||
@@ -80,7 +81,7 @@ func (ib intervalBuilder) Call(callback IntervalCallback) intervalBuilderCall {
|
||||
|
||||
// Takes a DurationString ("2h", "5m", etc) to set the frequency of the interval.
|
||||
func (ib intervalBuilderCall) Every(s DurationString) intervalBuilderEnd {
|
||||
d := internal.ParseDuration(string(s))
|
||||
d := parse.ParseDuration(string(s))
|
||||
ib.interval.frequency = d
|
||||
return intervalBuilderEnd(ib)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Xevion/gome-assistant/internal"
|
||||
"github.com/Xevion/gome-assistant/internal/parse"
|
||||
"github.com/golang-module/carbon"
|
||||
)
|
||||
|
||||
@@ -75,7 +76,7 @@ func (sb scheduleBuilder) Call(callback ScheduleCallback) scheduleBuilderCall {
|
||||
|
||||
// At takes a string in 24hr format time like "15:30".
|
||||
func (sb scheduleBuilderCall) At(s string) scheduleBuilderEnd {
|
||||
t := internal.ParseTime(s)
|
||||
t := parse.ParseTime(s)
|
||||
sb.schedule.hour = t.Hour()
|
||||
sb.schedule.minute = t.Minute()
|
||||
return scheduleBuilderEnd(sb)
|
||||
|
||||
Reference in New Issue
Block a user