Skip to content

Commit

Permalink
Merge pull request #126 from fbjerggaard/fix-125
Browse files Browse the repository at this point in the history
Handle API errors gracefully
  • Loading branch information
nickknissen authored Oct 21, 2024
2 parents 5a0921d + ae63998 commit f986983
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions custom_components/monta/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ async def async_get_charges(self, charge_point_id: int) -> any:
headers={"authorization": f"Bearer {access_token}"},
)

return sorted(response["data"], key=lambda charge: -charge["id"])
charges = response.get("data")

if charges is None:
_LOGGER.warning("No charges found in response!")
charges = []

return sorted(charges, key=lambda charge: -charge["id"])

async def async_start_charge(self, charge_point_id: int) -> any:
"""Start a charge."""
Expand Down Expand Up @@ -177,7 +183,13 @@ async def async_get_wallet_transactions(self) -> any:
headers={"authorization": f"Bearer {access_token}"},
)

return sorted(response["data"], key=lambda transaction: -transaction["id"])
transactions = response.get("data")

if transactions is None:
_LOGGER.warning("No transactions found in response!")
transactions = []

return sorted(transactions, key=lambda transaction: -transaction["id"])

async def async_get_access_token(self) -> str:
"""Get access token."""
Expand Down

0 comments on commit f986983

Please sign in to comment.