Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local support for Somfy Developer Mode #441

Merged
merged 13 commits into from
Apr 11, 2022
Prev Previous commit
Next Next commit
Add Unknown Object exception
  • Loading branch information
iMicknl authored Apr 11, 2022
commit 9031763d712ce7abbcb082ae0caa9c5ee0f5b8c5
31 changes: 26 additions & 5 deletions pyoverkiz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
TooManyExecutionsException,
TooManyRequestsException,
UnknownUserException,
UnknownObjectException,
)
from pyoverkiz.models import (
Command,
Expand Down Expand Up @@ -137,6 +138,7 @@ def __init__(
cafile=os.path.dirname(os.path.realpath(__file__))
+ "/overkiz-root-ca-2048.crt"
)
self._ssl_context = None
Copy link
Owner Author

@iMicknl iMicknl Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSL check removed for now. We can just pass a session that does not verify SSL.


async def __aenter__(self) -> OverkizClient:
return self
Expand Down Expand Up @@ -165,10 +167,13 @@ async def login(
Caller must provide one of [userId+userPassword, userId+ssoToken, accessToken, jwt]
"""
# Local authentication
# TODO check which endpoint can be used to validate the token!
if self._is_local:
if register_event_listener:
await self.register_event_listener()
else:
# Call a simple endpoint to verify if our token is correct
await self.get_api_version()

return True

# Somfy TaHoma authentication using access_token
Expand Down Expand Up @@ -594,6 +599,15 @@ async def get_current_executions(self) -> list[Execution]:

return executions

@backoff.on_exception(
backoff.expo, NotAuthenticatedException, max_tries=2, on_backoff=relogin
)
async def get_api_version(self) -> str:
"""Get the API version (local only)"""
response = await self.__get("apiVersion")

return cast(str, response["protocolVersion"])

@backoff.on_exception(backoff.expo, TooManyExecutionsException, max_tries=10)
@backoff.on_exception(
backoff.expo, NotAuthenticatedException, max_tries=2, on_backoff=relogin
Expand Down Expand Up @@ -670,7 +684,7 @@ async def generate_local_token(self, gateway_id: str) -> str:
Generates a new token
Access scope : Full enduser API access (enduser/*)
"""
response = await self.__get(f"/config/{gateway_id}/local/tokens/generate")
response = await self.__get(f"config/{gateway_id}/local/tokens/generate")

return cast(str, response["token"])

Expand All @@ -688,7 +702,7 @@ async def activate_local_token(
Access scope : Full enduser API access (enduser/*)
"""
response = await self.__post(
f"/config/{gateway_id}/local/tokens",
f"config/{gateway_id}/local/tokens",
{"label": label, "token": token, "scope": scope},
)

Expand All @@ -707,7 +721,7 @@ async def get_local_tokens(
Get all gateway tokens with the given scope
Access scope : Full enduser API access (enduser/*)
"""
response = await self.__get(f"/config/{gateway_id}/local/tokens/{scope}")
response = await self.__get(f"config/{gateway_id}/local/tokens/{scope}")
local_tokens = [LocalToken(**lt) for lt in humps.decamelize(response)]

return local_tokens
Expand All @@ -723,7 +737,7 @@ async def delete_local_token(self, gateway_id: str, uuid: str) -> bool:
Delete a token
Access scope : Full enduser API access (enduser/*)
"""
await self.__delete(f"/config/{gateway_id}/local/tokens/{uuid}")
await self.__delete(f"config/{gateway_id}/local/tokens/{uuid}")

return True

Expand Down Expand Up @@ -853,8 +867,15 @@ async def check_response(response: ClientResponse) -> None:
if "Not such token with UUID: " in message:
raise NotSuchTokenException(message)


<< << << < HEAD
if "Unknown user :" in message:
raise UnknownUserException(message)
== == == =
# {"error":"Unknown object.","errorCode":"UNSPECIFIED_ERROR"}
if message == "Unknown object.":
raise UnknownObjectException(message)
>>>>>> > 562d327(Add Unknown Object exception)

raise Exception(message if message else result)

Expand Down
4 changes: 4 additions & 0 deletions pyoverkiz/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class UnknownUserException(BaseOverkizException):
pass


class UnknownObjectException(BaseOverkizException):
pass


# Nexity
class NexityBadCredentialsException(BadCredentialsException):
pass
Expand Down