LGEss: support user login (#10009)
This commit is contained in:
parent
85517da4ff
commit
cd044259de
3 changed files with 38 additions and 13 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 }}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue