Async Python SDK for the Airbeld backend
Project description
Airbeld Python SDK
Async Python SDK for the Airbeld API, providing access to air quality devices and telemetry data.
If you want to contribute, read CONTRIBUTING and DEVELOPER.
Installation
# Using pip
pip install airbeld-api-sdk
# Using uv (recommended)
uv add airbeld-api-sdk
Examples
Running Examples
All examples require setting environment variables before running:
# Export required environment variables
export AIRBELD_API_BASE="https://api.airbeld.com"
export AIRBELD_API_TOKEN="your-jwt-token-here"
# Run an example
python examples/quickstart.py
Environment Variables:
AIRBELD_API_BASE: API base URL (default:https://api.airbeld.com)AIRBELD_API_TOKEN: JWT access token for authentication
Quickstart Example
import asyncio
import os
from datetime import datetime, timedelta, timezone
from airbeld import AirbeldClient
async def main():
# Initialize client (JWT token obtained outside SDK)
base_url = os.environ.get("AIRBELD_API_BASE", "https://api.airbeld.com")
token = os.environ["AIRBELD_API_TOKEN"]
async with AirbeldClient(token=token, base_url=base_url) as client:
# Get all devices
devices = await client.async_get_devices()
print(f"Found {len(devices)} devices")
if devices:
device = devices[0]
print(f"Device: {device.name} ({device.status})")
# Get recent telemetry readings
end = datetime.now(timezone.utc)
start = end - timedelta(hours=1)
readings = await client.async_get_readings_by_date(
device_id=device.id,
start=start,
end=end,
sensors=["temperature", "pm2p5"] # Optional sensor filter
)
# Print latest temperature reading
if "temperature" in readings.sensors:
latest_temp = readings.get_latest_value("temperature")
print(f"Latest temperature: {latest_temp}°C")
if __name__ == "__main__":
asyncio.run(main())
Note: JWT token acquisition happens outside the SDK. The SDK only handles API requests with a ready token.
Getting Latest Readings (Simplified)
You can fetch the latest sensor readings without specifying a date range:
import asyncio
import os
from airbeld import AirbeldClient
async def main():
base_url = os.environ.get("AIRBELD_API_BASE", "https://api.airbeld.com")
token = os.environ["AIRBELD_API_TOKEN"]
async with AirbeldClient(token=token, base_url=base_url) as client:
devices = await client.async_get_devices()
if devices:
device = devices[0]
# Get latest readings without specifying start/end dates
readings = await client.async_get_readings_by_date(
device_id=device.id,
sensors=["temperature", "pm2p5", "humidity"] # Optional filter
)
# Display latest values
for sensor_name, metric in readings.sensors.items():
latest = readings.get_latest_value(sensor_name)
print(f"{metric.display_name}: {latest} {metric.unit}")
if __name__ == "__main__":
asyncio.run(main())
Authentication Options
The SDK supports two authentication paths depending on your use case:
Home Assistant Integration
For Home Assistant users, authentication is handled by the integration:
- Home Assistant performs OAuth2 flow automatically
- SDK receives a ready JWT token from the integration
- See docs/home-assistant-auth.md for details
Standalone Applications (CLI, Scripts, etc.)
For standalone applications, use the built-in authentication:
import asyncio
import os
from airbeld import async_login, AirbeldClient
async def main():
# Authenticate with email/password
token_set = await async_login(
base_url="https://api.airbeld.com",
email=os.environ["AIRBELD_USER_EMAIL"],
password=os.environ["AIRBELD_USER_PASSWORD"]
)
# Create client with access token
async with AirbeldClient(token=token_set.access_token) as client:
# List devices
devices = await client.async_get_devices()
print(f"Found {len(devices)} devices:")
for device in devices:
print(f" - {device.name} ({device.status})")
if __name__ == "__main__":
asyncio.run(main())
⚠️ Security Warning: Never commit real credentials or tokens to version control. Use environment variables or secure secret storage systems.
Token Management
For applications requiring token refresh or runtime token updates:
# Update token at runtime
client.set_token(new_token)
# Or use refresh token (if implemented)
new_token_set = await async_login(...)
client.set_token(new_token_set.access_token)
Development
Installation
Using uv (recommended):
# Clone the repository
git clone https://github.com/Embio-Diagnostics/airbeld-api-sdk.git
cd airbeld-api-sdk
# Create virtual environment and install dependencies
uv sync
Using pip:
# Clone the repository
git clone https://github.com/Embio-Diagnostics/airbeld-api-sdk.git
cd airbeld-api-sdk
# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install package with dev dependencies
pip install -e ".[dev]"
Running Tests
# Using uv
uv run pytest
# Or with pip
pytest
Linting and Formatting
# Check code style
uv run ruff check .
# Format code
uv run ruff format .
# Or with pip
ruff check .
ruff format .
Type Checking
# Using uv
uv run mypy src
# Or with pip
mypy src
Building the Package
# Using uv
uv build
# Or with pip
python -m build
Resources
- Contributing: See CONTRIBUTING.md for contribution guidelines
- Changelog: See CHANGELOG.md for version history
- License: This project is licensed under the MIT License - see LICENSE file for details
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file airbeld_api_sdk-0.2.0.tar.gz.
File metadata
- Download URL: airbeld_api_sdk-0.2.0.tar.gz
- Upload date:
- Size: 43.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc7a38ab4610d05a7849c72f7ccff835ebcd34c3aeaf3eedf4e4e8cef3d52677
|
|
| MD5 |
fe10e941364dca9432e4c3f03702f5aa
|
|
| BLAKE2b-256 |
b62ac163cfda2f020ca1eca49772ba9933f946a881d342a2b4694a4a9a4ad8f0
|
File details
Details for the file airbeld_api_sdk-0.2.0-py3-none-any.whl.
File metadata
- Download URL: airbeld_api_sdk-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
417135b4cc6684be5f986b6b45b032cb518aa5c1fac222fb91e964fd48ff10f1
|
|
| MD5 |
5ee43785a6ab1fa2bc710211a45aa337
|
|
| BLAKE2b-256 |
1109471d644a99494cb175cf35d64279fe3a057afd2d937ca6b40d04404df06a
|