Skip to content

Commit e6222fc

Browse files
hugo-britoCopilot
andcommitted
Handle partial V02 authentication failures
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent ec9ced0 commit e6222fc

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

custom_components/bestway/aws_iot/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ async def fetch_data(self) -> Any: # Returns BestwayApiResults
684684

685685
refreshed, auth_failed = await self._poll_all_devices()
686686

687-
if self.devices and refreshed == 0 and auth_failed:
687+
if self.devices and auth_failed:
688688
_LOGGER.info("Re-authenticating after auth failure during poll")
689689
try:
690690
token = await self.authenticate(

tests/test_aws_iot_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,33 @@ async def test_fetch_data_reauthenticates_and_propagates_token(aws_api):
374374
token_updated.assert_called_once_with("fresh_token")
375375

376376

377+
@pytest.mark.asyncio
378+
async def test_fetch_data_reauthenticates_after_partial_auth_failure(aws_api):
379+
"""Any auth rejection refreshes the shared account token."""
380+
aws_api.devices = {
381+
"device1": _make_aws_device("device1"),
382+
"device2": _make_aws_device("device2"),
383+
}
384+
shadow = {"code": 0, "data": {"state": {"reported": {"power_state": 1}}}}
385+
aws_api._do_post = AsyncMock(
386+
side_effect=[
387+
shadow,
388+
AwsIotAuthException("expired"),
389+
shadow,
390+
shadow,
391+
]
392+
)
393+
394+
with patch.object(
395+
AwsIotApi, "authenticate", new=AsyncMock(return_value="fresh_token")
396+
) as authenticate:
397+
results = await aws_api.fetch_data()
398+
399+
authenticate.assert_awaited_once()
400+
assert set(results.devices) == {"device1", "device2"}
401+
assert aws_api._do_post.await_count == 4
402+
403+
377404
@pytest.mark.asyncio
378405
async def test_fetch_data_raises_auth_failed_when_reauth_rejected(aws_api):
379406
"""A rejected runtime reauth starts Home Assistant's reauth handling."""

0 commit comments

Comments
 (0)