A Python 3, asyncio-based library to interact with the PurpleAir API
Project description
🟣 aiopurpleair: A Python3, asyncio-based library to interact with the PurpleAir API
aiopurpleair
is a Python3, asyncio-based library to interact with the
PurpleAir API.
Installation
pip install aiopurpleair
Python Versions
aiopurpleair
is currently supported on:
- Python 3.10
- Python 3.11
- Python 3.12
Usage
In-depth documentation on the API can be found here. Unless otherwise
noted, aiopurpleair
endeavors to follow the API as closely as possible.
Checking an API Key
To check whether an API key is valid and what properties it has:
import asyncio
from aiopurpleair import API
async def main() -> None:
"""Run."""
api = API("<API KEY>")
response = await api.async_check_api_key()
# >>> response.api_key_type == ApiKeyType.READ
# >>> response.api_version == "V1.0.11-0.0.41"
# >>> response.timestamp_utc == datetime(2022, 10, 27, 18, 25, 41)
asyncio.run(main())
Getting Sensors
import asyncio
from aiopurpleair import API
async def main() -> None:
"""Run."""
api = API("<API_KEY>")
response = await api.sensors.async_get_sensors(["name"])
# >>> response.api_version == "V1.0.11-0.0.41"
# >>> response.data == {
# >>> 131075: SensorModel(sensor_index=131075, name=Mariners Bluff),
# >>> 131079: SensorModel(sensor_index=131079, name=BRSKBV-outside),
# >>> }
# >>> response.data_timestamp_utc == datetime(2022, 11, 3, 19, 25, 31)
# >>> response.fields == ["sensor_index", "name"]
# >>> response.firmware_default_version == "7.02"
# >>> response.max_age == 604800
# >>> response.timestamp_utc == datetime(2022, 11, 3, 19, 26, 29)
asyncio.run(main())
Method Parameters
fields
(required): The sensor data fields to includelocation_type
(optional): An LocationType to filter bymax_age
(optional): Filter results modified within these secondsmodified_since
(optional): Filter results modified since a UTC datetimeread_keys
(optional): Read keys for private sensorssensor_indices
(optional): Filter results by sensor index
Getting a Single Sensor
import asyncio
from aiopurpleair import API
async def main() -> None:
"""Run."""
api = API("<API_KEY>")
response = await api.sensors.async_get_sensor(131075)
# >>> response.api_version == "V1.0.11-0.0.41"
# >>> response.data_timestamp_utc == datetime(2022, 11, 5, 16, 36, 21)
# >>> response.sensor == SensorModel(sensor_index=131075, ...),
# >>> response.timestamp_utc == datetime(2022, 11, 5, 16, 37, 3)
asyncio.run(main())
Method Parameters
sensor_index
(required): The sensor index of the sensor to retrieve.fields
(optional): The sensor data fields to include.read_key
(optional): A read key for a private sensor.
Getting Nearby Sensors
This method returns a list of NearbySensorResult
objects that are within a bounding box
around a given latitude/longitude pair. The list is sorted from nearest to furthest
(i.e., the first index in the list is the closest to the latitude/longitude).
NearbySensorResult
objects have two properties:
sensor
: the correspondingSensorModel
objectdistance
: the calculated distance (in kilometers) between this sensor and the provided latitude/longitude
import asyncio
from aiopurpleair import API
async def main() -> None:
"""Run."""
api = API("<API_KEY>")
sensors = await api.sensors.async_get_nearby_sensors(
["name"], 51.5285582, -0.2416796, 10
)
# >>> [NearbySensorResult(...), NearbySensorResult(...)]
asyncio.run(main())
Method Parameters
fields
(required): The sensor data fields to includelatitude
(required): The latitude of the point to measure distance fromlongitude
(required): The longitude of the point to measure distance fromdistance
(required): The distance from the measured point to search (in kilometers)limit
(optional): Limit the results
Getting a Map URL
If you need to get the URL to a particular sensor index on the PurpleAir map website,
simply pass the appropriate sensor index to the get_map_url
method:
import asyncio
from aiopurpleair import API
async def main() -> None:
"""Run."""
api = API("<API_KEY>")
map_url = api.get_map_url(12345)
# >>> https://map.purpleair.com/1/mAQI/a10/p604800/cC0?select=12345
asyncio.run(main())
Connection Pooling
By default, the library creates a new connection to the PurpleAir API with each
coroutine. If you are calling a large number of coroutines (or merely want to squeeze
out every second of runtime savings possible), an aiohttp
ClientSession
can
be used for connection pooling:
import asyncio
from aiohttp import ClientSession
from aiopurpleair import API
async def main() -> None:
"""Run."""
async with ClientSession() as session:
api = await API("<API KEY>")
# Get to work...
asyncio.run(main())
Contributing
Thanks to all of our contributors so far!
- Check for open features/bugs or initiate a discussion on one.
- Fork the repository.
- (optional, but highly recommended) Create a virtual environment:
python3 -m venv .venv
- (optional, but highly recommended) Enter the virtual environment:
source ./.venv/bin/activate
- Install the dev environment:
script/setup
- Code your new feature or bug fix on a new branch.
- Write tests that cover your new functionality.
- Run tests and ensure 100% code coverage:
poetry run pytest --cov aiopurpleair tests
- Update
README.md
with any new documentation. - Submit a pull request!
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file aiopurpleair-2023.12.0.tar.gz
.
File metadata
- Download URL: aiopurpleair-2023.12.0.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2598d53b78fc5582b5ca1ca463d5277fe303075f6758329b37b98447fc2b9f89 |
|
MD5 | 4ec22629651ebd5792fbf7d76ab22085 |
|
BLAKE2b-256 | 6a22854663b08e33a1e713d669f6e541e1ee72fb232ffd4fdfb8f69ee6af1b71 |
File details
Details for the file aiopurpleair-2023.12.0-py3-none-any.whl
.
File metadata
- Download URL: aiopurpleair-2023.12.0-py3-none-any.whl
- Upload date:
- Size: 19.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 532ed293444e89d16ca612bbb9186bc1e6d97a85b0503125220a28d04774ea0a |
|
MD5 | 874480ba4979aac068cc53821f9df769 |
|
BLAKE2b-256 | 7abf839ab7a1280d0288be1dce968a448d517f1a5be6078099646c7c391d951e |