mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-10 08:07:23 -06:00
design and impl BaseServiceRequest
This commit is contained in:
@@ -24,8 +24,8 @@ func main() {
|
|||||||
log.Println(s2)
|
log.Println(s2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func lightsOut(service ga.Service, state ga.State) {
|
func lightsOut(service *ga.Service, state *ga.State) {
|
||||||
// ga.TurnOff("light.all_lights")
|
service.HomeAssistant.Toggle("light.office_ceiling_lights")
|
||||||
}
|
}
|
||||||
|
|
||||||
func cool(service ga.Service, data ga.Data) {
|
func cool(service ga.Service, data ga.Data) {
|
||||||
|
|||||||
@@ -3,9 +3,30 @@ package services
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/saml-dev/gome-assistant/internal/http"
|
||||||
"nhooyr.io/websocket"
|
"nhooyr.io/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BuildService[T Light | HomeAssistant](conn *websocket.Conn, ctx context.Context) *T {
|
func BuildService[T Light | HomeAssistant](conn *websocket.Conn, ctx context.Context, httpClient *http.HttpClient) *T {
|
||||||
return &T{conn: conn, ctx: ctx}
|
return &T{conn: conn, ctx: ctx, httpClient: httpClient}
|
||||||
|
}
|
||||||
|
|
||||||
|
type BaseServiceRequest struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
RequestType string `json:"type"` // hardcoded "call_service"
|
||||||
|
Domain string `json:"domain"`
|
||||||
|
Service string `json:"service"`
|
||||||
|
ServiceData map[string]any `json:"service_data,omitempty"`
|
||||||
|
Target struct {
|
||||||
|
EntityId string `json:"entity_id"`
|
||||||
|
} `json:"target"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBaseServiceRequest(entityId string) BaseServiceRequest {
|
||||||
|
bsr := BaseServiceRequest{
|
||||||
|
Id: 10,
|
||||||
|
RequestType: "call_service",
|
||||||
|
}
|
||||||
|
bsr.Target.EntityId = entityId
|
||||||
|
return bsr
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/saml-dev/gome-assistant/internal/http"
|
"github.com/saml-dev/gome-assistant/internal/http"
|
||||||
|
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||||
"nhooyr.io/websocket"
|
"nhooyr.io/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,6 +14,26 @@ type HomeAssistant struct {
|
|||||||
httpClient *http.HttpClient
|
httpClient *http.HttpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: design how much reuse I can get between request types. E.g.
|
func (ha *HomeAssistant) TurnOn(entityId string) {
|
||||||
// only difference between light.turnon and homeassistant.turnon is
|
req := NewBaseServiceRequest(entityId)
|
||||||
// domain and extra data
|
req.Domain = "homeassistant"
|
||||||
|
req.Service = "turn_on"
|
||||||
|
|
||||||
|
ws.WriteMessage(req, ha.conn, ha.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ha *HomeAssistant) TurnOff(entityId string) {
|
||||||
|
req := NewBaseServiceRequest(entityId)
|
||||||
|
req.Domain = "homeassistant"
|
||||||
|
req.Service = "turn_off"
|
||||||
|
|
||||||
|
ws.WriteMessage(req, ha.conn, ha.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ha *HomeAssistant) Toggle(entityId string) {
|
||||||
|
req := NewBaseServiceRequest(entityId)
|
||||||
|
req.Domain = "homeassistant"
|
||||||
|
req.Service = "toggle"
|
||||||
|
|
||||||
|
ws.WriteMessage(req, ha.conn, ha.ctx)
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,7 +23,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
|
||||||
}
|
}
|
||||||
@@ -100,6 +100,7 @@ func VerifyAuthResponse(conn *websocket.Conn, ctx context.Context) error {
|
|||||||
|
|
||||||
var authResp authResponse
|
var authResp authResponse
|
||||||
json.Unmarshal(msg, &authResp)
|
json.Unmarshal(msg, &authResp)
|
||||||
|
log.Println(authResp.MsgType)
|
||||||
if authResp.MsgType != "auth_ok" {
|
if authResp.MsgType != "auth_ok" {
|
||||||
return errors.New("invalid auth token")
|
return errors.New("invalid auth token")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func Duration(hour, minute int) time.Duration {
|
|||||||
return TimeOfDay(hour, minute)
|
return TimeOfDay(hour, minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
type scheduleCallback func(Service, State)
|
type scheduleCallback func(*Service, *State)
|
||||||
|
|
||||||
type schedule struct {
|
type schedule struct {
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type Service struct {
|
|||||||
|
|
||||||
func NewService(conn *websocket.Conn, ctx context.Context, httpClient *http.HttpClient) *Service {
|
func NewService(conn *websocket.Conn, ctx context.Context, httpClient *http.HttpClient) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
Light: services.BuildService[services.Light](conn, ctx),
|
Light: services.BuildService[services.Light](conn, ctx, httpClient),
|
||||||
HomeAssistant: services.BuildService[services.HomeAssistant](conn, ctx),
|
HomeAssistant: services.BuildService[services.HomeAssistant](conn, ctx, httpClient),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user