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

4
app.go
View File

@@ -3,6 +3,7 @@ package gomeassistant
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"os" "os"
"time" "time"
@@ -164,6 +165,9 @@ func getSunriseSunset(a *App, sunrise bool, offset []DurationString) carbon.Carb
} }
func (a *App) Start() { func (a *App) Start() {
log.Default().Println("Starting", a.schedules.Len(), "schedules")
log.Default().Println("Starting", len(a.entityListeners), "entity listeners")
log.Default().Println("Starting", len(a.eventListeners), "event listeners")
// schedules // schedules
go runSchedules(a) go runSchedules(a)

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

View File

@@ -18,6 +18,7 @@ type Service struct {
InputText *services.InputText InputText *services.InputText
InputDatetime *services.InputDatetime InputDatetime *services.InputDatetime
InputNumber *services.InputNumber InputNumber *services.InputNumber
Notify *services.Notify
} }
func newService(conn *websocket.Conn, ctx context.Context, httpClient *http.HttpClient) *Service { func newService(conn *websocket.Conn, ctx context.Context, httpClient *http.HttpClient) *Service {
@@ -31,5 +32,6 @@ func newService(conn *websocket.Conn, ctx context.Context, httpClient *http.Http
InputText: services.BuildService[services.InputText](conn, ctx), InputText: services.BuildService[services.InputText](conn, ctx),
InputDatetime: services.BuildService[services.InputDatetime](conn, ctx), InputDatetime: services.BuildService[services.InputDatetime](conn, ctx),
InputNumber: services.BuildService[services.InputNumber](conn, ctx), InputNumber: services.BuildService[services.InputNumber](conn, ctx),
Notify: services.BuildService[services.Notify](conn, ctx),
} }
} }

9
types/requestTypes.go Normal file
View File

@@ -0,0 +1,9 @@
package types
type NotifyRequest struct {
// Which notify service to call, such as mobile_app_sams_iphone
ServiceName string
Message string
Title string
Data map[string]any
}