Skip to content

Commit a7b4640

Browse files
committed
bugfix, refactor exception handling/messages
1 parent 7a36a12 commit a7b4640

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

src/evohomeasync/auth.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async def fetch_session_id(self) -> None:
123123
if "EmailOrPasswordIncorrect" not in err.message:
124124
raise
125125
self.logger.error(HINT_BAD_CREDS) # noqa: TRY400
126-
raise exc.BadUserCredentialsError(f"{err}", status=err.status) from err
126+
raise exc.BadUserCredentialsError(f"{err!r}", status=err.status) from err
127127

128128
self._was_authenticated = True
129129

@@ -163,7 +163,7 @@ async def _fetch_session_id(self, credentials: dict[str, str]) -> None:
163163

164164
except (KeyError, TypeError) as err:
165165
raise exc.AuthenticationFailedError(
166-
f"Authenticator response is invalid: {err}, payload={response}"
166+
f"Authenticator response is invalid: {err!r}, payload={response}"
167167
) from err
168168

169169
self._was_authenticated = True # i.e. the credentials are valid

src/evohomeasync/main.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,21 @@ async def _get_config(self) -> list[EvoTcsInfoDictT]:
124124
self._session_manager._clear_session_id()
125125
self._user_info = await self.auth.get(url, schema=SCH_GET_ACCOUNT_INFO) # type: ignore[assignment]
126126

127-
assert self._user_info is not None # mypy
127+
assert self._user_info is not None # mypy
128128

129129
if self._user_locs is None:
130-
url = f"locations?userId={self._user_info['user_id']}&allData=True"
131-
self._user_locs = await self.auth.get(url, schema=SCH_GET_ACCOUNT_LOCS) # type: ignore[assignment]
130+
try:
131+
user_id = self._user_info["user_id"]
132+
except (KeyError, TypeError) as err:
133+
raise exc.BadApiResponseError(
134+
f"User info is invalid: {err!r}, user_info={self._user_info}"
135+
) from err
132136

133-
assert self._user_locs is not None # mypy
137+
self._user_locs = await self.auth.get(
138+
f"locations?userId={user_id}&allData=True", schema=SCH_GET_ACCOUNT_LOCS
139+
) # type: ignore[assignment]
140+
141+
assert self._user_locs is not None # mypy
134142

135143
if self._locations is None:
136144
self._locations = []
@@ -152,7 +160,7 @@ def user_account(self) -> EvoUserAccountDictT:
152160

153161
if self._user_info is None:
154162
raise exc.InvalidConfigError(
155-
f"{self}: The account information is not (yet) available"
163+
"The account information is not (yet) available"
156164
)
157165

158166
return self._user_info
@@ -163,7 +171,7 @@ def locations(self) -> list[Location]:
163171

164172
if self._user_locs is None:
165173
raise exc.InvalidConfigError(
166-
f"{self}: The installation information is not (yet) available"
174+
"The installation information is not (yet) available"
167175
)
168176

169177
return self._locations # type: ignore[return-value]
@@ -174,7 +182,7 @@ def location_by_id(self) -> dict[str, Location]:
174182

175183
if self._user_locs is None:
176184
raise exc.InvalidConfigError(
177-
f"{self}: The installation information is not (yet) available"
185+
"The installation information is not (yet) available"
178186
)
179187

180188
return self._location_by_id # type: ignore[return-value]
@@ -189,7 +197,7 @@ def tcs(self) -> Location:
189197

190198
if not (locs := self.locations) or len(locs) != 1:
191199
raise exc.NoSingleTcsError(
192-
f"{self}: There is not a single location (only) for this account"
200+
"There is not a single location (only) for this account"
193201
)
194202

195203
return locs[0]

src/evohomeasync2/auth.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ async def _fetch_access_token(self, credentials: dict[str, str]) -> None:
211211

212212
except (KeyError, TypeError) as err:
213213
raise exc.AuthenticationFailedError(
214-
f"Authenticator response is invalid: {err}, payload={response}"
214+
f"Authenticator response is invalid: {err!r}, payload={response}"
215215
) from err
216216

217217
self._was_authenticated = True # i.e. the credentials are valid

src/evohomeasync2/main.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,22 @@ async def _get_config(
134134
self._token_manager._clear_access_token()
135135
self._user_info = await self.auth.get(url, schema=SCH_USER_ACCOUNT) # type: ignore[assignment]
136136

137-
assert self._user_info is not None # mypy
137+
assert self._user_info is not None # mypy
138138

139139
if self._user_locs is None:
140-
url = f"location/installationInfo?userId={self._user_info[SZ_USER_ID]}&includeTemperatureControlSystems=True"
141-
self._user_locs = await self.auth.get(url, schema=SCH_USER_LOCATIONS) # type: ignore[assignment]
140+
try:
141+
user_id = self._user_info[SZ_USER_ID]
142+
except (KeyError, TypeError) as err:
143+
raise exc.BadApiResponseError(
144+
f"User info is invalid: {err!r}, user_info={self._user_info}"
145+
) from err
142146

143-
assert self._user_locs is not None # mypy
147+
self._user_locs = await self.auth.get(
148+
f"location/installationInfo?userId={user_id}&includeTemperatureControlSystems=True",
149+
schema=SCH_USER_LOCATIONS,
150+
) # type: ignore[assignment]
151+
152+
assert self._user_locs is not None # mypy
144153

145154
if self._locations is None:
146155
self._locations = []
@@ -166,7 +175,7 @@ def user_account(self) -> EvoUsrConfigResponseT:
166175

167176
if not self._user_info:
168177
raise exc.InvalidConfigError(
169-
f"{self}: The account information is not (yet) available"
178+
"The account information is not (yet) available"
170179
)
171180

172181
return self._user_info
@@ -177,7 +186,7 @@ def locations(self) -> list[Location]:
177186

178187
if not self._user_locs:
179188
raise exc.InvalidConfigError(
180-
f"{self}: The installation information is not (yet) available"
189+
"The installation information is not (yet) available"
181190
)
182191

183192
return self._locations # type: ignore[return-value]
@@ -188,7 +197,7 @@ def location_by_id(self) -> dict[str, Location]:
188197

189198
if not self._user_locs:
190199
raise exc.InvalidConfigError(
191-
f"{self}: The installation information is not (yet) available"
200+
"The installation information is not (yet) available"
192201
)
193202

194203
return self._location_by_id # type: ignore[return-value]
@@ -203,17 +212,17 @@ def tcs(self) -> ControlSystem:
203212

204213
if not (locs := self.locations) or len(locs) != 1:
205214
raise exc.NoSingleTcsError(
206-
f"{self}: There is not a single location (only) for this account"
215+
"There is not a single location (only) for this account"
207216
)
208217

209218
if not (gwys := locs[0].gateways) or len(gwys) != 1:
210219
raise exc.NoSingleTcsError(
211-
f"{self}: There is not a single gateway (only) for this account/location"
220+
"There is not a single gateway (only) for this account/location"
212221
)
213222

214223
if not (tcss := gwys[0].systems) or len(tcss) != 1:
215224
raise exc.NoSingleTcsError(
216-
f"{self}: There is not a single TCS (only) for this account/location/gateway"
225+
"There is not a single TCS (only) for this account/location/gateway"
217226
)
218227

219228
return tcss[0]

0 commit comments

Comments
 (0)