mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 03:15:14 -06:00
good progress yay:
- impl http client - create http client in App() - generic builder for Service.* - set Service on app to pass to callbacks later - impl State - set State on app to pass to callbacks later - change panic to log.Fatalln
This commit is contained in:
11
internal/services/builder.go
Normal file
11
internal/services/builder.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
func BuildService[T Light | HomeAssistant](conn *websocket.Conn, ctx context.Context) *T {
|
||||
return &T{conn: conn, ctx: ctx}
|
||||
}
|
||||
18
internal/services/homeassistant.go
Normal file
18
internal/services/homeassistant.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal/http"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type HomeAssistant struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
httpClient *http.HttpClient
|
||||
}
|
||||
|
||||
// TODO: design how much reuse I can get between request types. E.g.
|
||||
// only difference between light.turnon and homeassistant.turnon is
|
||||
// domain and extra data
|
||||
@@ -3,13 +3,15 @@ package services
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal/http"
|
||||
"github.com/saml-dev/gome-assistant/internal/setup"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type Light struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
httpClient *http.HttpClient
|
||||
}
|
||||
|
||||
type LightRequest struct {
|
||||
@@ -22,7 +24,21 @@ type LightRequest struct {
|
||||
} `json:"target"`
|
||||
}
|
||||
|
||||
func LightOnRequest(entityId string) LightRequest {
|
||||
/* Public API */
|
||||
|
||||
func (l Light) TurnOn(entityId string) {
|
||||
req := newLightOnRequest(entityId)
|
||||
setup.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
func (l Light) TurnOff(entityId string) {
|
||||
req := newLightOffRequest(entityId)
|
||||
setup.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
/* Internal */
|
||||
|
||||
func newLightOnRequest(entityId string) LightRequest {
|
||||
req := LightRequest{
|
||||
Id: 5,
|
||||
Type: "call_service",
|
||||
@@ -33,18 +49,8 @@ func LightOnRequest(entityId string) LightRequest {
|
||||
return req
|
||||
}
|
||||
|
||||
func LightOffRequest(entityId string) LightRequest {
|
||||
req := LightOnRequest(entityId)
|
||||
func newLightOffRequest(entityId string) LightRequest {
|
||||
req := newLightOnRequest(entityId)
|
||||
req.Service = "turn_off"
|
||||
return req
|
||||
}
|
||||
|
||||
func (l Light) TurnOn(entityId string) {
|
||||
req := LightOnRequest(entityId)
|
||||
setup.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
func (l Light) TurnOff(entityId string) {
|
||||
req := LightOffRequest(entityId)
|
||||
setup.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user