mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-07 03: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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user