Skip to content

Fix device_login failing when login returns AuthenticationResult directly#111

Merged
KJonline merged 1 commit intoPyhass:devfrom
juliangall:fix/device-login-auth-result
Feb 20, 2026
Merged

Fix device_login failing when login returns AuthenticationResult directly#111
KJonline merged 1 commit intoPyhass:devfrom
juliangall:fix/device-login-auth-result

Conversation

@juliangall
Copy link
Copy Markdown
Contributor

Problem

When the stored refresh token expires or becomes invalid, hiveRefreshTokens
falls back to deviceLogin. This calls login() internally, which can return
an AuthenticationResult directly (no further challenge) when the Cognito user
pool does not enforce device tracking. The previous code had no handler for this
case, causing it to fall through to else: raise HiveInvalidDeviceAuthentication,
which prevents the integration from loading with a misleading error.

Specifically, when I tried to add the Hive integration, it got past the entry of username and password and said the integration had been added. However, it showed in the web page tile as "failed to set up", with an error in the log: "Invalid Refresh Token".

Fix

  • In device_login() (both async and sync), return immediately if login()
    already contains AuthenticationResult — the tokens are valid, no device
    challenge is needed.

  • Also handle the SMS_MFA challenge case by raising HiveReauthRequired
    instead of HiveInvalidDeviceAuthentication, so Home Assistant can trigger
    a proper re-auth flow for accounts with 2FA enabled.

Tested

Verified against a live Hive account where the integration previously failed
to set up with HiveInvalidDeviceAuthentication on every restart.

…ctly

When hiveRefreshTokens falls back to deviceLogin due to an expired/invalid
refresh token, device_login calls login() which can return an AuthenticationResult
directly (no device challenge) when device tracking is not enforced by the
Cognito user pool. Previously this case fell through to the else branch and
raised HiveInvalidDeviceAuthentication, preventing the integration from loading.

Also handle SMS_MFA challenge in device_login by raising HiveReauthRequired
so Home Assistant can trigger a proper re-auth flow for accounts with 2FA enabled.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@KJonline KJonline changed the base branch from master to dev February 20, 2026 15:32
@KJonline KJonline merged commit a4445fb into Pyhass:dev Feb 20, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants