Push: re-add vehicle title (#11709)
Some checks failed
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Demo (push) Has been cancelled
Release / Hassio Addon (push) Has been cancelled

This commit is contained in:
andig 2024-01-16 21:56:58 +01:00 • committed by GitHub
parent b996033974
commit ffc3ccdc23
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 4 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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)
}