Reauth flow and DataUpdateCoordinator

Hello,
I’m coding my first custom component and I ended up with the following situation:

  1. I have a user step that collects username and password
  2. async_setup_entry instantiates MyApi using username and password and triggers api.login()
  3. MyApi is then passed to MyCoordinator so that data can be polled from the cloud API
  4. Every 30 minutes the api fails due to a HTTP 401 error and I need to repeat the login (this currently triggers the reauth flow)

My issue is with async_step_reauth. I can see entry_data contains username and password, but I don’t know how to trigger the login() function on the instance of MyApi I initially provided to the coordinator.

Is there a way to trigger a login on the coordinator during the reauth step? Or should I rather catch the HTTP 401 inside the coordinator.fetch_data() and redo the login without even triggering the reauth flow?

Thank for any insight you may provide.

Personally I would catch the 401 inside your fetch data and not trigger a reauth as the reauth is really designed for a user to have to provide some input to the reauth - which I guess you do not want or need.

I would also say that if you get a token from your auth with an expiry, I would hold this expiry, test against it and reauth before getting the 401 as the ideal preference.

Thanks Mark, I’ll check if the auth token comes with an expiration date/time and do as you suggested!