Skip to content

Commit

Permalink
Fix smart cost type not published (evcc-io#11415)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jan 2, 2024
1 parent f517a8f commit f14b83c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 58 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func runRoot(cmd *cobra.Command, args []string) {
go socketHub.Run(pipe.NewDropper(ignoreEmpty).Pipe(tee.Attach()), cache)

// setup values channel
valueChan := make(chan util.Param, 1)
valueChan := make(chan util.Param, 64)
go tee.Run(valueChan)

// capture log messages for UI
Expand Down
62 changes: 26 additions & 36 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,52 +592,42 @@ func configureMessengers(conf messagingConfig, valueChan chan util.Param, cache
return messageChan, nil
}

func configureTariffs(conf tariffConfig) (tariff.Tariffs, error) {
var grid, feedin, co2, planner api.Tariff
var currencyCode currency.Unit = currency.EUR
var err error
func configureTariff(name string, conf config.Typed, t *api.Tariff, wg *sync.WaitGroup) {
defer wg.Done()

if conf.Currency != "" {
currencyCode = currency.MustParseISO(conf.Currency)
if conf.Type == "" {
return
}

if conf.Grid.Type != "" {
grid, err = tariff.NewFromConfig(conf.Grid.Type, conf.Grid.Other)
if err != nil {
grid = nil
log.ERROR.Printf("failed configuring grid tariff: %v", err)
}
res, err := tariff.NewFromConfig(conf.Type, conf.Other)
if err != nil {
log.ERROR.Printf("failed configuring %s tariff: %v", name, err)
return
}

if conf.FeedIn.Type != "" {
feedin, err = tariff.NewFromConfig(conf.FeedIn.Type, conf.FeedIn.Other)
if err != nil {
feedin = nil
log.ERROR.Printf("failed configuring feed-in tariff: %v", err)
}
}
*t = res
}

if conf.Co2.Type != "" {
co2, err = tariff.NewFromConfig(conf.Co2.Type, conf.Co2.Other)
if err != nil {
co2 = nil
log.ERROR.Printf("failed configuring co2 tariff: %v", err)
}
func configureTariffs(conf tariffConfig) (*tariff.Tariffs, error) {
tariffs := tariff.Tariffs{
Currency: currency.EUR,
}

if conf.Planner.Type != "" {
planner, err = tariff.NewFromConfig(conf.Planner.Type, conf.Planner.Other)
if err != nil {
planner = nil
log.ERROR.Printf("failed configuring planner tariff: %v", err)
} else if planner.Type() == api.TariffTypeCo2 {
log.WARN.Printf("tariff configuration changed, use co2 instead of planner for co2 tariff")
}
if conf.Currency != "" {
tariffs.Currency = currency.MustParseISO(conf.Currency)
}

tariffs := tariff.NewTariffs(currencyCode, grid, feedin, co2, planner)
var wg sync.WaitGroup
wg.Add(4)

go configureTariff("grid", conf.Grid, &tariffs.Grid, &wg)
go configureTariff("feedin", conf.FeedIn, &tariffs.FeedIn, &wg)
go configureTariff("co2", conf.Co2, &tariffs.Co2, &wg)
go configureTariff("planner", conf.Planner, &tariffs.Planner, &wg)

wg.Wait()

return *tariffs, nil
return &tariffs, nil
}

func configureDevices(conf globalConfig) error {
Expand Down Expand Up @@ -668,7 +658,7 @@ func configureSiteAndLoadpoints(conf globalConfig) (*core.Site, error) {
return configureSite(conf.Site, loadpoints, tariffs)
}

func configureSite(conf map[string]interface{}, loadpoints []*core.Loadpoint, tariffs tariff.Tariffs) (*core.Site, error) {
func configureSite(conf map[string]interface{}, loadpoints []*core.Loadpoint, tariffs *tariff.Tariffs) (*core.Site, error) {
site, err := core.NewSiteFromConfig(log, conf, loadpoints, tariffs)
if err != nil {
return nil, fmt.Errorf("failed configuring site: %w", err)
Expand Down
18 changes: 7 additions & 11 deletions core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ type Site struct {
bufferStartSoc float64 // start charging on battery above this Soc
batteryDischargeControl bool // prevent battery discharge for fast and planned charging

tariffs tariff.Tariffs // Tariff
loadpoints []*Loadpoint // Loadpoints
tariffs *tariff.Tariffs // Tariffs
coordinator *coordinator.Coordinator // Vehicles
prioritizer *prioritizer.Prioritizer // Power budgets
stats *Stats // Stats
Expand Down Expand Up @@ -111,7 +111,7 @@ func NewSiteFromConfig(
log *util.Logger,
other map[string]interface{},
loadpoints []*Loadpoint,
tariffs tariff.Tariffs,
tariffs *tariff.Tariffs,
) (*Site, error) {
site := NewSite()
if err := util.DecodeOther(other, site); err != nil {
Expand Down Expand Up @@ -822,20 +822,16 @@ func (site *Site) prepare() {
site.publish(keys.GridConfigured, site.gridMeter != nil)
site.publish(keys.PvConfigured, len(site.pvMeters) > 0)
site.publish(keys.BatteryConfigured, len(site.batteryMeters) > 0)
site.publish(keys.BufferSoc, site.bufferSoc)
site.publish(keys.BufferStartSoc, site.bufferStartSoc)
site.publish(keys.PrioritySoc, site.prioritySoc)
site.publish(keys.BatteryMode, site.batteryMode)
site.publish(keys.ResidualPower, site.ResidualPower)
site.publish(keys.SmartCostLimit, site.smartCostLimit)
site.publish(keys.SmartCostType, nil)

site.publish(keys.Currency, site.tariffs.Currency)
site.publish(keys.SmartCostActive, false)
if tariff := site.GetTariff(PlannerTariff); tariff != nil {
site.publish(keys.SmartCostType, tariff.Type())
} else {
site.publish(keys.SmartCostType, nil)
}
site.publish(keys.Currency, site.tariffs.Currency)

site.publish(keys.BatteryDischargeControl, site.batteryDischargeControl)
site.publish(keys.BatteryMode, site.batteryMode)

if err := site.restoreSettings(); err != nil {
site.log.ERROR.Println(err)
Expand Down
10 changes: 0 additions & 10 deletions tariff/tariffs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ type Tariffs struct {
Grid, FeedIn, Co2, Planner api.Tariff
}

func NewTariffs(currency currency.Unit, grid, feedin, co2 api.Tariff, planner api.Tariff) *Tariffs {
return &Tariffs{
Currency: currency,
Grid: grid,
FeedIn: feedin,
Co2: co2,
Planner: planner,
}
}

func currentPrice(t api.Tariff) (float64, error) {
if t != nil {
if rr, err := t.Rates(); err == nil {
Expand Down

0 comments on commit f14b83c

Please sign in to comment.