OpenWB: fix enabled state detection (#9651)
This commit is contained in:
parent
f594c9fca3
commit
4c6105faa7
3 changed files with 24 additions and 13 deletions
|
|
@ -4,6 +4,8 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
)
|
||||
|
||||
// ensureCharger extracts VIN from list of VINs returned from `list` function
|
||||
|
|
@ -52,3 +54,15 @@ func ensureChargerWithFeature[Charger, Feature any](
|
|||
func bytesAsString(b []byte) string {
|
||||
return strings.TrimSpace(string(bytes.TrimLeft(b, "\x00")))
|
||||
}
|
||||
|
||||
// verifyEnabled validates the enabled state against the charger status
|
||||
func verifyEnabled(c api.Charger, enabled bool) (bool, error) {
|
||||
if enabled {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
status, err := c.Status()
|
||||
|
||||
// always treat charging as enabled
|
||||
return status == api.StatusC, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -177,7 +177,12 @@ func (m *OpenWB) Enable(enable bool) error {
|
|||
}
|
||||
|
||||
func (m *OpenWB) Enabled() (bool, error) {
|
||||
return m.enabled, nil
|
||||
enabled, err := verifyEnabled(m, m.enabled)
|
||||
if err == nil {
|
||||
m.enabled = enabled
|
||||
}
|
||||
|
||||
return enabled, err
|
||||
}
|
||||
|
||||
func (m *OpenWB) Status() (api.ChargeStatus, error) {
|
||||
|
|
|
|||
|
|
@ -91,20 +91,12 @@ func NewTwc3FromConfig(other map[string]interface{}) (api.Charger, error) {
|
|||
|
||||
// Enabled implements the api.Charger interface
|
||||
func (c *Twc3) Enabled() (bool, error) {
|
||||
if c.enabled {
|
||||
return c.enabled, nil
|
||||
enabled, err := verifyEnabled(c, c.enabled)
|
||||
if err == nil {
|
||||
c.enabled = enabled
|
||||
}
|
||||
|
||||
status, err := c.Status()
|
||||
if err != nil {
|
||||
return c.enabled, err
|
||||
}
|
||||
|
||||
if status == api.StatusC {
|
||||
c.enabled = true
|
||||
}
|
||||
|
||||
return c.enabled, nil
|
||||
return enabled, err
|
||||
}
|
||||
|
||||
// Enable implements the api.Charger interface
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue