add notify service

This commit is contained in:
Sam Lewis
2022-11-06 15:00:39 -05:00
parent e3e7889574
commit ba9132745e
5 changed files with 49 additions and 3 deletions

View File

@@ -0,0 +1,31 @@
package services
import (
"context"
"github.com/gorilla/websocket"
ws "github.com/saml-dev/gome-assistant/internal/websocket"
"github.com/saml-dev/gome-assistant/types"
)
type Notify struct {
conn *websocket.Conn
ctx context.Context
}
// Send a notification. Takes a types.NotifyRequest.
func (ha *Notify) Notify(reqData types.NotifyRequest) {
req := NewBaseServiceRequest("")
req.Domain = "notify"
req.Service = reqData.ServiceName
serviceData := map[string]any{}
serviceData["message"] = reqData.Message
serviceData["title"] = reqData.Title
if reqData.Data != nil {
serviceData["data"] = reqData.Data
}
req.ServiceData = serviceData
ws.WriteMessage(req, ha.conn, ha.ctx)
}

View File

@@ -17,7 +17,8 @@ func BuildService[
InputButton |
InputDatetime |
InputText |
InputNumber,
InputNumber |
Notify,
](conn *websocket.Conn, ctx context.Context) *T {
return &T{conn: conn, ctx: ctx}
}
@@ -29,7 +30,7 @@ type BaseServiceRequest struct {
Service string `json:"service"`
ServiceData map[string]any `json:"service_data,omitempty"`
Target struct {
EntityId string `json:"entity_id"`
EntityId string `json:"entity_id,omitempty"`
} `json:"target,omitempty"`
}
@@ -42,6 +43,5 @@ func NewBaseServiceRequest(entityId string) BaseServiceRequest {
if entityId != "" {
bsr.Target.EntityId = entityId
}
id += 1
return bsr
}