Api Wrapper for BlueAir API using async in python. This was inspired by this guide to be a lightweight wrapper with simple error handling.
a lot of this is based on hass-blueair.
- REST API — async device discovery, sensor polling, and control commands for both legacy and AWS-based Blueair devices.
- MQTT real-time updates — receive sensor data and device state changes via AWS IoT WebSocket with sub-second latency instead of 5-minute polling.
- Automatic reconnect with credential refresh — MQTT tokens expire after 24 hours; the client refreshes credentials and reconnects with exponential backoff on unexpected disconnects.
- Split AWS regions when needed — keep the normal single
regionsetting for most accounts, or configure separate Gigya account and BlueCloud device-control regions for accounts whose devices are hosted elsewhere. See AWS account and BlueCloud regions. - Data-driven SKU mapping — resolves device SKUs to human-readable product names via a built-in lookup table.
- Per-device hardware detection —
mood_brightness_maxandfan_speed_countadapt automatically based on the device's hardware identifier.
-
Shared aiohttp sessions — pass
client_sessionto reuse an existing session, such as Home Assistant's.cleanup_client_session()only closes a session the client created itself; a session you pass in is left open for its owner to close. -
Re-authentication — concurrent requests share one login. The BlueCloud access token is refreshed shortly before the
expclaim in the token, and a request rejected with an expired session re-authenticates and retries once. -
Errors — every error is a
BaseError, which is an aiohttpClientError:LoginError/SessionError(bothAuthError): rejected credentials or session.RateError: rate limit (HTTP 429, or BlueCloud's non-standard 229).ServerError: HTTP 5xx; does not discard the session.ResponseError: any other unexpected status; also aValueError.RequestTimeoutError: a request took longer than 20 seconds; also aTimeoutError.
Device setters (
set_fan_speed,set_standby, ...) store the new value on the device, and notify its callbacks, only once the API call returns; if it raises, the device keeps its last known state. An AWS write the API doesn't answer withSuccessraisesResponseError.MqttAwsBlueair.connect()raisesMqttError(aiomqtt's, exported here so callers needn't import aiomqtt) when the first connection fails, andValueErrorfor a region without an MQTT broker. -
Debug logging — request and response logs mask credentials, tokens, session secrets, signatures and email addresses.
Clone this repository and install the development environment:
git clone https://github.com/dahlb/blueair_api.git
cd blueair_api
scripts/setupUse the repository scripts so commands run through the local .venv.
scripts/setup
scripts/test
scripts/lintThe underlying commands are:
./.venv/bin/pytest
./.venv/bin/prek run --all-files
./.venv/bin/mypymypy runs in strict mode (strict = true in pyproject.toml) over
src/blueair_api and tests. The Lint workflow runs the same check on every push and pull
request.
Add or update pytest coverage for behavior changes. Fixture data for device
info responses lives under tests/device_info.
The live smoke test connects to a real Blueair account, loads and refreshes AWS devices, prints their state, starts the MQTT real-time listener, sends one sample control update to the first AWS device, and checks the legacy API.
scripts/live_smoke_testUse it only when you want to exercise the live Blueair APIs. The sample control update writes the first AWS device's current LED brightness back to it, so the setting is unchanged but the control path and MQTT shadow update are exercised. The script then waits 5 minutes so MQTT messages can be observed.
To run non-interactively, put credentials in a local env file and pass it
with --credentials-file. Supported keys are BLUEAIR_USERNAME/BLUEAIR_PASSWORD
(see scripts/blueair_api.env.example); TOML and JSON files with
username/password keys also work, as do BLUEAIR_USERNAME/BLUEAIR_PASSWORD
environment variables.
scripts/live_smoke_test --credentials-file scripts/blueair_api.env --output-file /private/tmp/blueair_api.txt --read-onlyOther options:
--output-filewrites a full transcript (stdout, stderr, and logs) for later API debugging or fixture updates.--read-onlyskips the sample control update while still loading devices and listening for MQTT messages.--regionselects the Blueair region (au,cn,eu,us; defaultus).--gigya-regionand--cloud-regionoverride the account-auth and device-control regions separately; see AWS account and BlueCloud regions.--skip-legacyskips the legacy (non-AWS) API check.