mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-10 10:07:27 -06:00
Added lots of services and fixed bug with ID for websocket calls
This commit is contained in:
47
internal/services/services.go
Normal file
47
internal/services/services.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
func BuildService[
|
||||
T Light |
|
||||
HomeAssistant |
|
||||
Lock |
|
||||
Switch |
|
||||
InputBoolean |
|
||||
InputButton |
|
||||
InputDatetime |
|
||||
InputText |
|
||||
InputNumber,
|
||||
](conn *websocket.Conn, ctx context.Context) *T {
|
||||
return &T{conn: conn, ctx: ctx}
|
||||
}
|
||||
|
||||
type BaseServiceRequest struct {
|
||||
Id string `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,omitempty"`
|
||||
}
|
||||
|
||||
var id int64 = 1
|
||||
|
||||
func NewBaseServiceRequest(entityId string) BaseServiceRequest {
|
||||
bsr := BaseServiceRequest{
|
||||
Id: fmt.Sprint(id),
|
||||
RequestType: "call_service",
|
||||
}
|
||||
if entityId != "" {
|
||||
bsr.Target.EntityId = entityId
|
||||
}
|
||||
id += 1
|
||||
return bsr
|
||||
}
|
||||
Reference in New Issue
Block a user