diff --git a/ariston/__init__.py b/ariston/__init__.py index 4ea7689..691a111 100644 --- a/ariston/__init__.py +++ b/ariston/__init__.py @@ -39,7 +39,9 @@ def __init__(self) -> None: self.api = None self.cloud_devices: list[dict[str, Any]] = [] - async def async_connect(self, username: str, password: str, api_url: str = ARISTON_API_URL) -> bool: + async def async_connect( + self, username: str, password: str, api_url: str = ARISTON_API_URL + ) -> bool: """Connect to the ariston cloud""" self.api = AristonAPI(username, password, api_url) return await self.api.async_connect() diff --git a/ariston/ariston_api.py b/ariston/ariston_api.py index 822f9f2..44ce323 100644 --- a/ariston/ariston_api.py +++ b/ariston/ariston_api.py @@ -435,19 +435,20 @@ def __request( method, path, params=params, json=body, headers=headers, timeout=30000 ) if not response.ok: - if response.status_code == 405: - if not is_retry: - if self.connect(): + match response.status_code: + case 405: + if not is_retry: + if self.connect(): + return self.__request(method, path, params, body, True) + raise Exception("Login failed (password changed?)") + raise Exception("Invalid token") + case 404: + return None + case _: + if not is_retry: + time.sleep(5) return self.__request(method, path, params, body, True) - raise Exception("Login failed (password changed?)") - raise Exception("Invalid token") - if response.status_code == 404: - return None - if response.status_code == 500: - if not is_retry: - time.sleep(1) - return self.__request(method, path, params, body, True) - raise Exception(response.status_code) + raise Exception(response.status_code) if len(response.content) > 0: json = response.json() @@ -849,23 +850,24 @@ async def __async_request( ) if not response.ok: - if response.status == 405: - if not is_retry: - if await self.async_connect(): + match response.status: + case 405: + if not is_retry: + if await self.async_connect(): + return await self.__async_request( + method, path, params, body, True + ) + raise Exception("Login failed (password changed?)") + raise Exception("Invalid token") + case 404: + return None + case _: + if not is_retry: + await asyncio.sleep(5) return await self.__async_request( method, path, params, body, True ) - raise Exception("Login failed (password changed?)") - raise Exception("Invalid token") - if response.status == 404: - return None - if response.status == 500: - if not is_retry: - await asyncio.sleep(1) - return await self.__async_request( - method, path, params, body, True - ) - raise Exception(response.status) + raise Exception(response.status) if response.content_length and response.content_length > 0: json = await response.json() diff --git a/ariston/device.py b/ariston/device.py index 5b73337..773b616 100644 --- a/ariston/device.py +++ b/ariston/device.py @@ -364,7 +364,7 @@ def _set_energy_features(self): def are_device_features_available( self, - device_features: Optional[list[DeviceFeatures or CustomDeviceFeatures or DeviceAttribute]], + device_features: Optional[list[DeviceFeatures | CustomDeviceFeatures | DeviceAttribute]], system_types: Optional[list[SystemType]], whe_types: Optional[list[WheType]], ) -> bool: