Push: re-add vehicle title (#11709)
Some checks failed
Some checks failed
This commit is contained in:
parent
b996033974
commit
ffc3ccdc23
3 changed files with 19 additions and 4 deletions
|
|
@ -214,7 +214,7 @@ func runRoot(cmd *cobra.Command, args []string) {
|
|||
// setup messaging
|
||||
var pushChan chan push.Event
|
||||
if err == nil {
|
||||
pushChan, err = configureMessengers(conf.Messaging, valueChan, cache)
|
||||
pushChan, err = configureMessengers(conf.Messaging, site.Vehicles(), valueChan, cache)
|
||||
}
|
||||
|
||||
// run shutdown functions on stop
|
||||
|
|
|
|||
|
|
@ -571,10 +571,10 @@ func configureEEBus(conf map[string]interface{}) error {
|
|||
}
|
||||
|
||||
// setup messaging
|
||||
func configureMessengers(conf messagingConfig, valueChan chan util.Param, cache *util.Cache) (chan push.Event, error) {
|
||||
func configureMessengers(conf messagingConfig, vehicles push.Vehicles, valueChan chan util.Param, cache *util.Cache) (chan push.Event, error) {
|
||||
messageChan := make(chan push.Event, 1)
|
||||
|
||||
messageHub, err := push.NewHub(conf.Events, cache)
|
||||
messageHub, err := push.NewHub(conf.Events, vehicles, cache)
|
||||
if err != nil {
|
||||
return messageChan, fmt.Errorf("failed configuring push services: %w", err)
|
||||
}
|
||||
|
|
|
|||
17
push/hub.go
17
push/hub.go
|
|
@ -6,6 +6,7 @@ import (
|
|||
"text/template"
|
||||
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"github.com/evcc-io/evcc/core/vehicle"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
)
|
||||
|
||||
|
|
@ -20,15 +21,21 @@ type EventTemplateConfig struct {
|
|||
Title, Msg string
|
||||
}
|
||||
|
||||
type Vehicles interface {
|
||||
// ByName returns a single vehicle adapter by name
|
||||
ByName(string) (vehicle.API, error)
|
||||
}
|
||||
|
||||
// Hub subscribes to event notifications and sends them to client devices
|
||||
type Hub struct {
|
||||
definitions map[string]EventTemplateConfig
|
||||
sender []Messenger
|
||||
cache *util.Cache
|
||||
vehicles Vehicles
|
||||
}
|
||||
|
||||
// NewHub creates push hub with definitions and receiver
|
||||
func NewHub(cc map[string]EventTemplateConfig, cache *util.Cache) (*Hub, error) {
|
||||
func NewHub(cc map[string]EventTemplateConfig, vv Vehicles, cache *util.Cache) (*Hub, error) {
|
||||
// instantiate all event templates
|
||||
for k, v := range cc {
|
||||
if _, err := template.New("out").Funcs(sprig.TxtFuncMap()).Parse(v.Title); err != nil {
|
||||
|
|
@ -42,6 +49,7 @@ func NewHub(cc map[string]EventTemplateConfig, cache *util.Cache) (*Hub, error)
|
|||
h := &Hub{
|
||||
definitions: cc,
|
||||
cache: cache,
|
||||
vehicles: vv,
|
||||
}
|
||||
|
||||
return h, nil
|
||||
|
|
@ -68,6 +76,13 @@ func (h *Hub) apply(ev Event, tmpl string) (string, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// add missing attributes
|
||||
if name, ok := attr["vehicleName"].(string); ok {
|
||||
if v, err := h.vehicles.ByName(name); err == nil {
|
||||
attr["vehicleTitle"] = v.Instance().Title()
|
||||
}
|
||||
}
|
||||
|
||||
return util.ReplaceFormatted(tmpl, attr)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue