mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 05:15:15 -06:00
Added lots of services and fixed bug with ID for websocket calls
This commit is contained in:
@@ -3,21 +3,37 @@ package services
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal/http"
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type HomeAssistant struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
httpClient *http.HttpClient
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) TurnOn(entityId string) {
|
||||
// TurnOn a Home Assistant entity. Takes an entityId and an optional
|
||||
// map that is translated into service_data.
|
||||
func (ha *HomeAssistant) TurnOn(entityId string, serviceData ...map[string]any) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "homeassistant"
|
||||
req.Service = "turn_on"
|
||||
if len(serviceData) != 0 {
|
||||
req.ServiceData = serviceData[0]
|
||||
}
|
||||
|
||||
ws.WriteMessage(req, ha.conn, ha.ctx)
|
||||
}
|
||||
|
||||
// Toggle a Home Assistant entity. Takes an entityId and an optional
|
||||
// map that is translated into service_data.
|
||||
func (ha *HomeAssistant) Toggle(entityId string, serviceData ...map[string]any) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "homeassistant"
|
||||
req.Service = "toggle"
|
||||
if len(serviceData) != 0 {
|
||||
req.ServiceData = serviceData[0]
|
||||
}
|
||||
|
||||
ws.WriteMessage(req, ha.conn, ha.ctx)
|
||||
}
|
||||
@@ -29,11 +45,3 @@ func (ha *HomeAssistant) TurnOff(entityId string) {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
47
internal/services/input_boolean.go
Normal file
47
internal/services/input_boolean.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
/* Structs */
|
||||
|
||||
type InputBoolean struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
func (ib InputBoolean) TurnOn(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_boolean"
|
||||
req.Service = "turn_on"
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputBoolean) Toggle(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_boolean"
|
||||
req.Service = "toggle"
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputBoolean) TurnOff(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_boolean"
|
||||
req.Service = "turn_off"
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputBoolean) Reload() {
|
||||
req := NewBaseServiceRequest("")
|
||||
req.Domain = "input_boolean"
|
||||
req.Service = "reload"
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
32
internal/services/input_button.go
Normal file
32
internal/services/input_button.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
/* Structs */
|
||||
|
||||
type InputButton struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
func (ib InputButton) Press(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_button"
|
||||
req.Service = "press"
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputButton) Reload() {
|
||||
req := NewBaseServiceRequest("")
|
||||
req.Domain = "input_button"
|
||||
req.Service = "reload"
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
38
internal/services/input_datetime.go
Normal file
38
internal/services/input_datetime.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
/* Structs */
|
||||
|
||||
type InputDatetime struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
func (ib InputDatetime) Set(entityId string, value time.Time) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_datetime"
|
||||
req.Service = "set_datetime"
|
||||
req.ServiceData = map[string]any{
|
||||
"timestamp": fmt.Sprint(value.Unix()),
|
||||
}
|
||||
fmt.Println(req)
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx) // TODO: this ain't working for some reason
|
||||
}
|
||||
|
||||
func (ib InputDatetime) Reload() {
|
||||
req := NewBaseServiceRequest("")
|
||||
req.Domain = "input_datetime"
|
||||
req.Service = "reload"
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
49
internal/services/input_number.go
Normal file
49
internal/services/input_number.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
/* Structs */
|
||||
|
||||
type InputNumber struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
func (ib InputNumber) Set(entityId string, value float32) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_number"
|
||||
req.Service = "set_value"
|
||||
req.ServiceData = map[string]any{"value": value}
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputNumber) Increment(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_number"
|
||||
req.Service = "increment"
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputNumber) Decrement(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_number"
|
||||
req.Service = "decrement"
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputNumber) Reload() {
|
||||
req := NewBaseServiceRequest("")
|
||||
req.Domain = "input_number"
|
||||
req.Service = "reload"
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
35
internal/services/input_text.go
Normal file
35
internal/services/input_text.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
/* Structs */
|
||||
|
||||
type InputText struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
func (ib InputText) Set(entityId string, value string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "input_text"
|
||||
req.Service = "set_value"
|
||||
req.ServiceData = map[string]any{
|
||||
"value": value,
|
||||
}
|
||||
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
|
||||
func (ib InputText) Reload() {
|
||||
req := NewBaseServiceRequest("")
|
||||
req.Domain = "input_text"
|
||||
req.Service = "reload"
|
||||
ws.WriteMessage(req, ib.conn, ib.ctx)
|
||||
}
|
||||
@@ -3,54 +3,48 @@ package services
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal/http"
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type Light struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
httpClient *http.HttpClient
|
||||
}
|
||||
/* Structs */
|
||||
|
||||
type LightRequest struct {
|
||||
Id int `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Domain string `json:"domain"`
|
||||
Service string `json:"service"`
|
||||
Target struct {
|
||||
EntityId string `json:"entity_id"`
|
||||
} `json:"target"`
|
||||
type Light struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
func (l Light) TurnOn(entityId string) {
|
||||
req := newLightOnRequest(entityId)
|
||||
// TurnOn a light entity. Takes an entityId and an optional
|
||||
// map that is translated into service_data.
|
||||
func (l Light) TurnOn(entityId string, serviceData ...map[string]any) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "light"
|
||||
req.Service = "turn_on"
|
||||
if len(serviceData) != 0 {
|
||||
req.ServiceData = serviceData[0]
|
||||
}
|
||||
|
||||
ws.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
// Toggle a light entity. Takes an entityId and an optional
|
||||
// map that is translated into service_data.
|
||||
func (l Light) Toggle(entityId string, serviceData ...map[string]any) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "light"
|
||||
req.Service = "toggle"
|
||||
if len(serviceData) != 0 {
|
||||
req.ServiceData = serviceData[0]
|
||||
}
|
||||
|
||||
ws.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
func (l Light) TurnOff(entityId string) {
|
||||
req := newLightOffRequest(entityId)
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "light"
|
||||
req.Service = "turn_off"
|
||||
ws.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
/* Internal */
|
||||
|
||||
func newLightOnRequest(entityId string) LightRequest {
|
||||
req := LightRequest{
|
||||
Id: 5,
|
||||
Type: "call_service",
|
||||
Domain: "light",
|
||||
Service: "turn_on",
|
||||
}
|
||||
req.Target.EntityId = entityId
|
||||
return req
|
||||
}
|
||||
|
||||
func newLightOffRequest(entityId string) LightRequest {
|
||||
req := newLightOnRequest(entityId)
|
||||
req.Service = "turn_off"
|
||||
return req
|
||||
}
|
||||
|
||||
43
internal/services/lock.go
Normal file
43
internal/services/lock.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
/* Structs */
|
||||
|
||||
type Lock struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
// Lock a lock entity. Takes an entityId and an optional
|
||||
// map that is translated into service_data.
|
||||
func (l Lock) Lock(entityId string, serviceData ...map[string]any) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "lock"
|
||||
req.Service = "lock"
|
||||
if len(serviceData) != 0 {
|
||||
req.ServiceData = serviceData[0]
|
||||
}
|
||||
|
||||
ws.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
|
||||
// Unlock a lock entity. Takes an entityId and an optional
|
||||
// map that is translated into service_data.
|
||||
func (l Lock) Unlock(entityId string, serviceData ...map[string]any) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "lock"
|
||||
req.Service = "unlock"
|
||||
if len(serviceData) != 0 {
|
||||
req.ServiceData = serviceData[0]
|
||||
}
|
||||
|
||||
ws.WriteMessage(req, l.conn, l.ctx)
|
||||
}
|
||||
@@ -2,31 +2,46 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal/http"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
func BuildService[T Light | HomeAssistant](conn *websocket.Conn, ctx context.Context, httpClient *http.HttpClient) *T {
|
||||
return &T{conn: conn, ctx: ctx, httpClient: httpClient}
|
||||
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 int `json:"id"`
|
||||
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"`
|
||||
} `json:"target,omitempty"`
|
||||
}
|
||||
|
||||
var id int64 = 1
|
||||
|
||||
func NewBaseServiceRequest(entityId string) BaseServiceRequest {
|
||||
bsr := BaseServiceRequest{
|
||||
Id: 10,
|
||||
Id: fmt.Sprint(id),
|
||||
RequestType: "call_service",
|
||||
}
|
||||
bsr.Target.EntityId = entityId
|
||||
if entityId != "" {
|
||||
bsr.Target.EntityId = entityId
|
||||
}
|
||||
id += 1
|
||||
return bsr
|
||||
}
|
||||
40
internal/services/switch.go
Normal file
40
internal/services/switch.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
/* Structs */
|
||||
|
||||
type Switch struct {
|
||||
conn *websocket.Conn
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
/* Public API */
|
||||
|
||||
func (s Switch) TurnOn(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "switch"
|
||||
req.Service = "turn_on"
|
||||
|
||||
ws.WriteMessage(req, s.conn, s.ctx)
|
||||
}
|
||||
|
||||
func (s Switch) Toggle(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "switch"
|
||||
req.Service = "toggle"
|
||||
|
||||
ws.WriteMessage(req, s.conn, s.ctx)
|
||||
}
|
||||
|
||||
func (s Switch) TurnOff(entityId string) {
|
||||
req := NewBaseServiceRequest(entityId)
|
||||
req.Domain = "switch"
|
||||
req.Service = "turn_off"
|
||||
ws.WriteMessage(req, s.conn, s.ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user