diff --git a/internal/charger/easee.go b/internal/charger/easee.go index a7fff35df..10a6a3e1f 100644 --- a/internal/charger/easee.go +++ b/internal/charger/easee.go @@ -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() { @@ -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) @@ -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 @@ -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() } }