From af3b6dbd371e18e5260c5bd8cc9e339182b7171e Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Wed, 20 Oct 2021 06:33:01 -0700 Subject: [PATCH] Use OverkizServer class instead of API_URL (#201) * Use OverkizServer type * Fix test * Fix test2 * Bugfixes --- README.md | 16 ++-------------- pyhoma/client.py | 25 ++++++++++++------------- tests/test_client.py | 3 ++- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 17875824..e8698485 100644 --- a/README.md +++ b/README.md @@ -23,26 +23,14 @@ pip install pyhoma import asyncio import time +from pyhoma.const import SUPPORTED_SERVERS from pyhoma.client import TahomaClient -SUPPORTED_ENDPOINTS = { - "Cozytouch": "https://ha110-1.overkiz.com/enduser-mobile-web/enduserAPI/", - "eedomus": "https://ha101-1.overkiz.com/enduser-mobile-web/enduserAPI/", - "Hi Kumo": "https://ha117-1.overkiz.com/enduser-mobile-web/enduserAPI/", - "Rexel Energeasy Connect": "https://ha112-1.overkiz.com/enduser-mobile-web/enduserAPI/", - "Somfy Connexoon IO": "https://tahomalink.com/enduser-mobile-web/enduserAPI/", - "Somfy Connexoon RTS": "https://ha201-1.overkiz.com/enduser-mobile-web/enduserAPI/", - "Somfy TaHoma": "https://tahomalink.com/enduser-mobile-web/enduserAPI/", - "Somfy (Australia)": "https://ha201-1.overkiz.com/enduser-mobile-web/enduserAPI/", - "Somfy (Europe)": "https://tahomalink.com/enduser-mobile-web/enduserAPI/", - "Somfy (North America)": "https://ha401-1.overkiz.com/enduser-mobile-web/enduserAPI/", -} - USERNAME = "" PASSWORD = "" async def main() -> None: - async with TahomaClient(USERNAME, PASSWORD, api_url=SUPPORTED_ENDPOINTS["Somfy (Europe)"]) as client: + async with TahomaClient(USERNAME, PASSWORD, server=SUPPORTED_SERVERS["somfy_europe"]) as client: try: await client.login() except Exception as exception: # pylint: disable=broad-except diff --git a/pyhoma/client.py b/pyhoma/client.py index 0f7e34ba..177232a4 100644 --- a/pyhoma/client.py +++ b/pyhoma/client.py @@ -43,6 +43,7 @@ Execution, Gateway, HistoryExecution, + OverkizServer, Place, Scenario, State, @@ -50,8 +51,6 @@ JSON = Union[Dict[str, Any], List[Dict[str, Any]]] -DEFAULT_SERVER = SUPPORTED_SERVERS["somfy_europe"] - async def relogin(invocation: dict[str, Any]) -> None: await invocation["args"][0].login() @@ -68,7 +67,7 @@ def __init__( self, username: str, password: str, - api_url: str = DEFAULT_SERVER.endpoint, + server: OverkizServer, session: ClientSession = None, ) -> None: """ @@ -76,13 +75,13 @@ def __init__( :param username: the username for Tahomalink.com :param password: the password for Tahomalink.com - :param api_url: optional custom api url + :param server: OverkizServer :param session: optional ClientSession """ self.username = username self.password = password - self.api_url = api_url + self.server = server self.devices: list[Device] = [] self.gateways: list[Gateway] = [] @@ -118,12 +117,12 @@ async def login( """ # CozyTouch authentication using jwt - if self.api_url == SUPPORTED_SERVERS["atlantic_cozytouch"].endpoint: + if self.server == SUPPORTED_SERVERS["atlantic_cozytouch"]: jwt = await self.cozytouch_login() payload = {"jwt": jwt} # Nexity authentication using ssoToken - elif self.api_url == SUPPORTED_SERVERS["nexity"].endpoint: + elif self.server == SUPPORTED_SERVERS["nexity"]: sso_token = await self.nexity_login() user_id = self.username.replace("@", "_-_") # Replace @ for _-_ payload = {"ssoToken": sso_token, "userId": user_id} @@ -441,27 +440,27 @@ async def execute_scenario(self, oid: str) -> str: response = await self.__post(f"exec/{oid}") return response["execId"] - async def __get(self, endpoint: str) -> Any: + async def __get(self, path: str) -> Any: """Make a GET request to the TaHoma API""" - async with self.session.get(f"{self.api_url}{endpoint}") as response: + async with self.session.get(f"{self.server.endpoint}{path}") as response: await self.check_response(response) return await response.json() async def __post( - self, endpoint: str, payload: JSON | None = None, data: JSON | None = None + self, path: str, payload: JSON | None = None, data: JSON | None = None ) -> Any: """Make a POST request to the TaHoma API""" async with self.session.post( - f"{self.api_url}{endpoint}", + f"{self.server.endpoint}{path}", data=data, json=payload, ) as response: await self.check_response(response) return await response.json() - async def __delete(self, endpoint: str) -> None: + async def __delete(self, path: str) -> None: """Make a DELETE request to the TaHoma API""" - async with self.session.delete(f"{self.api_url}{endpoint}") as response: + async with self.session.delete(f"{self.server.endpoint}{path}") as response: await self.check_response(response) @staticmethod diff --git a/tests/test_client.py b/tests/test_client.py index 2e32f7a7..69cd8cdc 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -7,6 +7,7 @@ from pytest import fixture from pyhoma.client import TahomaClient +from pyhoma.const import SUPPORTED_SERVERS CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -14,7 +15,7 @@ class TestTahomaClient: @fixture def api(self): - return TahomaClient("username", "password") + return TahomaClient("username", "password", SUPPORTED_SERVERS["somfy_europe"]) @pytest.mark.asyncio async def test_get_devices_basic(self, api):