Skip to content

Commit 93ac70e

Browse files
authored
feat: add SwitchbotAuthenticationError exception (#174)
1 parent a34072f commit 93ac70e

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

switchbot/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
7+
from .const import LockStatus, SwitchbotModel, SwitchbotAuthenticationError
88
from .devices.base_light import SwitchbotBaseLight
99
from .devices.bot import Switchbot
1010
from .devices.bulb import SwitchbotBulb
@@ -24,6 +24,7 @@
2424
"parse_advertisement_data",
2525
"GetSwitchbotDevices",
2626
"SwitchBotAdvertisement",
27+
"SwitchbotAuthenticationError",
2728
"ColorMode",
2829
"LockStatus",
2930
"SwitchbotBaseLight",

switchbot/const.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
from .enum import StrEnum
1111

1212

13+
class SwitchbotAuthenticationError(RuntimeError):
14+
"""Raised when authentication fails.
15+
16+
This exception inherits from RuntimeError to avoid breaking existing code
17+
but will be changed to Exception in a future release.
18+
"""
19+
20+
1321
class SwitchbotModel(StrEnum):
1422

1523
BOT = "WoHand"

switchbot/devices/lock.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
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
18+
from ..const import LockStatus, SwitchbotAuthenticationError
1919
from .device import SwitchbotDevice, SwitchbotOperationError
2020

2121
COMMAND_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

Comments
 (0)