Skip to content

Make HTTP client timeout configurable (hard-coded 5s today) #14

Description

@Viper780

Problem

opensensemap_api wraps every request in a hard-coded aiohttp.ClientTimeout(total=5) (see opensensemap_api/__init__.py, get_data()). This timeout is not configurable — neither as a constructor parameter nor as a module constant.

Impact

The openSenseMap API occasionally answers the first request after an idle period much slower than 5 s. Measured from Vienna on 2026-09-05:

warm (5 s interval):   0.10 / 0.10 / 1.76 s
after 45 s idle:       0.11 s
after 6 min idle:      0.17 s
after ~4.5 h idle:     11.15 s  (first request), 0.09–0.11 s (following)

DNS/TLS are fast (3 ms / < 50 ms), HTTP 200 on completion — this looks like a backend (MongoDB connection pool / instance cold path) issue on the openSenseMap side. But with the fixed 5 s client timeout, ANY client using this library fails on every such cold cycle: Home Assistant's opensensemap integration polling 3 stations gets all 3 requests timed out at once (Unable to fetch data from openSenseMap:), so sensor values go stale for up to a polling interval.

There is no open issue for this pattern in sensebox/openSenseMap-API yet; a draft is prepared. Even if the server side gets fixed, a configurable timeout makes the library robust against slow-first-response backends.

Suggestion

  • Add a timeout parameter to OpenSenseMap.__init__(station_id, session, timeout: aiohttp.ClientTimeout | float = 5.0) (or a module-level constant), and/or
  • Document that the timeout exists and how to raise it.

No behavior change by default (keep 5 s default to stay backward compatible).

Environment

  • opensensemap-api 0.4.1 (latest), Home Assistant Core 2026.8.3
  • Python 3.14 (HA container), aiohttp current

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions