mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 11:15:13 -06:00
add notify service
This commit is contained in:
4
app.go
4
app.go
@@ -3,6 +3,7 @@ package gomeassistant
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -164,6 +165,9 @@ func getSunriseSunset(a *App, sunrise bool, offset []DurationString) carbon.Carb
|
||||
}
|
||||
|
||||
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
|
||||
go runSchedules(a)
|
||||
|
||||
|
||||
31
internal/services/notify.go
Normal file
31
internal/services/notify.go
Normal 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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type Service struct {
|
||||
InputText *services.InputText
|
||||
InputDatetime *services.InputDatetime
|
||||
InputNumber *services.InputNumber
|
||||
Notify *services.Notify
|
||||
}
|
||||
|
||||
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),
|
||||
InputDatetime: services.BuildService[services.InputDatetime](conn, ctx),
|
||||
InputNumber: services.BuildService[services.InputNumber](conn, ctx),
|
||||
Notify: services.BuildService[services.Notify](conn, ctx),
|
||||
}
|
||||
}
|
||||
|
||||
9
types/requestTypes.go
Normal file
9
types/requestTypes.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user