diff --git a/envoy_reader/envoy_reader.py b/envoy_reader/envoy_reader.py index 43cd4d4..314eaa5 100644 --- a/envoy_reader/envoy_reader.py +++ b/envoy_reader/envoy_reader.py @@ -71,30 +71,34 @@ async def getData(self): await self.detect_model() if(self.get_inverters): - """If a password was not given as an argument when instantiating - the EnvoyReader object than use the last six numbers of the serial - number as the password. Otherwise use the password argument value.""" - if self.password == "": - if self.serial_number_last_six == "": + for i in range(0,3): + while True: + """If a password was not given as an argument when instantiating + the EnvoyReader object than use the last six numbers of the serial + number as the password. Otherwise use the password argument value.""" + if self.password == "": + if self.serial_number_last_six == "": + try: + await self.get_serial_number() + except httpx.HTTPError: + raise + self.password = self.serial_number_last_six try: - await self.get_serial_number() + async with httpx.AsyncClient() as client: + response = await client.get(ENDPOINT_URL_PRODUCTION_INVERTERS.format(self.host), + timeout=30, auth=httpx.DigestAuth(self.username, self.password)) + if response is not None and response.status_code != 401: + self.endpoint_production_inverters = response + else: + response.raise_for_status() + except (httpcore.RemoteProtocolError, httpx.RemoteProtocolError) as err: + continue except httpx.HTTPError: - raise - self.password = self.serial_number_last_six - try: - async with httpx.AsyncClient() as client: - response = await client.get(ENDPOINT_URL_PRODUCTION_INVERTERS.format(self.host), - timeout=30, auth=httpx.DigestAuth(self.username, self.password)) - if response is not None and response.status_code != 401: - self.endpoint_production_inverters = response - else: - response.raise_for_status() - except (httpx.RemoteProtocolError): - raise - except (httpcore.RemoteProtocolError, httpx.RemoteProtocolError): - await response.close() - except httpx.HTTPError: - response.raise_for_status() + response.raise_for_status() + break + break + if(i == 2): + raise httpx.RemoteProtocolError(message='Malformed request. Failed after 3 retries.', request=None) async def detect_model(self): """Method to determine if the Envoy supports consumption values or diff --git a/setup.py b/setup.py index 213363c..12235ed 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="envoy_reader", - version="0.18.1", + version="0.18.2", author="Jesse Rizzo", author_email="jesse.rizzo@gmail.com", description="A program to read from an Enphase Envoy on the local network",