Skip to content

startSession always fails on load due to tokenCreated defaulting to datetime.min #123

Description

@markybry

Summary

The Hive integration fails to initialise on every HA startup and first setup due to two related bugs. The integration shows "Initialising" indefinitely before failing with HiveRefreshTokenExpired.


Bug 1 — tokenCreated defaults to datetime.min when loading stored tokens

When startSession loads tokens from the HA config entry it calls:

await self.updateTokens(config["tokens"], False)

With update_expiry_time=False, tokenCreated is never set and stays at datetime.min (0001-01-01). The expiry check then always evaluates as expired:

Current : 2026-05-04 19:30:37
Expiry  : 0001-01-01 00:54:00  ← always in the past

This happens even when tokens were issued seconds earlier during the config flow.


Bug 2 — force_refresh=True after _retryLogin hangs indefinitely

After the fallback login succeeds and fresh tokens are obtained, _retryLogin immediately calls:

await self.hiveRefreshTokens(force_refresh=True)

Attempting a Cognito token refresh on a brand new refresh token either:

  • Hangs until boto3 timeout (~5 minutes in v1.0.8)
  • Fails with HiveRefreshTokenExpired causing setup cancellation

There is no reason to force refresh tokens that were just issued.


Observed log sequence

tokenCreated: 0001-01-01 00:00:00 | tokenExpiry: 1:00:00
Session token expiry time (Current: 2026-05-04 19:30:37 | Expiry: 0001-01-01 00:54:00)
WARNING: Refresh token is invalid or expired.
WARNING: Session Token refresh failed (HiveRefreshTokenExpired), falling back to login.
... login succeeds, fresh tokens obtained ...
tokenCreated: 2026-05-04 19:30:37
[hangs here on force_refresh=True]
ERROR: Setup of config entry cancelled

Impact

Affects all users on first setup or after any HA restart.

Version Behaviour
1.0.8 Falls back to _retryDeviceLogin → hangs ~5 min → fails
1.0.9 Falls back to _retryLogin → login succeeds → hangs on force refresh

Fix

Option 1 (recommended): Remove the redundant force refresh after a successful login

# _retryLogin — remove this line, tokens from fresh login are already valid
await self.hiveRefreshTokens(force_refresh=True)

Option 2: Store tokenCreated in the config entry so tokens survive HA restarts without appearing immediately expired


Environment

  • pyhive-integration 1.0.8 and 1.0.9
  • Home Assistant (all recent versions)
  • Python 3.14

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions