Skip to content

Commit

Permalink
Add ability to retry if SerialException occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
davet2001 committed May 8, 2023
1 parent 32cbb19 commit f745523
Showing 1 changed file with 50 additions and 34 deletions.
84 changes: 50 additions & 34 deletions homeassistant/components/aurora_abb_powerone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
# Developer note:
# vscode devcontainer: use the following to access USB device:
# "runArgs": ["-e", "GIT_EDITOR=code --wait", "--device=/dev/ttyUSB0"],
# and add the following to the end of script/bootstrap:
# sudo chmod 777 /dev/ttyUSB0

import logging
from time import sleep

from aurorapy.client import AuroraError, AuroraSerialClient, AuroraTimeoutError
from serial import SerialException

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_PORT, Platform
Expand Down Expand Up @@ -67,40 +71,52 @@ def _update_data(self) -> dict[str, float]:
This is the only function that should fetch new data for Home Assistant.
"""
data: dict[str, float] = {}
try:
self.available_prev = self.available
self.client.connect()

# read ADC channel 3 (grid power output)
power_watts = self.client.measure(3, True)
data["instantaneouspower"] = round(power_watts, 1)

temperature_c = self.client.measure(21)
data["temp"] = round(temperature_c, 1)

energy_wh = self.client.cumulated_energy(5)
data["totalenergy"] = round(energy_wh / 1000, 2)
self.available = True

except AuroraTimeoutError:
data = {}
self.available = False
_LOGGER.debug("No response from inverter (could be dark)")
except AuroraError as error:
data = {}
self.available = False
raise error
finally:
if self.available != self.available_prev:
if self.available:
_LOGGER.info("Communication with %s back online", self.name)
else:
_LOGGER.warning(
"Communication with %s lost",
self.name,
)
if self.client.serline.isOpen():
self.client.close()
retries: int = 3
while retries > 0:
try:
self.available_prev = self.available
self.client.connect()

# read ADC channel 3 (grid power output)
power_watts = self.client.measure(3, True)
data["instantaneouspower"] = round(power_watts, 1)

temperature_c = self.client.measure(21)
data["temp"] = round(temperature_c, 1)

energy_wh = self.client.cumulated_energy(5)
data["totalenergy"] = round(energy_wh / 1000, 2)
self.available = True
self.data = data
retries = 0
except AuroraTimeoutError:
self.data = {}
self.available = False
_LOGGER.debug("No response from inverter (could be dark)")
retries = 0
except AuroraError as error:
self.data = {}
self.available = False
retries = 0
raise error
except SerialException as ex:
retries -= 1
_LOGGER.warning(
"Exception: %s occurred, %d retries remaining", retries, repr(ex)
)
sleep(1)

finally:
if self.available != self.available_prev:
if self.available:
_LOGGER.info("Communication with %s back online", self.name)
else:
_LOGGER.warning(
"Communication with %s lost",
self.name,
)
if self.client.serline.isOpen():
self.client.close()

return data

Expand Down

0 comments on commit f745523

Please sign in to comment.