diff --git a/provider/helper.go b/provider/helper.go index da9cd94881..94d2e8aed2 100644 --- a/provider/helper.go +++ b/provider/helper.go @@ -3,6 +3,7 @@ package provider import ( "fmt" + "github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/util" ) @@ -16,3 +17,17 @@ func setFormattedValue(message, param string, v interface{}) (string, error) { param: v, }) } + +// knownErrors maps string responses to known error codes +func knownErrors(b []byte) error { + switch string(b) { + case "ErrAsleep": + return api.ErrAsleep + case "ErrMustRetry": + return api.ErrMustRetry + case "ErrNotAvailable": + return api.ErrNotAvailable + default: + return nil + } +} diff --git a/provider/http.go b/provider/http.go index 6ee6f0eedc..b47c0b02dc 100644 --- a/provider/http.go +++ b/provider/http.go @@ -168,6 +168,11 @@ func (p *HTTP) request(url string, body string) ([]byte, error) { } p.val, p.err = p.DoBody(req) + if p.err != nil { + if err := knownErrors(p.val); err != nil { + p.err = err + } + } p.updated = time.Now() } diff --git a/provider/mqtt_handler.go b/provider/mqtt_handler.go index 4597348517..acc11d3e56 100644 --- a/provider/mqtt_handler.go +++ b/provider/mqtt_handler.go @@ -27,6 +27,10 @@ func (h *msgHandler) hasValue() (string, error) { return "", err } + if err := knownErrors([]byte(payload)); err != nil { + return "", err + } + if h.pipeline != nil { b, err := h.pipeline.Process([]byte(payload)) return string(b), err diff --git a/provider/socket.go b/provider/socket.go index 39b45f164c..f66171cda8 100644 --- a/provider/socket.go +++ b/provider/socket.go @@ -152,7 +152,15 @@ var _ StringProvider = (*Socket)(nil) func (p *Socket) StringGetter() (func() (string, error), error) { return func() (string, error) { val, err := p.val.Get() - return string(val), err + if err != nil { + return "", err + } + + if err := knownErrors(val); err != nil { + return "", err + } + + return string(val), nil }, nil }