Fix device_login failing when login returns AuthenticationResult directly#111
Merged
KJonline merged 1 commit intoPyhass:devfrom Feb 20, 2026
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the stored refresh token expires or becomes invalid,
hiveRefreshTokensfalls back to
deviceLogin. This callslogin()internally, which can returnan
AuthenticationResultdirectly (no further challenge) when the Cognito userpool 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 iflogin()already contains
AuthenticationResult— the tokens are valid, no devicechallenge is needed.
Also handle the
SMS_MFAchallenge case by raisingHiveReauthRequiredinstead of
HiveInvalidDeviceAuthentication, so Home Assistant can triggera 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
HiveInvalidDeviceAuthenticationon every restart.