aioambient


Nameaioambient JSON
Version 2024.1.0 PyPI version JSON
download
home_pagehttps://github.com/bachya/aioambient
SummaryA clean, async-friendly library for the Ambient Weather API
upload_time2024-01-10 22:36:12
maintainer
docs_urlNone
authorAaron Bach
requires_python>=3.10,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 🌤 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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bachya/aioambient",
    "name": "aioambient",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Aaron Bach",
    "author_email": "bachya1208@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/af/99/21b5b9437cba939085ea85f8e30e370f762ab52b5fa460d405902d02e217/aioambient-2024.1.0.tar.gz",
    "platform": null,
    "description": "# \ud83c\udf24 aioambient: An async library for Ambient Weather Personal Weather Stations\n\n[![CI][ci-badge]][ci]\n[![PyPI][pypi-badge]][pypi]\n[![Version][version-badge]][version]\n[![License][license-badge]][license]\n[![Code Coverage][codecov-badge]][codecov]\n[![Maintainability][maintainability-badge]][maintainability]\n\n<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>\n\n`aioambient` is a Python3, asyncio-driven library that interfaces with both the REST and\nWebsocket APIs provided by [Ambient Weather][ambient-weather].\n\n- [Installation](#installation)\n- [Python Versions](#python-versions)\n- [API and Application Keys](#api-and-application-keys)\n- [Usage](#usage)\n- [Contributing](#contributing)\n\n# Installation\n\n```bash\npip install aioambient\n```\n\n# Python Versions\n\n`aioambient` is currently supported on:\n\n- Python 3.10\n- Python 3.11\n- Python 3.12\n\n# API and Application Keys\n\nUtilizing `aioambient` requires both an Application Key and an API Key from Ambient\nWeather. You can generate both from the Profile page in your\n[Ambient Weather Dashboard][ambient-weather-dashboard].\n\n# Usage\n\n## REST API\n\n```python\nimport asyncio\nfrom datetime import date\n\nfrom aiohttp import ClientSession\n\nfrom aioambient import API\n\n\nasync def main() -> None:\n    \"\"\"Create the aiohttp session and run the example.\"\"\"\n    api = API(\"<YOUR APPLICATION KEY>\", \"<YOUR API KEY>\")\n\n    # Get all devices in an account:\n    await api.get_devices()\n\n    # Get all stored readings from a device:\n    await api.get_device_details(\"<DEVICE MAC ADDRESS>\")\n\n    # Get all stored readings from a device (starting at a datetime):\n    await api.get_device_details(\"<DEVICE MAC ADDRESS>\", end_date=date(2019, 1, 16))\n\n\nasyncio.run(main())\n```\n\nBy default, the library creates a new connection to Ambient Weather with each coroutine.\nIf you are calling a large number of coroutines (or merely want to squeeze out every\nsecond of runtime savings possible), an [`aiohttp`][aiohttp] `ClientSession` can be used for\nconnection pooling:\n\n```python\nimport asyncio\nfrom datetime import date\n\nfrom aiohttp import ClientSession\n\nfrom aioambient import API\n\n\nasync def main() -> None:\n    \"\"\"Create the aiohttp session and run the example.\"\"\"\n    async with ClientSession() as session:\n        api = API(\"<YOUR APPLICATION KEY>\", \"<YOUR API KEY>\")\n\n        # Get all devices in an account:\n        await api.get_devices()\n\n        # Get all stored readings from a device:\n        await api.get_device_details(\"<DEVICE MAC ADDRESS>\")\n\n        # Get all stored readings from a device (starting at a datetime):\n        await api.get_device_details(\"<DEVICE MAC ADDRESS>\", end_date=date(2019, 1, 16))\n\n\nasyncio.run(main())\n```\n\nPlease be aware of Ambient Weather's\n[rate limiting policies][ambient-weather-rate-limiting].\n\n## Websocket API\n\n```python\nimport asyncio\n\nfrom aiohttp import ClientSession\n\nfrom aioambient import Websocket\n\n\nasync def main() -> None:\n    \"\"\"Create the aiohttp session and run the example.\"\"\"\n    websocket = Websocket(\"<YOUR APPLICATION KEY>\", \"<YOUR API KEY>\")\n\n    # Note that you can watch multiple API keys at once:\n    websocket = Websocket(\"YOUR APPLICATION KEY\", [\"<API KEY 1>\", \"<API KEY 2>\"])\n\n    # Define a method that should be fired when the websocket client\n    # connects:\n    def connect_method():\n        \"\"\"Print a simple \"hello\" message.\"\"\"\n        print(\"Client has connected to the websocket\")\n\n    websocket.on_connect(connect_method)\n\n    # Alternatively, define a coroutine handler:\n    async def connect_coroutine():\n        \"\"\"Waits for 3 seconds, then print a simple \"hello\" message.\"\"\"\n        await asyncio.sleep(3)\n        print(\"Client has connected to the websocket\")\n\n    websocket.async_on_connect(connect_coroutine)\n\n    # Define a method that should be run upon subscribing to the Ambient\n    # Weather cloud:\n    def subscribed_method(data):\n        \"\"\"Print the data received upon subscribing.\"\"\"\n        print(f\"Subscription data received: {data}\")\n\n    websocket.on_subscribed(subscribed_method)\n\n    # Alternatively, define a coroutine handler:\n    async def subscribed_coroutine(data):\n        \"\"\"Waits for 3 seconds, then print the incoming data.\"\"\"\n        await asyncio.sleep(3)\n        print(f\"Subscription data received: {data}\")\n\n    websocket.async_on_subscribed(subscribed_coroutine)\n\n    # Define a method that should be run upon receiving data:\n    def data_method(data):\n        \"\"\"Print the data received.\"\"\"\n        print(f\"Data received: {data}\")\n\n    websocket.on_data(data_method)\n\n    # Alternatively, define a coroutine handler:\n    async def data_coroutine(data):\n        \"\"\"Wait for 3 seconds, then print the data received.\"\"\"\n        await asyncio.sleep(3)\n        print(f\"Data received: {data}\")\n\n    websocket.async_on_data(data_coroutine)\n\n    # Define a method that should be run when the websocket client\n    # disconnects:\n    def disconnect_method(data):\n        \"\"\"Print a simple \"goodbye\" message.\"\"\"\n        print(\"Client has disconnected from the websocket\")\n\n    websocket.on_disconnect(disconnect_method)\n\n    # Alternatively, define a coroutine handler:\n    async def disconnect_coroutine(data):\n        \"\"\"Wait for 3 seconds, then print a simple \"goodbye\" message.\"\"\"\n        await asyncio.sleep(3)\n        print(\"Client has disconnected from the websocket\")\n\n    websocket.async_on_disconnect(disconnect_coroutine)\n\n    # Connect to the websocket:\n    await websocket.connect()\n\n    # At any point, disconnect from the websocket:\n    await websocket.disconnect()\n\n\nasyncio.run(main())\n```\n\n## Open REST API\n\nThe official REST API and Websocket API require an API and application key to access\ndata for the devices you own. This API cannot be used if you do not own a personal\nweather station.\n\nHowever, there is a second, undocumented API that is used by the https://ambientweather.net\nweb application that does not require an API and application key. You can use the\n`OpenAPI` class to retrieve weather station data from this API:\n\n```python\nimport asyncio\nfrom datetime import date\nfrom aiohttp import ClientSession\nfrom aioambient import OpenAPI\n\n\nasync def main() -> None:\n    \"\"\"Create the aiohttp session and run the example.\"\"\"\n    api = OpenAPI()\n\n    # Get a list of all the devices that are located within a radius of\n    # three miles from the given latitude/longitude. Each device lists its\n    # MAC address.\n    await api.get_devices_by_location(32.5, -97.3, 3.0)\n\n    # Get the current data from a device:\n    await api.get_device_details(\"<DEVICE MAC ADDRESS>\")\n\n\nasyncio.run(main())\n```\n\n# Contributing\n\nThanks to all of [our contributors][contributors] so far!\n\n1. [Check for open features/bugs][issues] or [initiate a discussion on one][new-issue].\n2. [Fork the repository][fork].\n3. (_optional, but highly recommended_) Create a virtual environment: `python3 -m venv .venv`\n4. (_optional, but highly recommended_) Enter the virtual environment: `source ./.venv/bin/activate`\n5. Install the dev environment: `script/setup`\n6. Code your new feature or bug fix on a new branch.\n7. Write tests that cover your new functionality.\n8. Run tests and ensure 100% code coverage: `poetry run pytest --cov aioambient tests`\n9. Update `README.md` with any new documentation.\n10. Submit a pull request!\n\n[aiohttp]: https://github.com/aio-libs/aiohttp\n[ambient-weather-dashboard]: https://dashboard.ambientweather.net\n[ambient-weather-rate-limiting]: https://ambientweather.docs.apiary.io/#introduction/rate-limiting\n[ambient-weather]: https://ambientweather.net\n[ci-badge]: https://github.com/bachya/aioambient/workflows/CI/badge.svg\n[ci]: https://github.com/bachya/aioambient/actions\n[codecov-badge]: https://codecov.io/gh/bachya/aioambient/branch/dev/graph/badge.svg\n[codecov]: https://codecov.io/gh/bachya/aioambient\n[contributors]: https://github.com/bachya/aioambient/graphs/contributors\n[fork]: https://github.com/bachya/aioambient/fork\n[issues]: https://github.com/bachya/aioambient/issues\n[license-badge]: https://img.shields.io/pypi/l/aioambient.svg\n[license]: https://github.com/bachya/aioambient/blob/main/LICENSE\n[maintainability-badge]: https://api.codeclimate.com/v1/badges/81a9f8274abf325b2fa4/maintainability\n[maintainability]: https://codeclimate.com/github/bachya/aioambient/maintainability\n[new-issue]: https://github.com/bachya/aioambient/issues/new\n[new-issue]: https://github.com/bachya/aioambient/issues/new\n[pypi-badge]: https://img.shields.io/pypi/v/aioambient.svg\n[pypi]: https://pypi.python.org/pypi/aioambient\n[version-badge]: https://img.shields.io/pypi/pyversions/aioambient.svg\n[version]: https://pypi.python.org/pypi/aioambient\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A clean, async-friendly library for the Ambient Weather API",
    "version": "2024.1.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/bachya/aioambient/issues",
        "Changelog": "https://github.com/bachya/aioambient/releases",
        "Homepage": "https://github.com/bachya/aioambient",
        "Repository": "https://github.com/bachya/aioambient"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f48d1b7e3bd558ff6d592fff832e63d0fd58e4b7f0f397c8157fb301c305df73",
                "md5": "0867c8fc393b0a3e3c54530da6dc5a8b",
                "sha256": "ca6bfb4ea4fe9d2fdc3563908d99a92389eda9c742bdac3e11e0f43dfd3f821a"
            },
            "downloads": -1,
            "filename": "aioambient-2024.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0867c8fc393b0a3e3c54530da6dc5a8b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 13839,
            "upload_time": "2024-01-10T22:36:10",
            "upload_time_iso_8601": "2024-01-10T22:36:10.139824Z",
            "url": "https://files.pythonhosted.org/packages/f4/8d/1b7e3bd558ff6d592fff832e63d0fd58e4b7f0f397c8157fb301c305df73/aioambient-2024.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "af9921b5b9437cba939085ea85f8e30e370f762ab52b5fa460d405902d02e217",
                "md5": "672ba27889d66079aeb94ec96f5b9012",
                "sha256": "cad62a6e00dd8afdb3126a256010b0c0abdb4433b3534a604e31d92b49b025fa"
            },
            "downloads": -1,
            "filename": "aioambient-2024.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "672ba27889d66079aeb94ec96f5b9012",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 13603,
            "upload_time": "2024-01-10T22:36:12",
            "upload_time_iso_8601": "2024-01-10T22:36:12.004693Z",
            "url": "https://files.pythonhosted.org/packages/af/99/21b5b9437cba939085ea85f8e30e370f762ab52b5fa460d405902d02e217/aioambient-2024.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-10 22:36:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bachya",
    "github_project": "aioambient",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "aioambient"
}
        
Elapsed time: 0.16362s