mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 17:15:14 -06:00
enforce ToState when using Duration
This commit is contained in:
8
app.go
8
app.go
@@ -75,7 +75,7 @@ func (a *app) RegisterSchedule(s Schedule) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.frequency == 0 {
|
if s.frequency == 0 {
|
||||||
log.Fatalln("A schedule must call either Daily() or Every() when built.")
|
panic("A schedule must use either Daily() or Every() when built.")
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -95,6 +95,10 @@ func (a *app) RegisterSchedule(s Schedule) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) RegisterEntityListener(etl EntityListener) {
|
func (a *app) RegisterEntityListener(etl EntityListener) {
|
||||||
|
if etl.delay != 0 && etl.toState == "" {
|
||||||
|
panic("EntityListener error: you have to use ToState() when using Duration()")
|
||||||
|
}
|
||||||
|
|
||||||
for _, entity := range etl.entityIds {
|
for _, entity := range etl.entityIds {
|
||||||
if elList, ok := a.entityListeners[entity]; ok {
|
if elList, ok := a.entityListeners[entity]; ok {
|
||||||
a.entityListeners[entity] = append(elList, &etl)
|
a.entityListeners[entity] = append(elList, &etl)
|
||||||
@@ -135,7 +139,7 @@ func getSunriseSunset(a *app, sunrise bool, offset []DurationString) carbon.Carb
|
|||||||
// get next sunrise/sunset time from HA
|
// get next sunrise/sunset time from HA
|
||||||
state, err := a.state.Get("sun.sun")
|
state, err := a.state.Get("sun.sun")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Couldn't get sun.sun state from HA to calculate", printString)
|
panic("Couldn't get sun.sun state from HA to calculate", printString)
|
||||||
}
|
}
|
||||||
|
|
||||||
nextSetOrRise := carbon.Parse(state.Attributes[attrKey].(string))
|
nextSetOrRise := carbon.Parse(state.Attributes[attrKey].(string))
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ type elBuilder1 struct {
|
|||||||
|
|
||||||
func (b elBuilder1) EntityIds(entityIds ...string) elBuilder2 {
|
func (b elBuilder1) EntityIds(entityIds ...string) elBuilder2 {
|
||||||
if len(entityIds) == 0 {
|
if len(entityIds) == 0 {
|
||||||
log.Fatalln("must pass at least one entityId to EntityIds()")
|
panic("must pass at least one entityId to EntityIds()")
|
||||||
} else {
|
} else {
|
||||||
b.entityListener.entityIds = entityIds
|
b.entityListener.entityIds = entityIds
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,12 +71,12 @@ func get(url, token string) ([]byte, error) {
|
|||||||
// defer resp.Body.Close()
|
// defer resp.Body.Close()
|
||||||
|
|
||||||
// if resp.StatusCode == 401 {
|
// if resp.StatusCode == 401 {
|
||||||
// log.Fatalln("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
|
// panic("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// body, err := io.ReadAll(resp.Body)
|
// body, err := io.ReadAll(resp.Body)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// log.Fatalln(err)
|
// panic(err)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// return body, nil
|
// return body, nil
|
||||||
|
|||||||
@@ -59,21 +59,21 @@ func SetupConnection(connString string, authToken string) (*websocket.Conn, cont
|
|||||||
_, err = ReadMessage(conn, ctx)
|
_, err = ReadMessage(conn, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
log.Fatalln("Unknown error creating websocket client")
|
panic("Unknown error creating websocket client")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send auth message
|
// Send auth message
|
||||||
err = SendAuthMessage(conn, ctx, authToken)
|
err = SendAuthMessage(conn, ctx, authToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
log.Fatalln("Unknown error creating websocket client")
|
panic("Unknown error creating websocket client")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify auth message was successful
|
// Verify auth message was successful
|
||||||
err = VerifyAuthResponse(conn, ctx)
|
err = VerifyAuthResponse(conn, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
log.Fatalln("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
|
panic("ERROR: Auth token is invalid. Please double check it or create a new token in your Home Assistant profile")
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn, ctx, ctxCancel
|
return conn, ctx, ctxCancel
|
||||||
@@ -132,7 +132,7 @@ func SubscribeToEventType(eventType string, conn *websocket.Conn, ctx context.Co
|
|||||||
}
|
}
|
||||||
err := WriteMessage(e, conn, ctx)
|
err := WriteMessage(e, conn, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Error writing to websocket: ", err)
|
panic("Error writing to websocket: ", err)
|
||||||
}
|
}
|
||||||
// m, _ := ReadMessage(conn, ctx)
|
// m, _ := ReadMessage(conn, ctx)
|
||||||
// log.Default().Println(string(m))
|
// log.Default().Println(string(m))
|
||||||
|
|||||||
Reference in New Issue
Block a user