Files
go-ha/internal/services/light.go
2023-02-24 11:49:26 +01:00

50 lines
1.1 KiB
Go

package services
import (
"context"
ws "saml.dev/gome-assistant/internal/websocket"
)
/* Structs */
type Light struct {
conn *ws.WebsocketWriter
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]
}
l.conn.WriteMessage(req, 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]
}
l.conn.WriteMessage(req, l.ctx)
}
func (l Light) TurnOff(entityId string) {
req := NewBaseServiceRequest(entityId)
req.Domain = "light"
req.Service = "turn_off"
l.conn.WriteMessage(req, l.ctx)
}