mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 03:15:14 -06:00
Additionally, removed the context that gets passed into the Services but isn't used in one of them. The websockets APIs also don't have any use for context.
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package services
|
|
|
|
import (
|
|
ws "saml.dev/gome-assistant/internal/websocket"
|
|
)
|
|
|
|
type HomeAssistant struct {
|
|
conn *ws.WebsocketWriter
|
|
}
|
|
|
|
// 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) error {
|
|
req := NewBaseServiceRequest(entityId)
|
|
req.Domain = "homeassistant"
|
|
req.Service = "turn_on"
|
|
if len(serviceData) != 0 {
|
|
req.ServiceData = serviceData[0]
|
|
}
|
|
|
|
return ha.conn.WriteMessage(req)
|
|
}
|
|
|
|
// 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) error {
|
|
req := NewBaseServiceRequest(entityId)
|
|
req.Domain = "homeassistant"
|
|
req.Service = "toggle"
|
|
if len(serviceData) != 0 {
|
|
req.ServiceData = serviceData[0]
|
|
}
|
|
|
|
return ha.conn.WriteMessage(req)
|
|
}
|
|
|
|
func (ha *HomeAssistant) TurnOff(entityId string) error {
|
|
req := NewBaseServiceRequest(entityId)
|
|
req.Domain = "homeassistant"
|
|
req.Service = "turn_off"
|
|
|
|
return ha.conn.WriteMessage(req)
|
|
}
|