mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-07 01:15:15 -06:00
few things:
- easy comparison of hourMinute with .int() - created Sunset/Sunrise vars of type hourMinute for easy end-user use
This commit is contained in:
24
app.go
24
app.go
@@ -2,7 +2,6 @@ package gomeassistant
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/saml-dev/gome-assistant/internal/setup"
|
"github.com/saml-dev/gome-assistant/internal/setup"
|
||||||
@@ -17,6 +16,11 @@ type app struct {
|
|||||||
entityListeners []entityListener
|
entityListeners []entityListener
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
Sunrise hourMinute = hourMinute{1000, 0}
|
||||||
|
Sunset hourMinute = hourMinute{1001, 0}
|
||||||
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
App establishes the websocket connection and returns an object
|
App establishes the websocket connection and returns an object
|
||||||
you can use to register schedules and listeners.
|
you can use to register schedules and listeners.
|
||||||
@@ -43,7 +47,6 @@ func (a *app) Cleanup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) RegisterSchedule(s schedule) {
|
func (a *app) RegisterSchedule(s schedule) {
|
||||||
fmt.Println(a.schedules)
|
|
||||||
if s.err != nil {
|
if s.err != nil {
|
||||||
panic(s.err) // something wasn't configured properly when the schedule was built
|
panic(s.err) // something wasn't configured properly when the schedule was built
|
||||||
}
|
}
|
||||||
@@ -56,9 +59,16 @@ func (a *app) RegisterSchedule(s schedule) {
|
|||||||
startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) // start at midnight today
|
startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) // start at midnight today
|
||||||
|
|
||||||
// apply offset if set
|
// apply offset if set
|
||||||
if s.offset.Hour != 0 || s.offset.Minute != 0 {
|
if s.offset.int() != 0 {
|
||||||
startTime.Add(time.Hour * time.Duration(s.offset.Hour))
|
if s.offset.int() == Sunrise.int() {
|
||||||
startTime.Add(time.Minute * time.Duration(s.offset.Minute))
|
// TODO: same as sunset w/ sunrise
|
||||||
|
} else if s.offset.int() == Sunset.int() {
|
||||||
|
// TODO: add an http client (w/ token) to *app, use it to get state of sun.sun
|
||||||
|
// to get next sunset time
|
||||||
|
} else {
|
||||||
|
startTime.Add(time.Hour * time.Duration(s.offset.Hour))
|
||||||
|
startTime.Add(time.Minute * time.Duration(s.offset.Minute))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// advance first scheduled time by frequency until it is in the future
|
// advance first scheduled time by frequency until it is in the future
|
||||||
@@ -70,6 +80,10 @@ func (a *app) RegisterSchedule(s schedule) {
|
|||||||
a.schedules = append(a.schedules, s)
|
a.schedules = append(a.schedules, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *app) Start() {
|
||||||
|
// TODO: figure out looping listening to messages
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FrequencyMissing time.Duration = 0
|
FrequencyMissing time.Duration = 0
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,24 @@ type entityListenerCallback func(Service, Data)
|
|||||||
|
|
||||||
type Data struct{}
|
type Data struct{}
|
||||||
|
|
||||||
|
func (b elBuilder3) OnlyBetween(start hourMinute, end hourMinute) elBuilder3 {
|
||||||
|
b.entityListener.betweenStart = start
|
||||||
|
b.entityListener.betweenEnd = end
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b elBuilder3) FromState(s string) elBuilder3 {
|
||||||
|
b.entityListener.fromState = s
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b elBuilder3) ToState(s string) elBuilder3 {
|
||||||
|
b.entityListener.toState = s
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Builders */
|
||||||
|
|
||||||
func EntityListenerBuilder() elBuilder1 {
|
func EntityListenerBuilder() elBuilder1 {
|
||||||
return elBuilder1{entityListener{}}
|
return elBuilder1{entityListener{}}
|
||||||
}
|
}
|
||||||
@@ -38,19 +56,3 @@ func (b elBuilder2) Call(callback entityListenerCallback) elBuilder3 {
|
|||||||
type elBuilder3 struct {
|
type elBuilder3 struct {
|
||||||
entityListener
|
entityListener
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b elBuilder3) OnlyBetween(start hourMinute, end hourMinute) elBuilder3 {
|
|
||||||
b.entityListener.betweenStart = start
|
|
||||||
b.entityListener.betweenEnd = end
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b elBuilder3) FromState(s string) elBuilder3 {
|
|
||||||
b.entityListener.fromState = s
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b elBuilder3) ToState(s string) elBuilder3 {
|
|
||||||
b.entityListener.toState = s
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type AuthMessage struct {
|
|||||||
|
|
||||||
func WriteMessage[T any](msg T, conn *websocket.Conn, ctx context.Context) error {
|
func WriteMessage[T any](msg T, conn *websocket.Conn, ctx context.Context) error {
|
||||||
msgJson, err := json.Marshal(msg)
|
msgJson, err := json.Marshal(msg)
|
||||||
fmt.Println(string(msgJson))
|
// fmt.Println(string(msgJson))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,6 @@ func VerifyAuthResponse(conn *websocket.Conn, ctx context.Context) error {
|
|||||||
|
|
||||||
var authResp authResponse
|
var authResp authResponse
|
||||||
json.Unmarshal(msg, &authResp)
|
json.Unmarshal(msg, &authResp)
|
||||||
fmt.Println(authResp)
|
|
||||||
if authResp.MsgType != "auth_ok" {
|
if authResp.MsgType != "auth_ok" {
|
||||||
return errors.New("invalid auth token")
|
return errors.New("invalid auth token")
|
||||||
}
|
}
|
||||||
|
|||||||
12
schedule.go
12
schedule.go
@@ -12,6 +12,10 @@ type hourMinute struct {
|
|||||||
Minute int
|
Minute int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (hm hourMinute) int() int {
|
||||||
|
return hm.Hour + hm.Minute
|
||||||
|
}
|
||||||
|
|
||||||
func HourMinute(Hour, Minute int) hourMinute {
|
func HourMinute(Hour, Minute int) hourMinute {
|
||||||
return hourMinute{Hour, Minute}
|
return hourMinute{Hour, Minute}
|
||||||
}
|
}
|
||||||
@@ -76,7 +80,12 @@ type scheduleBuilderEnd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ScheduleBuilder() scheduleBuilder {
|
func ScheduleBuilder() scheduleBuilder {
|
||||||
return scheduleBuilder{schedule{}}
|
return scheduleBuilder{
|
||||||
|
schedule{
|
||||||
|
frequency: 0,
|
||||||
|
offset: hourMinute{0, 0},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s schedule) String() string {
|
func (s schedule) String() string {
|
||||||
@@ -88,7 +97,6 @@ func (s schedule) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func frequencyToString(d time.Duration) string {
|
func frequencyToString(d time.Duration) string {
|
||||||
fmt.Println(d.Hours(), d.Minutes(), d.Seconds())
|
|
||||||
if d.Hours() == 24 {
|
if d.Hours() == 24 {
|
||||||
return "daily at"
|
return "daily at"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user