File tree Expand file tree Collapse file tree
custom_components/bestway/aws_iot Expand file tree Collapse file tree Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff 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
378405async def test_fetch_data_raises_auth_failed_when_reauth_rejected (aws_api ):
379406 """A rejected runtime reauth starts Home Assistant's reauth handling."""
You can’t perform that action at this time.
0 commit comments