Skip to content

Commit

Permalink
Merge pull request #15 from Taraman17/dev
Browse files Browse the repository at this point in the history
fix: Unexpected exception when login is incorrect.
  • Loading branch information
Taraman17 authored Dec 3, 2024
2 parents a711e2b + 473866b commit 3e60405
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions pyHomee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,25 @@ async def get_access_token(self):
)
except aiohttp.client_exceptions.ClientError as e:
await client.close()
raise AuthenticationFailedException from e
raise HomeeAuthFailedException("Could not connect to Homee") from e

try:
req_text = await req.text()
regex = r"^access_token=([0-z]+)&.*&expires=(\d+)$"
matches = re.match(regex, req_text)
if req.status == 200:
regex = r"^access_token=([0-z]+)&.*&expires=(\d+)$"
matches = re.match(regex, req_text)

self.token = matches[1]
self.expires = datetime.now().timestamp() + int(matches[2])
self.token = matches[1]
self.expires = datetime.now().timestamp() + int(matches[2])

self.retries = 0
self.retries = 0
else:
await client.close()
raise HomeeAuthFailedException(f"Did not get a valid token: {req.reason}")

except aiohttp.client_exceptions.ClientError as e:
await client.close()
raise AuthenticationFailedException from e
raise HomeeAuthFailedException(f"Client error: {e.__cause__}") from e

await client.close()
return self.token
Expand Down Expand Up @@ -143,7 +147,7 @@ async def run(self):

try:
await self.get_access_token()
except AuthenticationFailedException:
except HomeeAuthFailedException("Could not get access token."):
# Reconnect
self.retries += 1
continue
Expand Down Expand Up @@ -575,6 +579,10 @@ async def on_attribute_updated(self, attribute_data: dict, node: HomeeNode):
class HomeeException(Exception):
"""Base class for all errors thrown by this library."""

class HomeeConnectionFailedException(HomeeException):
"""Raised if connection can not be established."""

class AuthenticationFailedException(HomeeException):
class HomeeAuthFailedException(HomeeException):
"""Raised if no valid access token could be acquired."""
def __init__(self, reason):
self.reason = reason
2 changes: 1 addition & 1 deletion pyHomee/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ def watchdog_sms_notifications(self) -> bool:
@property
def devices(self) -> list[HomeeDevice]:
"""Return the list of devices associated with the user."""
return self._devices
return self._data["devices"]

def set_data(self, data: str) -> None:
"""Update data of the user"""
Expand Down

0 comments on commit 3e60405

Please sign in to comment.