Return Service errors

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.
This commit is contained in:
Matthias Loibl
2025-01-17 17:50:06 +01:00
parent 066441762b
commit 02b6c413f1
26 changed files with 228 additions and 296 deletions

View File

@@ -1,8 +1,6 @@
package services
import (
"context"
ws "saml.dev/gome-assistant/internal/websocket"
)
@@ -10,50 +8,49 @@ import (
type Cover struct {
conn *ws.WebsocketWriter
ctx context.Context
}
/* Public API */
// Close all or specified cover. Takes an entityId.
func (c Cover) Close(entityId string) {
func (c Cover) Close(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "close_cover"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Close all or specified cover tilt. Takes an entityId.
func (c Cover) CloseTilt(entityId string) {
func (c Cover) CloseTilt(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "close_cover_tilt"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Open all or specified cover. Takes an entityId.
func (c Cover) Open(entityId string) {
func (c Cover) Open(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "open_cover"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Open all or specified cover tilt. Takes an entityId.
func (c Cover) OpenTilt(entityId string) {
func (c Cover) OpenTilt(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "open_cover_tilt"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Move to specific position all or specified cover. Takes an entityId and an optional
// map that is translated into service_data.
func (c Cover) SetPosition(entityId string, serviceData ...map[string]any) {
func (c Cover) SetPosition(entityId string, serviceData ...map[string]any) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "set_cover_position"
@@ -61,12 +58,12 @@ func (c Cover) SetPosition(entityId string, serviceData ...map[string]any) {
req.ServiceData = serviceData[0]
}
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Move to specific position all or specified cover tilt. Takes an entityId and an optional
// map that is translated into service_data.
func (c Cover) SetTiltPosition(entityId string, serviceData ...map[string]any) {
func (c Cover) SetTiltPosition(entityId string, serviceData ...map[string]any) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "set_cover_tilt_position"
@@ -74,41 +71,41 @@ func (c Cover) SetTiltPosition(entityId string, serviceData ...map[string]any) {
req.ServiceData = serviceData[0]
}
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Stop a cover entity. Takes an entityId.
func (c Cover) Stop(entityId string) {
func (c Cover) Stop(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "stop_cover"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Stop a cover entity tilt. Takes an entityId.
func (c Cover) StopTilt(entityId string) {
func (c Cover) StopTilt(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "stop_cover_tilt"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Toggle a cover open/closed. Takes an entityId.
func (c Cover) Toggle(entityId string) {
func (c Cover) Toggle(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "toggle"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}
// Toggle a cover tilt open/closed. Takes an entityId.
func (c Cover) ToggleTilt(entityId string) {
func (c Cover) ToggleTilt(entityId string) error {
req := NewBaseServiceRequest(entityId)
req.Domain = "cover"
req.Service = "toggle_cover_tilt"
c.conn.WriteMessage(req, c.ctx)
return c.conn.WriteMessage(req)
}