Skip to content

Commit

Permalink
Push: re-add vehicle title (evcc-io#11709)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jan 16, 2024
1 parent b996033 commit ffc3ccd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 16 additions & 1 deletion push/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit ffc3ccd

Please sign in to comment.