2828ENVOY_MODEL_M = "Metered"
2929ENVOY_MODEL_S = "Standard"
3030
31+ ENLIGHTEN_AUTH_URL = "https://enlighten.enphaseenergy.com/login/login.json?"
32+ ENLIGHTEN_TOKEN_URL = "https://entrez.enphaseenergy.com/tokens"
33+
3134# paths used for fetching enlighten token through envoy
3235ENLIGHTEN_LOGIN_URL = "https://entrez.enphaseenergy.com/login"
3336ENDPOINT_URL_GET_JWT = "https://{}/auth/get_jwt"
@@ -784,6 +787,7 @@ def __init__(
784787 disabled_endpoints = [],
785788 lifetime_production_correction = 0 ,
786789 device_data_endpoint = "endpoint_device_data" ,
790+ token_method_envoy = False ,
787791 ):
788792 """Init the EnvoyReader."""
789793 self .host = host .lower ()
@@ -809,6 +813,7 @@ def __init__(
809813 self .disabled_endpoints = disabled_endpoints
810814 self .lifetime_production_correction = lifetime_production_correction
811815 self .device_data_endpoint = device_data_endpoint
816+ self .token_method_envoy = token_method_envoy
812817
813818 self .uri_registry = {}
814819 for key , endpoint in ENVOY_ENDPOINTS .items ():
@@ -970,6 +975,36 @@ async def _async_put(self, url, data, **kwargs):
970975 _LOGGER .debug ("TransportError: %s" , e )
971976 raise e
972977
978+ async def _fetch_enlighten_token_json (self ):
979+ """
980+ Try to fetch the token json from Enlighten API
981+ :return:
982+ """
983+ _LOGGER .debug ("Fetching enlighten token" )
984+ async with self .async_client as client :
985+ # login to Enlighten
986+ payload_login = {
987+ "user[email]" : self .enlighten_user ,
988+ "user[password]" : self .enlighten_pass ,
989+ }
990+ resp = await client .post (ENLIGHTEN_AUTH_URL , data = payload_login , timeout = 30 )
991+ if resp .status_code >= 400 :
992+ raise EnlightenError ("Could not Authenticate via Enlighten" )
993+
994+ # now that we're in a logged in session, we can request the installer token
995+ login_data = resp .json ()
996+ payload_token = {
997+ "session_id" : login_data ["session_id" ],
998+ "serial_num" : self .enlighten_serial_num ,
999+ "username" : self .enlighten_user ,
1000+ }
1001+ resp = await client .post (
1002+ ENLIGHTEN_TOKEN_URL , json = payload_token , timeout = 30
1003+ )
1004+ if resp .status_code != 200 :
1005+ raise EnlightenError ("Could not get enlighten token" )
1006+ return resp .text
1007+
9731008 async def _fetch_envoy_token_json (self ):
9741009 """
9751010 Fetch a token, using the same procedure envoy uses in the webUI
@@ -1039,7 +1074,10 @@ async def _fetch_envoy_token_json(self):
10391074 return resp .json ()["access_token" ]
10401075
10411076 async def _get_enphase_token (self ):
1042- self ._token = await self ._fetch_envoy_token_json ()
1077+ if self .token_method_envoy :
1078+ self ._token = await self ._fetch_envoy_token_json ()
1079+ else :
1080+ self ._token = await self ._fetch_enlighten_token_json ()
10431081
10441082 _LOGGER .debug ("Envoy Token" )
10451083 if self ._is_enphase_token_expired (self ._token ):
0 commit comments