1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
# 🌤 aioambient: An async library for Ambient Weather Personal Weather Stations
[![CI][ci-badge]][ci]
[![PyPI][pypi-badge]][pypi]
[![Version][version-badge]][version]
[![License][license-badge]][license]
[![Code Coverage][codecov-badge]][codecov]
[![Maintainability][maintainability-badge]][maintainability]
<a href="https://www.buymeacoffee.com/bachya1208P" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>
`aioambient` is a Python3, asyncio-driven library that interfaces with both the REST and
Websocket APIs provided by [Ambient Weather][ambient-weather].
- [Installation](#installation)
- [Python Versions](#python-versions)
- [API and Application Keys](#api-and-application-keys)
- [Usage](#usage)
- [Contributing](#contributing)
# Installation
```bash
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][ambient-weather-dashboard].
# Usage
## REST API
```python
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`][aiohttp] `ClientSession` can be used for
connection pooling:
```python
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][ambient-weather-rate-limiting].
## Websocket API
```python
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:
```python
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][contributors] so far!
1. [Check for open features/bugs][issues] or [initiate a discussion on one][new-issue].
2. [Fork the repository][fork].
3. (_optional, but highly recommended_) Create a virtual environment: `python3 -m venv .venv`
4. (_optional, but highly recommended_) Enter the virtual environment: `source ./.venv/bin/activate`
5. Install the dev environment: `script/setup`
6. Code your new feature or bug fix on a new branch.
7. Write tests that cover your new functionality.
8. Run tests and ensure 100% code coverage: `poetry run pytest --cov aioambient tests`
9. Update `README.md` with any new documentation.
10. Submit a pull request!
[aiohttp]: https://github.com/aio-libs/aiohttp
[ambient-weather-dashboard]: https://dashboard.ambientweather.net
[ambient-weather-rate-limiting]: https://ambientweather.docs.apiary.io/#introduction/rate-limiting
[ambient-weather]: https://ambientweather.net
[ci-badge]: https://github.com/bachya/aioambient/workflows/CI/badge.svg
[ci]: https://github.com/bachya/aioambient/actions
[codecov-badge]: https://codecov.io/gh/bachya/aioambient/branch/dev/graph/badge.svg
[codecov]: https://codecov.io/gh/bachya/aioambient
[contributors]: https://github.com/bachya/aioambient/graphs/contributors
[fork]: https://github.com/bachya/aioambient/fork
[issues]: https://github.com/bachya/aioambient/issues
[license-badge]: https://img.shields.io/pypi/l/aioambient.svg
[license]: https://github.com/bachya/aioambient/blob/main/LICENSE
[maintainability-badge]: https://api.codeclimate.com/v1/badges/81a9f8274abf325b2fa4/maintainability
[maintainability]: https://codeclimate.com/github/bachya/aioambient/maintainability
[new-issue]: https://github.com/bachya/aioambient/issues/new
[new-issue]: https://github.com/bachya/aioambient/issues/new
[pypi-badge]: https://img.shields.io/pypi/v/aioambient.svg
[pypi]: https://pypi.python.org/pypi/aioambient
[version-badge]: https://img.shields.io/pypi/pyversions/aioambient.svg
[version]: https://pypi.python.org/pypi/aioambient
|