1515from cryptography .hazmat .primitives .ciphers import Cipher , algorithms , modes
1616
1717from ..api_config import SWITCHBOT_APP_API_BASE_URL , SWITCHBOT_APP_COGNITO_POOL
18- from ..const import LockStatus
18+ from ..const import LockStatus , SwitchbotAuthenticationError
1919from .device import SwitchbotDevice , SwitchbotOperationError
2020
2121COMMAND_HEADER = "57"
@@ -103,16 +103,18 @@ def retrieve_encryption_key(device_mac: str, username: str, password: str):
103103 },
104104 )
105105 except cognito_idp_client .exceptions .NotAuthorizedException as err :
106- raise RuntimeError ("Failed to authenticate" ) from err
106+ raise SwitchbotAuthenticationError ("Failed to authenticate" ) from err
107107 except BaseException as err :
108- raise RuntimeError ("Unexpected error during authentication" ) from err
108+ raise SwitchbotAuthenticationError (
109+ "Unexpected error during authentication"
110+ ) from err
109111
110112 if (
111113 auth_response is None
112114 or "AuthenticationResult" not in auth_response
113115 or "AccessToken" not in auth_response ["AuthenticationResult" ]
114116 ):
115- raise RuntimeError ("Unexpected authentication response" )
117+ raise SwitchbotAuthenticationError ("Unexpected authentication response" )
116118
117119 access_token = auth_response ["AuthenticationResult" ]["AccessToken" ]
118120 key_response = requests .post (
@@ -126,7 +128,7 @@ def retrieve_encryption_key(device_mac: str, username: str, password: str):
126128 )
127129 key_response_content = json .loads (key_response .content )
128130 if key_response_content ["statusCode" ] != 100 :
129- raise RuntimeError (
131+ raise SwitchbotAuthenticationError (
130132 f"Unexpected status code returned by SwitchBot API: { key_response_content ['statusCode' ]} "
131133 )
132134
0 commit comments