From cd044259de47863ba44e2c3d7bc2cd37899e923e Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 29 Sep 2023 16:33:09 +0200 Subject: [PATCH] LGEss: support user login (#10009) --- meter/lgess.go | 13 ++++++------ meter/lgpcs/lgpcs.go | 21 +++++++++++++++---- .../definition/meter/lg-ess-home-8-10.yaml | 17 ++++++++++++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/meter/lgess.go b/meter/lgess.go index 430237879..2570cf989 100644 --- a/meter/lgess.go +++ b/meter/lgess.go @@ -54,9 +54,10 @@ func init() { // NewLgEssFromConfig creates an LgEss Meter from generic config func NewLgEssFromConfig(other map[string]interface{}) (api.Meter, error) { cc := struct { - capacity `mapstructure:",squash"` - URI, Usage, Password string - Cache time.Duration + capacity `mapstructure:",squash"` + URI, Usage string + Registration, Password string + Cache time.Duration }{ Cache: time.Second, } @@ -69,12 +70,12 @@ func NewLgEssFromConfig(other map[string]interface{}) (api.Meter, error) { return nil, errors.New("missing usage") } - return NewLgEss(cc.URI, cc.Usage, cc.Password, cc.Cache, cc.capacity.Decorator()) + return NewLgEss(cc.URI, cc.Usage, cc.Registration, cc.Password, cc.Cache, cc.capacity.Decorator()) } // NewLgEss creates an LgEss Meter -func NewLgEss(uri, usage, password string, cache time.Duration, capacity func() float64) (api.Meter, error) { - lp, err := lgpcs.GetInstance(uri, password, cache) +func NewLgEss(uri, usage, registration, password string, cache time.Duration, capacity func() float64) (api.Meter, error) { + lp, err := lgpcs.GetInstance(uri, registration, password, cache) if err != nil { return nil, err } diff --git a/meter/lgpcs/lgpcs.go b/meter/lgpcs/lgpcs.go index 7f63ec9ef..35d1297f6 100644 --- a/meter/lgpcs/lgpcs.go +++ b/meter/lgpcs/lgpcs.go @@ -18,8 +18,9 @@ import ( // URIs const ( - LoginURI = "/v1/login" - MeterURI = "/v1/user/essinfo/home" + LoginURI = "/v1/login" + UserLoginURI = "/v1/user/setting/login" + MeterURI = "/v1/user/essinfo/home" ) type MeterResponse struct { @@ -42,6 +43,7 @@ type EssData struct { type Com struct { *request.Helper uri string // URI address of the LG ESS inverter - e.g. "https://192.168.1.28" + authPath string password string // registration number of the LG ESS Inverter - e.g. "DE2001..." authKey string // auth_key returned during login and renewed with new login after expiration dataG func() (EssData, error) @@ -53,7 +55,7 @@ var ( ) // GetInstance implements the singleton pattern to handle the access via the authkey to the PCS of the LG ESS HOME system -func GetInstance(uri, password string, cache time.Duration) (*Com, error) { +func GetInstance(uri, registration, password string, cache time.Duration) (*Com, error) { uri = util.DefaultScheme(strings.TrimSuffix(uri, "/"), "https") var err error @@ -62,9 +64,15 @@ func GetInstance(uri, password string, cache time.Duration) (*Com, error) { instance = &Com{ Helper: request.NewHelper(log), uri: uri, + authPath: UserLoginURI, password: password, } + if registration != "" { + instance.authPath = LoginURI + instance.password = registration + } + // ignore the self signed certificate instance.Client.Transport = request.NewTripper(log, transport.Insecure()) @@ -78,6 +86,11 @@ func GetInstance(uri, password string, cache time.Duration) (*Com, error) { } }) + // check if different uris are provided + if password != "" && registration != "" { + return nil, errors.New("cannot have registration and password") + } + // check if different uris are provided if uri != "" && instance.uri != uri { return nil, fmt.Errorf("uri mismatch: %s vs %s", instance.uri, uri) @@ -97,7 +110,7 @@ func (m *Com) Login() error { "password": m.password, } - req, err := request.New(http.MethodPut, m.uri+LoginURI, request.MarshalJSON(data), request.JSONEncoding) + req, err := request.New(http.MethodPut, m.uri+m.authPath, request.MarshalJSON(data), request.JSONEncoding) if err != nil { return err } diff --git a/templates/definition/meter/lg-ess-home-8-10.yaml b/templates/definition/meter/lg-ess-home-8-10.yaml index 26883a86d..f07a59d43 100644 --- a/templates/definition/meter/lg-ess-home-8-10.yaml +++ b/templates/definition/meter/lg-ess-home-8-10.yaml @@ -8,11 +8,17 @@ params: choice: ["grid", "pv", "battery"] allinone: true - name: host + - name: password + help: + en: | + User password, see https://github.com/Morluktom/ioBroker.lg-ess-home/tree/master#getting-the-password.Alteratively, use registration id for admin login. + de: | + Benutzerpasswort, siehe https://github.com/Morluktom/ioBroker.lg-ess-home/tree/master#getting-the-password.Alterativ kann die Registriernummer für Administratorlogin verwendet werden. - name: registration - required: true + advanced: true example: "DE200..." help: - en: Registration number of the LG ESS HOME inverter. + en: Registration id of the LG ESS HOME inverter. de: Registriernummer des LG ESS HOME Wechselrichters. - name: capacity advanced: true @@ -21,7 +27,12 @@ render: | usage: {{ .usage }} # uri and password are only required once if multiple lgess usages are defined uri: https://{{ .host }} - password: {{ .registration }} + {{- if .password }} + password: {{ .password }} + {{- end }} + {{- if .registration }} + registration: {{ .registration }} + {{- end }} {{- if .capacity }} capacity: {{ .capacity }} # kWh {{- end }}