From 327cf49ec6ec8190de751cf590825454ede49151 Mon Sep 17 00:00:00 2001 From: Markus Thierolf <77847348+thierolm@users.noreply.github.com> Date: Mon, 20 Sep 2021 13:10:19 +0200 Subject: [PATCH] Shelly: make optional user and password configurable (#1600) --- charger/shelly.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/charger/shelly.go b/charger/shelly.go index 606b31aed..2e9ec2ab3 100644 --- a/charger/shelly.go +++ b/charger/shelly.go @@ -3,7 +3,7 @@ package charger import ( "errors" "fmt" - "strings" + "net/url" "github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/util" @@ -41,6 +41,8 @@ func init() { func NewShellyFromConfig(other map[string]interface{}) (api.Charger, error) { cc := struct { URI string + User string + Password string Channel int StandbyPower float64 }{} @@ -53,14 +55,19 @@ func NewShellyFromConfig(other map[string]interface{}) (api.Charger, error) { return nil, errors.New("missing uri") } - return NewShelly(cc.URI, cc.Channel, cc.StandbyPower) + return NewShelly(cc.URI, cc.User, cc.Password, cc.Channel, cc.StandbyPower) } // NewShelly creates Shelly charger -func NewShelly(uri string, channel int, standbypower float64) (*Shelly, error) { +func NewShelly(uri, user, password string, channel int, standbypower float64) (*Shelly, error) { + u, _ := url.Parse(uri) + if u.Host != "" { + uri = u.Host + } + c := &Shelly{ Helper: request.NewHelper(util.NewLogger("shelly")), - uri: strings.TrimRight(uri, "/"), + uri: fmt.Sprintf("http://%s:%s@%s", user, password, uri), channel: channel, standbypower: standbypower, }