Skip to content

Commit

Permalink
Fix error response detection
Browse files Browse the repository at this point in the history
The check for `is dict` we were trying to use previously was wrong,
and would have needed to be `type(data) is dict`. But that is not
the recommended way to check for a dictionary anyway, so this fixes
it to use `isinstance()` instead.

This also adds a check for a different way that TeslaFi can report
an error: specifically, if the API key is not recognized, the response
is completely different compared to when it is recognized but the
API has been disabled; or the particular feature of the API is not
enabled (such as unlocking). We were already checking for the latter,
but not the former, so the error in this case is confusing.
  • Loading branch information
jhansche committed Nov 25, 2023
1 parent 65d8f38 commit ecbfc2d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions custom_components/teslafi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ async def _request(self, command: str = "", **kwargs) -> dict:
_LOGGER.warning("Error reading as json: %s", response.text, exc_info=True)
raise SyntaxError("Failed parsing JSON") from exc

if data is dict and (err := data.get("error")):
raise RuntimeError(f"{err}: {data.get('error_description')}")
if isinstance(data, dict):
if err := data.get("error"):
raise RuntimeError(f"{err}: {data.get('error_description')}")
if data.get("response", {}).get("result", None) == "unauthorized":
raise PermissionError(
f"TeslaFi response unauthorized for api key {self._api_key}: {data}"
)

return data

0 comments on commit ecbfc2d

Please sign in to comment.