Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enphase Envoy with token-based authentication #8146

Closed
salz3n opened this issue May 23, 2023 · 7 comments · Fixed by #8247
Closed

Enphase Envoy with token-based authentication #8146

salz3n opened this issue May 23, 2023 · 7 comments · Fixed by #8247
Labels
devices Specific device support

Comments

@salz3n
Copy link
Contributor

salz3n commented May 23, 2023

BRAUCHE UNTERSTÜTZUNG BEI DER PROGRAMMIERUNG

In Issue #7779 wurde ein Template für das Enphase Envoy-S implementiert.

Leider wurde bei der neueren Enphase Envoy Firmware der Zugriff auf eine Token-Authentifizierung umgestellt. Daher geht das aktuelle Template https://github.com/evcc-io/evcc/blob/master/templates/definition/meter/enphase.yaml nicht mehr für die neuesten Enphase-Versionen.

Ich habe das Template leicht angepasst und konnte es zumindest als Proof-of-Concept wieder zum Laufen bringen:

/templates/definition/meter/enphase.yaml

template: enphase
products:
  - brand: Enphase
    description:
      generic: IQ Envoy
params:
  - name: usage
    choice: ["pv"]
  - name: host
    required: true
render: |
  type: custom
  power:
    source: http
    uri: https://{{ .host }}/api/v1/production
    method: GET
    auth:
      type: bearer
      password: <THE TOKEN>
    insecure: true
    jq: .wattsNow
  energy:
    source: http
    uri: https://{{ .host }}/api/v1/production
    method: GET
    auth:
      type: bearer
      password: <THE TOKEN>
    insecure: true
    jq: .wattHoursLifetime
    scale: 0.001

Jedoch gibt es noch zwei Probleme, welche vor einem Pull Request gelöst werden müssten. Vielleicht findet sich ja jemand, der mich unterstützen kann, da ich noch nie in Go programmiert habe.

Problem 1: Token-Generierung

Das Authentifizierung-Token (oben im Code mit <THE TOKEN> benannt) hat nur eine Laufzeit von 1 Jahr und müsste daher am besten automatisiert vom evcc Code erzeugt und vor Ablauf erneuert werden.

Der genaue Prozess ist hier sehr gut beschrieben: https://enphase.com/download/iq-gateway-access-using-local-apis-or-local-ui-token-based-authentication-tech-brief

Es gibt auch ein funktionierendes Skript in Python. Dieses müsste vermutlich in Go umgeschrieben und dann irgendwie in den evcc Code integriert werden:

import json
import requests
user=’<UserName>’
password='<Password>'
envoy_serial=’< Envoy_Serial_No>’
data = {'user[email]': user, 'user[password]': password}
response = requests.post('https://enlighten.enphaseenergy.com/login/login.json?', data=data) 
response_data = json.loads(response.text)
data = {'session_id': response_data['session_id'], 'serial_num': envoy_serial, 'username': user}
response = requests.post('https://entrez.enphaseenergy.com/tokens', json=data) 
token_raw = response.text

Problem 2: Endpoint production.json funktioniert nicht mehr

Der Endpoint /production.json ist leider zur Zeit noch nicht mit Hilfe des Bearer Tokens abrufbar. Dies scheint ein Bug von der Enphase API zu sein, da nur manche API-Endpunkte mit dem Token funktionieren - andere aber nur mit einem Session-Cookie (Erst im Browser lokal in den Envoy mit Hilfe vom Bearer Token einloggen, dann den Cookie-Wert sessionId auslesen).

Es gibt jedoch noch den Endpoint /ivp/meters/readings. Dieser scheint ähnliche Werte zu liefern. Ich denke, dass man activePower nutzen könnte.

[
	{
		...
		"timestamp": 1685003344,
		"actEnergyDlvd": 70854.625,
		"actEnergyRcvd": 86.873,
		"apparentEnergy": 86657.288,
		"reactEnergyLagg": 25699.533,
		"reactEnergyLead": 6.603,
		"instantaneousDemand": 5298.611,
		"activePower": 5298.611,
		"apparentPower": 5324.968,
		"reactivePower": 358.640,
		"pwrFactor": 0.993,
		"voltage": 711.846,
		"current": 22.440,
		"freq": 50.062,
		...

Ich würde mich sehr freuen, wenn sich jemand findet, mit dem ich das Template fertig entwickeln kann.

@premultiply premultiply added the devices Specific device support label May 23, 2023
@andig
Copy link
Member

andig commented May 23, 2023

Ich sehe nicht wie wir Token Auth generisch implementieren könnten. Das würde dann wohl eine eigene Go Umsetzung erfordern, Beispiele mit HTTP finden sich diverse, z.B. die Easee (OAuth2).

@pitware2005
Copy link

@salz3n danke für Deine Arbeit.

Meine Meinung zu Deinen 2 Punkten wäre:

  1. Das Token ist 12 Monate gültig, das sollte also kein Problem darstellen es 1x im Jahr von Hand zu erneuern.
  2. Bei dem anderen Endpunkt gibt es da überhaupt alle wichtigen Daten? Also Produktion und Bezug?

Kannst Du bitte die anderen Punkte auch einbauen? Also Bezug usw. Eben was wichtig ist.

@andig könnten bitte in dem aktuellen Template auch der Bezug mit aufgenommen werden. Damit man es nicht über irgendwelche Umwege bauen muss?!

@andig
Copy link
Member

andig commented May 28, 2023

Ein PR für das Template wäre schön

@salz3n
Copy link
Contributor Author

salz3n commented May 31, 2023

Bei mir wurde gestern das neuste Update im Enphase Envoy installiert und nun bekomme ich endlich alle Werte in der API korrekt ausgeliefert. Ich werde im Laufe der Woche noch ein PR erstellen.

Wenn ich das richtig sehe, dann kann ich mit dem Template dann meters für pv, grid und battery implementieren. Jedoch habe ich keine Batterie und könnte somit nur pv und grid testen.

@pitware2005 Hast Du eine Enphase-Installation mit Software 7.x und Batterie zum Testen von meinem PR?

@bgrigoriu
Copy link
Contributor

bgrigoriu commented Jun 4, 2023

HI everyone,

A have some difficulties in setting up an IQ enphase with 7.X firmware. Can you make a short step by step to-do list with the settings or a link to a newbie style tutorial ?

@salz3n
Copy link
Contributor Author

salz3n commented Jun 4, 2023

@bgrigoriu It shouldn't be too complex, but you will have to wait until the support of firmware 7.X is added to the code. The maintainers of the project need to include the pull request #8247 into a release of evcc. Until this change is not included the Enphase Envoy 7.x is not supported by evcc.

@bgrigoriu
Copy link
Contributor

Thanks for the comment.
I will wait.
Unfortunately I am not at all a Go programmer and I cannot help.
For the 5.XX firmware a wrote a routine in C++ which gets a stream reading from the IQ envoy with 2x reads/ sec on the local network
I can write somthing similar thing for the 7.X but since it uses a token for authethican it has to pass through their servers and it is not possible to get a such granular reading ( but I am not sure at all that this is necesary).
When the issue is fixed Please let me know. I am craving tu use the system which seams to very very well designed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devices Specific device support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants