Skip to content

Commit

Permalink
Merge pull request #89 from gtdiehl/token_firmware
Browse files Browse the repository at this point in the history
Fix issue where Token was not being saved after each polling cycle
  • Loading branch information
gtdiehl authored Jan 21, 2022
2 parents 9d1be53 + f6967c7 commit 99d208f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions envoy_reader/envoy_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

LOGIN_URL = "https://entrez.enphaseenergy.com/login"
TOKEN_URL = "https://entrez.enphaseenergy.com/entrez_tokens"
TOKEN = ""

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -107,6 +106,7 @@ def __init__( # pylint: disable=too-many-arguments
self.enlighten_site_id = enlighten_site_id
self.enlighten_serial_num = enlighten_serial_num
self.https_flag = https_flag
self._token = ""

@property
def async_client(self):
Expand Down Expand Up @@ -205,22 +205,24 @@ async def _getEnphaseToken( # pylint: disable=invalid-name
)

parsed_html = BeautifulSoup(response.text, features="html.parser")
TOKEN = parsed_html.body.find( # pylint: disable=invalid-name, unused-variable, redefined-outer-name
self._token = parsed_html.body.find( # pylint: disable=invalid-name, unused-variable, redefined-outer-name
"textarea"
).text
_LOGGER.debug("Commissioned Token: %s", TOKEN)
_LOGGER.debug("Commissioned Token: %s", self._token)

else:
payload_token = {"uncommissioned": "true", "Site": ""}
response = await self._async_post(
TOKEN_URL, data=payload_token, cookies=resp.cookies
)
soup = BeautifulSoup(response.text, features="html.parser")
TOKEN = soup.find("textarea").contents[0] # pylint: disable=invalid-name
_LOGGER.debug("Uncommissioned Token: %s", TOKEN)
self._token = soup.find("textarea").contents[
0
] # pylint: disable=invalid-name
_LOGGER.debug("Uncommissioned Token: %s", self._token)

# Create HTTP Header
self._authorization_header = {"Authorization": "Bearer " + TOKEN}
self._authorization_header = {"Authorization": "Bearer " + self._token}

# Fetch the Enphase Token status from the local Envoy
token_validation_html = await self._async_fetch_with_retry(
Expand Down Expand Up @@ -270,14 +272,14 @@ async def getData(self, getInverters=True): # pylint: disable=invalid-name

# Check if the Secure flag is set
if self.https_flag == "s":
_LOGGER.debug("Checking Token value: %s", TOKEN)
_LOGGER.debug("Checking Token value: %s", self._token)
# Check if a token has already been retrieved
if TOKEN == "":
_LOGGER.debug("Found empty token: %s", TOKEN)
if self._token == "":
_LOGGER.debug("Found empty token: %s", self._token)
await self._getEnphaseToken()
else:
_LOGGER.debug("Token is populated: %s", TOKEN)
if self._is_enphase_token_expired(TOKEN):
_LOGGER.debug("Token is populated: %s", self._token)
if self._is_enphase_token_expired(self._token):
_LOGGER.debug("Found Expired token - Retrieving new token")
await self._getEnphaseToken()

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.21.2
current_version = 0.21.3
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

setuptools.setup(
name="envoy_reader",
version="0.21.2",
version="0.21.3",
author="Jesse Rizzo",
author_email="[email protected]",
description="A program to read from an Enphase Envoy on the local network",
Expand Down

0 comments on commit 99d208f

Please sign in to comment.