A clean, async-friendly library for the Ambient Weather API
Project description
🌤 aioambient: An async library for Ambient Weather Personal Weather Stations
aioambient
is a Python3, asyncio-driven library that interfaces with both the REST and
Websocket APIs provided by Ambient Weather.
Installation
pip install aioambient
Python Versions
aioambient
is currently supported on:
- Python 3.10
- Python 3.11
- Python 3.12
API and Application Keys
Utilizing aioambient
requires both an Application Key and an API Key from Ambient
Weather. You can generate both from the Profile page in your
Ambient Weather Dashboard.
Usage
REST API
import asyncio
from datetime import date
from aiohttp import ClientSession
from aioambient import API
async def main() -> None:
"""Create the aiohttp session and run the example."""
api = API("<YOUR APPLICATION KEY>", "<YOUR API KEY>")
# Get all devices in an account:
await api.get_devices()
# Get all stored readings from a device:
await api.get_device_details("<DEVICE MAC ADDRESS>")
# Get all stored readings from a device (starting at a datetime):
await api.get_device_details("<DEVICE MAC ADDRESS>", end_date=date(2019, 1, 16))
asyncio.run(main())
By default, the library creates a new connection to Ambient Weather 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 datetime import date
from aiohttp import ClientSession
from aioambient import API
async def main() -> None:
"""Create the aiohttp session and run the example."""
async with ClientSession() as session:
api = API("<YOUR APPLICATION KEY>", "<YOUR API KEY>")
# Get all devices in an account:
await api.get_devices()
# Get all stored readings from a device:
await api.get_device_details("<DEVICE MAC ADDRESS>")
# Get all stored readings from a device (starting at a datetime):
await api.get_device_details("<DEVICE MAC ADDRESS>", end_date=date(2019, 1, 16))
asyncio.run(main())
Please be aware of Ambient Weather's rate limiting policies.
Websocket API
import asyncio
from aiohttp import ClientSession
from aioambient import Websocket
async def main() -> None:
"""Create the aiohttp session and run the example."""
websocket = Websocket("<YOUR APPLICATION KEY>", "<YOUR API KEY>")
# Note that you can watch multiple API keys at once:
websocket = Websocket("YOUR APPLICATION KEY", ["<API KEY 1>", "<API KEY 2>"])
# Define a method that should be fired when the websocket client
# connects:
def connect_method():
"""Print a simple "hello" message."""
print("Client has connected to the websocket")
websocket.on_connect(connect_method)
# Alternatively, define a coroutine handler:
async def connect_coroutine():
"""Waits for 3 seconds, then print a simple "hello" message."""
await asyncio.sleep(3)
print("Client has connected to the websocket")
websocket.async_on_connect(connect_coroutine)
# Define a method that should be run upon subscribing to the Ambient
# Weather cloud:
def subscribed_method(data):
"""Print the data received upon subscribing."""
print(f"Subscription data received: {data}")
websocket.on_subscribed(subscribed_method)
# Alternatively, define a coroutine handler:
async def subscribed_coroutine(data):
"""Waits for 3 seconds, then print the incoming data."""
await asyncio.sleep(3)
print(f"Subscription data received: {data}")
websocket.async_on_subscribed(subscribed_coroutine)
# Define a method that should be run upon receiving data:
def data_method(data):
"""Print the data received."""
print(f"Data received: {data}")
websocket.on_data(data_method)
# Alternatively, define a coroutine handler:
async def data_coroutine(data):
"""Wait for 3 seconds, then print the data received."""
await asyncio.sleep(3)
print(f"Data received: {data}")
websocket.async_on_data(data_coroutine)
# Define a method that should be run when the websocket client
# disconnects:
def disconnect_method(data):
"""Print a simple "goodbye" message."""
print("Client has disconnected from the websocket")
websocket.on_disconnect(disconnect_method)
# Alternatively, define a coroutine handler:
async def disconnect_coroutine(data):
"""Wait for 3 seconds, then print a simple "goodbye" message."""
await asyncio.sleep(3)
print("Client has disconnected from the websocket")
websocket.async_on_disconnect(disconnect_coroutine)
# Connect to the websocket:
await websocket.connect()
# At any point, disconnect from the websocket:
await websocket.disconnect()
asyncio.run(main())
Open REST API
The official REST API and Websocket API require an API and application key to access data for the devices you own. This API cannot be used if you do not own a personal weather station.
However, there is a second, undocumented API that is used by the https://ambientweather.net
web application that does not require an API and application key. You can use the
OpenAPI
class to retrieve weather station data from this API:
import asyncio
from datetime import date
from aiohttp import ClientSession
from aioambient import OpenAPI
async def main() -> None:
"""Create the aiohttp session and run the example."""
api = OpenAPI()
# Get a list of all the devices that are located within a radius of
# three miles from the given latitude/longitude. Each device lists its
# MAC address.
await api.get_devices_by_location(32.5, -97.3, 3.0)
# Get the current data from a device:
await api.get_device_details("<DEVICE MAC ADDRESS>")
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 aioambient 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 aioambient-2024.8.0.tar.gz
.
File metadata
- Download URL: aioambient-2024.8.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a11643343db382ba9c5a79c70a9394dbf2ae6b1d4a28c4ef43b7c69618b237da |
|
MD5 | 735f6e9ddd74f0b6cd2b74fa601cd7c4 |
|
BLAKE2b-256 | 83680846033ed3676295f6769e24621dba75689a9c31020b7c16fa73b0127122 |
File details
Details for the file aioambient-2024.8.0-py3-none-any.whl
.
File metadata
- Download URL: aioambient-2024.8.0-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5656bfc3c614bb8fddd945ffbe6f9aaa19d3bbda4db2beed58c1b79e1d928fea |
|
MD5 | f4d5237766d628f738ba4177a81a86a1 |
|
BLAKE2b-256 | b5311a0b97c6e52a1748aa56f206e14e2cc704ffaa4869aaa71ab6fe198f0b9c |