Skip to content

Commit aa4b033

Browse files
authored
feat: wrap requests exceptions in SwitchbotAccountConnectionError (#175)
1 parent 271b1ad commit aa4b033

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

switchbot/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
from bleak_retry_connector import close_stale_connections, get_device
55

66
from .adv_parser import SwitchbotSupportedType, parse_advertisement_data
7-
from .const import LockStatus, SwitchbotModel, SwitchbotAuthenticationError
7+
from .const import (
8+
LockStatus,
9+
SwitchbotAccountConnectionError,
10+
SwitchbotAuthenticationError,
11+
SwitchbotModel,
12+
)
813
from .devices.base_light import SwitchbotBaseLight
914
from .devices.bot import Switchbot
1015
from .devices.bulb import SwitchbotBulb
@@ -24,6 +29,7 @@
2429
"parse_advertisement_data",
2530
"GetSwitchbotDevices",
2631
"SwitchBotAdvertisement",
32+
"SwitchbotAccountConnectionError",
2733
"SwitchbotAuthenticationError",
2834
"ColorMode",
2935
"LockStatus",

switchbot/const.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class SwitchbotAuthenticationError(RuntimeError):
1818
"""
1919

2020

21+
class SwitchbotAccountConnectionError(RuntimeError):
22+
"""Raised when connection to Switchbot account fails.
23+
24+
This exception inherits from RuntimeError to avoid breaking existing code
25+
but will be changed to Exception in a future release.
26+
"""
27+
28+
2129
class SwitchbotModel(StrEnum):
2230

2331
BOT = "WoHand"

switchbot/devices/lock.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
1616

1717
from ..api_config import SWITCHBOT_APP_API_BASE_URL, SWITCHBOT_APP_COGNITO_POOL
18-
from ..const import LockStatus, SwitchbotAuthenticationError
18+
from ..const import (
19+
LockStatus,
20+
SwitchbotAccountConnectionError,
21+
SwitchbotAuthenticationError,
22+
)
1923
from .device import SwitchbotDevice, SwitchbotOperationError
2024

2125
COMMAND_HEADER = "57"
@@ -104,7 +108,7 @@ def retrieve_encryption_key(device_mac: str, username: str, password: str):
104108
)
105109
except cognito_idp_client.exceptions.NotAuthorizedException as err:
106110
raise SwitchbotAuthenticationError("Failed to authenticate") from err
107-
except BaseException as err:
111+
except Exception as err:
108112
raise SwitchbotAuthenticationError(
109113
"Unexpected error during authentication"
110114
) from err
@@ -117,15 +121,24 @@ def retrieve_encryption_key(device_mac: str, username: str, password: str):
117121
raise SwitchbotAuthenticationError("Unexpected authentication response")
118122

119123
access_token = auth_response["AuthenticationResult"]["AccessToken"]
120-
key_response = requests.post(
121-
url=SWITCHBOT_APP_API_BASE_URL + "/developStage/keys/v1/communicate",
122-
headers={"authorization": access_token},
123-
json={
124-
"device_mac": device_mac,
125-
"keyType": "user",
126-
},
127-
timeout=10,
128-
)
124+
try:
125+
key_response = requests.post(
126+
url=SWITCHBOT_APP_API_BASE_URL + "/developStage/keys/v1/communicate",
127+
headers={"authorization": access_token},
128+
json={
129+
"device_mac": device_mac,
130+
"keyType": "user",
131+
},
132+
timeout=10,
133+
)
134+
except requests.exceptions.RequestException as err:
135+
raise SwitchbotAccountConnectionError(
136+
f"Failed to retrieve encryption key from SwitchBot Account: {err}"
137+
) from err
138+
if key_response.status_code > 299:
139+
raise SwitchbotAuthenticationError(
140+
f"Unexpected status code returned by SwitchBot Account API: {key_response.status_code}"
141+
)
129142
key_response_content = json.loads(key_response.content)
130143
if key_response_content["statusCode"] != 100:
131144
raise SwitchbotAuthenticationError(

0 commit comments

Comments
 (0)