mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-07 16:07:19 -06:00
58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gorilla/websocket"
|
|
ws "github.com/saml-dev/gome-assistant/internal/websocket"
|
|
)
|
|
|
|
type HomeAssistant struct {
|
|
conn *websocket.Conn
|
|
ctx context.Context
|
|
}
|
|
|
|
// 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)
|
|
// msg, _ := ws.ReadMessage(ha.conn, ha.ctx)
|
|
// log.Default().Println(string(msg))
|
|
// msg, _ = ws.ReadMessage(ha.conn, ha.ctx)
|
|
// log.Default().Println(string(msg))
|
|
// msg, _ = ws.ReadMessage(ha.conn, ha.ctx)
|
|
// log.Default().Println(string(msg))
|
|
// msg, _ = ws.ReadMessage(ha.conn, ha.ctx)
|
|
// log.Default().Println(string(msg))
|
|
// msg, _ = ws.ReadMessage(ha.conn, ha.ctx)
|
|
// log.Default().Println(string(msg))
|
|
}
|
|
|
|
func (ha *HomeAssistant) TurnOff(entityId string) {
|
|
req := NewBaseServiceRequest(entityId)
|
|
req.Domain = "homeassistant"
|
|
req.Service = "turn_off"
|
|
|
|
ws.WriteMessage(req, ha.conn, ha.ctx)
|
|
}
|