Skip to content

Commit d095fa2

Browse files
committed
Raise InvalidAuthError for 401/403 responses (fixes YeomansIII/ha-dominion-energy#16)
_handle_response() was raising generic ApiError for all 4xx status codes, including 401/403. Downstream consumers (ha-dominion-energy) could not distinguish auth failures from transient API errors, preventing the Home Assistant reauth flow from triggering.
1 parent d4f1eec commit d095fa2

5 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎dompower/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
UsageResolution,
6363
)
6464

65-
__version__ = "0.2.1"
65+
__version__ = "0.2.2"
6666

6767
__all__ = [
6868
"LOGIN_URL",

‎dompower/client.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ async def _handle_response(
268268
retry_after=int(retry_after) if retry_after else None,
269269
)
270270

271+
if resp.status in (401, 403):
272+
text = await resp.text()
273+
raise InvalidAuthError(
274+
f"Authentication failed: {resp.status}",
275+
)
276+
271277
if resp.status >= 400:
272278
text = await resp.text()
273279
raise ApiError(

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "dompower"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "Async Python client for Dominion Energy API"
55
readme = "README.md"
66
license = { text = "MIT" }

‎tests/test_client.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,12 @@ async def test_4xx_raises_api_error(
558558
self,
559559
aresponses: ResponsesMockServer,
560560
) -> None:
561-
"""4xx responses raise ApiError with status code."""
561+
"""Non-auth 4xx responses raise ApiError with status code."""
562562
aresponses.add(
563563
"prodsvc-dominioncip.smartcmobile.com",
564564
"/Service/api/1/test",
565565
"GET",
566-
response=aresponses.Response(status=403, body="Forbidden"),
566+
response=aresponses.Response(status=404, body="Not Found"),
567567
)
568568

569569
async with ClientSession() as session:
@@ -577,7 +577,33 @@ async def test_4xx_raises_api_error(
577577
with pytest.raises(ApiError) as exc_info:
578578
await client._async_request("GET", "/Service/api/1/test")
579579

580-
assert exc_info.value.status_code == 403
580+
assert exc_info.value.status_code == 404
581+
582+
async def test_401_403_raises_invalid_auth_error(
583+
self,
584+
aresponses: ResponsesMockServer,
585+
) -> None:
586+
"""401/403 responses after token refresh raise InvalidAuthError."""
587+
# 401 triggers a token refresh attempt, which also fails,
588+
# then the retry response (also 401) hits _handle_response
589+
# which should raise InvalidAuthError
590+
aresponses.add(
591+
"prodsvc-dominioncip.smartcmobile.com",
592+
"/Service/api/1/test",
593+
"GET",
594+
response=aresponses.Response(status=403, body="Forbidden"),
595+
)
596+
597+
async with ClientSession() as session:
598+
client = DompowerClient(
599+
session,
600+
access_token="tok", # noqa: S106
601+
refresh_token="ref", # noqa: S106
602+
)
603+
client._token_manager._token_expires_at = datetime(2099, 1, 1, tzinfo=UTC)
604+
605+
with pytest.raises(InvalidAuthError):
606+
await client._async_request("GET", "/Service/api/1/test")
581607

582608
async def test_api_level_error_in_body(
583609
self,

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)