Skip to content

Commit

Permalink
Sync Easee Smart with Now/PV Modes (evcc-io#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
naltatis authored May 12, 2021
1 parent d868af0 commit a71a036
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions internal/charger/easee.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import (
// Easee charger implementation
type Easee struct {
*request.Helper
charger string
site, circuit int
status easee.ChargerStatus
updated time.Time
cache time.Duration
lp core.LoadPointAPI
charger string
site, circuit int
status easee.ChargerStatus
updated time.Time
cache time.Duration
lp core.LoadPointAPI
lastSmartCharging bool
lastChargeMode api.ChargeMode
log *util.Logger
}

func init() {
Expand Down Expand Up @@ -60,6 +63,7 @@ func NewEasee(user, password, charger string, cache time.Duration) (*Easee, erro
Helper: request.NewHelper(log),
charger: charger,
cache: cache,
log: log,
}

ts, err := easee.TokenSource(log, user, password)
Expand Down Expand Up @@ -125,6 +129,47 @@ func (c *Easee) chargerDetails() (res easee.Site, err error) {
return res, err
}

func (c *Easee) syncSmartCharging() error {
if c.lp == nil {
return nil
}

if c.lp.GetMode() != c.lastChargeMode {
c.log.DEBUG.Printf("charge mode changed by loadpoint: %v -> %v", c.lastChargeMode, c.lp.GetMode())
newSmartCharging := false
if c.lp.GetMode() == api.ModePV {
newSmartCharging = true
}

data := easee.ChargerSettings{
SmartCharging: &newSmartCharging,
}

var req *http.Request
uri := fmt.Sprintf("%s/chargers/%s/settings", easee.API, c.charger)
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding)
if err == nil {
_, err = c.Do(req)
c.updated = time.Time{} // clear cache
c.lastChargeMode = c.lp.GetMode()
c.lastSmartCharging = newSmartCharging
}
return err
}

if c.lastSmartCharging != c.status.SmartCharging {
c.log.DEBUG.Printf("smart status changed by charger: %v -> %v", c.lastSmartCharging, c.status.SmartCharging)
if c.status.SmartCharging {
c.lp.SetMode(api.ModePV)
} else {
c.lp.SetMode(api.ModeNow)
}
c.lastSmartCharging = c.status.SmartCharging
c.lastChargeMode = c.lp.GetMode()
}
return nil
}

func (c *Easee) state() (easee.ChargerStatus, error) {
if time.Since(c.updated) < c.cache {
return c.status, nil
Expand All @@ -134,6 +179,7 @@ func (c *Easee) state() (easee.ChargerStatus, error) {
req, err := request.New(http.MethodGet, uri, nil, request.JSONEncoding)
if err == nil {
if err = c.DoJSON(req, &c.status); err == nil {
err = c.syncSmartCharging()
c.updated = time.Now()
}
}
Expand Down

0 comments on commit a71a036

Please sign in to comment.