mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-08 12:07:17 -06:00
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gorilla/websocket"
|
|
ws "saml.dev/gome-assistant/internal/websocket"
|
|
)
|
|
|
|
/* Structs */
|
|
|
|
type Light struct {
|
|
conn *websocket.Conn
|
|
ctx context.Context
|
|
}
|
|
|
|
/* Public API */
|
|
|
|
// 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 := NewBaseServiceRequest(entityId)
|
|
req.Domain = "light"
|
|
req.Service = "turn_off"
|
|
ws.WriteMessage(req, l.conn, l.ctx)
|
|
}
|