Skip to main content

Lightweight Flowerhub portal client (synchronous and async)

Project description

Flowerhub Portal Python Client

CI Code Coverage Python 3.10+ License

A lightweight, Python client for the Flowerhub portal API with cookie-based JWT authentication.

Related Projects:

Features

  • Cookie-based JWT authentication with automatic token refresh
  • Async/await support via aiohttp
  • Client provides asset, consumption, and invoice data from API endpoints
  • Designed for Home Assistant integrations and similar use cases

Installation

From PyPI

pip install flowerhub-portal-api-client

From Source

git clone https://github.com/MichaelPihlblad/flowerhub-portal-api_python-lib.git
cd flowerhub-portal-api_python-lib
pip install -e ".[async]"

Quick Start

import asyncio
from flowerhub_portal_api_client import AsyncFlowerhubClient

async def main():
    async with aiohttp.ClientSession() as session:
        client = AsyncFlowerhubClient(session=session)

        # Login
        result = await client.async_login("user@example.com", "password")

        # Fetch asset information
        await client.async_readout_sequence()

        # Access asset status
        if client.flowerhub_status:
            print(f"Status: {client.flowerhub_status.status}")
            print(f"Message: {client.flowerhub_status.message}")

asyncio.run(main())

Documentation

  • Online: Library documentation — hosted on GitHub Pages, auto-published on push to main.
  • Local preview:
pip install -r dev-requirements.txt
mkdocs serve

To build the static site locally:

mkdocs build --strict

Development Setup

For contributors and local development:

# Clone and setup environment
git clone https://github.com/MichaelPihlblad/flowerhub-portal-api_python-lib.git
cd flowerhub-portal-api_python-lib
python -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt
pip install -r dev-requirements.txt

  - `result_queue` (an `asyncio.Queue`) will receive `FlowerHubStatus` objects via non-blocking `put_nowait`.
  - `interval_seconds` must be >= 5 (default 60).

Usage Guide

  • Per-call options: All public fetch methods accept raise_on_error (default True), retry_5xx_attempts (controls 5xx/429 retries; None or 0 disables), and timeout_total (per-call aiohttp.ClientTimeout override; default is 10s, 0/None disables timeout).
  • Error handling: AuthenticationError is raised after a failed refresh retry on 401; ApiError is raised for other HTTP errors or validation issues when raise_on_error=True. Use on_auth_failed and on_api_error callbacks for integration-specific handling.
  • Lifecycle: Prefer async with AsyncFlowerhubClient(...) or call close() to stop background tasks. The client does not close an injected session.
  • Concurrency / rate limiting: Call set_max_concurrency(n) to limit in-flight requests with a semaphore; pass 0/None to disable.
  • Retry/backoff: 5xx and 429 responses are retried with a small jitter; 429 honors Retry-After when provided.

Public Methods (async)

  • async_login(..)
  • async_fetch_asset_id(...)
  • async_fetch_asset(...)
  • async_readout_sequence(...) - Fetches assetID then asset info
  • async_fetch_electricity_agreement(...)
  • async_fetch_invoices(...)
  • async_fetch_consumption(...)
  • async_fetch_system_notification(...) - Unclear purpose, provides some status for flower/zavann systems, possibly for the user. Flower is electrical energy provider, Zavann seems to be the billing system.
  • start_periodic_asset_fetch(...)
  • stop_periodic_asset_fetch(...)
  • is_asset_fetch_running(...)

Data Models and Types

  • Exceptions: AuthenticationError, ApiError.
  • Status/model classes: FlowerHubStatus, ElectricityAgreement, Invoice, ConsumptionRecord, etc. (see types.py).
  • Typed results: AssetIdResult, AssetFetchResult, AgreementResult, InvoicesResult, ConsumptionResult.

Options, Callbacks, and Lifecycle

  • Callbacks: on_auth_failed (refresh failed + second 401), on_api_error (before raising ApiError when raise_on_error=True).
  • Concurrency: set_max_concurrency(n) adds a semaphore-based rate limiter; 0/None disables.
  • Context manager: async with AsyncFlowerhubClient(...) or close() to stop periodic tasks; injected sessions are not closed by the client.

Modules

  • exceptions.py: AuthenticationError, ApiError (includes status_code, url, payload).
  • types.py: dataclasses and TypedDicts used by the client (FlowerHubStatus, ElectricityAgreement, Invoice, ConsumptionRecord, AssetIdResult, AssetFetchResult, etc.).
  • parsers.py: pure helpers to parse/validate API payloads (used by the client but can be imported directly for advanced/standalone parsing).

Authentication Error Handling

The client automatically handles token refresh on 401 Unauthorized responses. If the refresh fails and the request still returns 401, an AuthenticationError is raised, indicating that the user needs to login again.

Exception-Based Handling

from flowerhub_portal_api_client import AsyncFlowerhubClient, AuthenticationError

try:
    await client.async_fetch_asset_id()
except AuthenticationError:
    # Token refresh failed, re-authentication required
    await client.async_login(username, password)
    await client.async_fetch_asset_id()

Callback-Based Handling

For Home Assistant integrations or event-driven architectures:

def on_auth_failed():
    """Called when authentication fails and re-login is needed."""
    print("Re-authentication required")
    # Trigger reauth flow, set flag, etc.

client = AsyncFlowerhubClient(
    session=session,
    on_auth_failed=on_auth_failed
)

See examples/auth_error_handling.py for complete examples including Home Assistant patterns.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flowerhub_portal_api_client-0.4.0.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flowerhub_portal_api_client-0.4.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file flowerhub_portal_api_client-0.4.0.tar.gz.

File metadata

File hashes

Hashes for flowerhub_portal_api_client-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a5e9679efa312388ef3412ccdbe91eb2f36c525f8d05671e3a52839cd26039cf
MD5 264a363ab976bc91a092934e82e9f3d8
BLAKE2b-256 41c6d9db5af404a16be1c3cc7ee8cc573bd3402b3adec64a4bf6b47ece7fefa9

See more details on using hashes here.

File details

Details for the file flowerhub_portal_api_client-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flowerhub_portal_api_client-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 defa04106b114b6fa68030c17faea334e1aec40e21d7d3b703cbb064a7575e29
MD5 0b5c28c0b626e848a87ad6aafa8a44c2
BLAKE2b-256 0815e60ad7c209bb78008d4cb701781af65d4ea848dd289894ae08a72eac4544

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page