Skip to content

Authentication Token Expiration Causes Data Loss #32

@Feriman22

Description

@Feriman22

Bug Report: Authentication Token Expiration Causes Data Loss

Description

The Beszel Home Assistant integration stops receiving system data after the initial authentication token expires. Instead of failing with an error, the PocketBase API (used by Beszel) returns an empty list of systems when a request is made with an expired token. This leads to the integration reporting "No systems found in Beszel API" and all sensors becoming unavailable or showing no data.

Root Cause Analysis

The issue is located in the _ensure_client method within api.py. The current implementation only performs authentication when the PocketBase client object is first initialized (if self._client is None).

    def _ensure_client(self):
        """Initialize the PocketBase client if not already done"""
        if self._client is None:
            try:
                self._client = PocketBase(self._url, verify=self._verify_ssl)
                if self._username and self._password:
                    self._client.collection("users").auth_with_password(
                        self._username,
                        self._password,
                    )
            except Exception as e:
                LOGGER.error(f"Failed to initialize PocketBase client: {e}")
                raise

Since the client object is persistent throughout the lifetime of the Home Assistant process, it never re-authenticates once the initial token expires.

Steps to Reproduce

  1. Install and configure the Beszel API integration.
  2. Wait for the authentication token to expire.
  3. Observe that all sensors lose their data and Home Assistant logs show:
    WARNING (MainThread) [custom_components.beszel_api] No systems found in Beszel API

Expected Behavior

The integration should check if the authentication session is still valid before making requests and re-authenticate if necessary.

Proposed Fix

Modify the _ensure_client method in custom_components/beszel_api/api.py to check the is_valid property of the auth_store.

    def _ensure_client(self):
        """Initialize or re-authenticate the PocketBase client if session is invalid"""
        if self._client is None:
            self._client = PocketBase(self._url, verify=self._verify_ssl)

        if not self._client.auth_store.is_valid:
            try:
                if self._username and self._password:
                    self._client.collection("users").auth_with_password(
                        self._username,
                        self._password,
                    )
                    LOGGER.info("Re-authenticated with Beszel Hub")
            except Exception as e:
                LOGGER.error(f"Failed to authenticate with Beszel Hub: {e}")
                raise

Environment

  • Home Assistant Core: 2026.5.4
  • Beszel HA Integration: 1.0.1
  • python-pocketbase: 0.17.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions