Skip to content

Commit 9f52061

Browse files
committed
Handle missing battery capacity in SEMS API response gracefully
Enclosed the retrieval in a try-except block to catch KeyError and TypeError. Added a warning log when defaulting to a predefined value of 10 kWh
1 parent 114f05b commit 9f52061

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

source/inverter.py

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def __init__(self):
1818

1919
self.sems_portal_api_handler = SemsPortalApiHandler()
2020
self.battery_capacity = None
21+
# We have to pull the battery capacity at startup since there are functions here that require it which are
22+
# called when using the bash script to control the inverter manually
2123
self.update_battery_capacity()
2224

2325
@property

source/sems_portal_api_handler.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,12 @@ def get_battery_capacity(self) -> EnergyAmount:
376376

377377
self.log.trace(f"Retrieved data: {response}")
378378

379-
return EnergyAmount.from_kilo_watt_hours(response["data"]["info"]["battery_capacity"])
379+
try:
380+
return EnergyAmount.from_kilo_watt_hours(response["data"]["info"]["battery_capacity"])
381+
except (KeyError, TypeError):
382+
# This is not that bad as we pull the battery capacity every iteration
383+
# If this is the first time after starting we don't even use the value
384+
self.log.warning(
385+
"Unable to retrieve battery capacity from SEMSPORTAL API, using a default value", exc_info=True
386+
)
387+
return EnergyAmount.from_kilo_watt_hours(10)

0 commit comments

Comments
 (0)