☀️ pyairvisual: a thin Python wrapper for the AirVisual© API


pyairvisual
is a simple, clean, well-tested library for interacting with
AirVisual to retrieve air quality information.
Python Versions
pyairvisual
is currently supported on:
- Python 3.10
- Python 3.11
- Python 3.12
Installation
pip install pyairvisual
API Key
You can get an AirVisual API key from the AirVisual API site.
Depending on the plan you choose, more functionality will be available from the API:
The Community Plan gives access to:
- List supported countries
- List supported states
- List supported cities
- Get data from the nearest city based on IP address
- Get data from the nearest city based on latitude/longitude
- Get data from a specific city
Startup
The Startup Plan gives access to:
- List supported stations in a city
- Get data from the nearest station based on IP address
- Get data from the nearest station based on latitude/longitude
- Get data from a specific station
Enterprise
The Enterprise Plan gives access to:
- Get a global city ranking of air quality
Usage
Using the Cloud API
import asyncio
from pyairvisual.cloud_api import CloudAPI
async def main() -> None:
"""Run!"""
cloud_api = CloudAPI("<YOUR_AIRVISUAL_API_KEY>")
data = await cloud_api.air_quality.nearest_city()
data = await cloud_api.air_quality.nearest_city(
latitude=39.742599, longitude=-104.9942557
)
data = await cloud_api.air_quality.city(
city="Los Angeles", state="California", country="USA"
)
data = await cloud_api.air_quality.nearest_station()
data = await cloud_api.air_quality.nearest_station(
latitude=39.742599, longitude=-104.9942557
)
data = await cloud_api.air_quality.station(
station="US Embassy in Beijing",
city="Beijing",
state="Beijing",
country="China",
)
data = await cloud_api.air_quality.ranking()
countries = await cloud_api.supported.countries()
states = await cloud_api.supported.states("USA")
cities = await cloud_api.supported.cities("USA", "Colorado")
stations = await cloud_api.supported.stations("USA", "Colorado", "Denver")
asyncio.run(main())
By default, the library creates a new connection to AirVisual 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 pyairvisual.cloud_api import CloudAPI
async def main() -> None:
"""Run!"""
async with ClientSession() as session:
cloud_api = CloudAPI("<YOUR_AIRVISUAL_API_KEY>", session=session)
asyncio.run(main())
Working with Node/Pro Units
pyairvisual
also allows users to interact with Node/Pro units, both via
the cloud API:
import asyncio
from aiohttp import ClientSession
from pyairvisual.cloud_api import CloudAPI
async def main() -> None:
"""Run!"""
cloud_api = CloudAPI("<YOUR_AIRVISUAL_API_KEY>")
data = await cloud_api.node.get_by_node_id("<NODE_ID>")
asyncio.run(main())
...or over the local network via Samba (the unit password can be found
on the device itself):
import asyncio
from aiohttp import ClientSession
from pyairvisual.node import NodeSamba
async def main() -> None:
"""Run!"""
async with NodeSamba("<IP_ADDRESS_OR_HOST>", "<PASSWORD>") as node:
measurements = await node.async_get_latest_measurements()
history = await node.async_get_history()
asyncio.run(main())
Check out the examples, the tests, and the source files themselves for method
signatures and more examples.
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 pyairvisual tests
- Update
README.md
with any new documentation. - Submit a pull request!