Skip to main content

A clean, async-friendly library for the Ambient Weather API

Project description

🌤 aioambient: An async library for Ambient Weather Personal Weather Stations

Travis CI PyPi Version License Code Coverage Maintainability Say Thanks

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.5
  • Python 3.6
  • Python 3.7

However, running the test suite currently requires Python 3.6 or higher; tests run on Python 3.5 will fail.

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

Creating a Client

An aioambient client starts with an aiohttp ClientSession:

import asyncio

from aiohttp import ClientSession

from aioambient import Client


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as websession:
      # YOUR CODE HERE


asyncio.get_event_loop().run_until_complete(main())

Create a client, initialize it, then get to it:

import asyncio

from aiohttp import ClientSession

from aioambient import Client


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as websession:
      client = Client(
        '<YOUR API KEY>',
        '<YOUR APPLICATION KEY>',
        websession)


asyncio.get_event_loop().run_until_complete(main())

REST API

import asyncio

from aiohttp import ClientSession

from aioambient import Client


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as websession:
      client = Client(
        '<YOUR API KEY>',
        '<YOUR APPLICATION KEY>',
        websession)

      # Get all devices in an account:
      await client.api.get_devices()

      # Get all stored readings from a device:
      await client.api.get_device_details('<DEVICE MAC ADDRESS>')

      # Get all stored readings from a device (starting at a datetime):
      await client.api.get_device_details(
        '<DEVICE MAC ADDRESS>', end_date="2019-01-16")


asyncio.get_event_loop().run_until_complete(main())

Please be aware of Ambient Weather's rate limiting policies.

Websocket API

import asyncio

from aiohttp import ClientSession

from aioambient import Client


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as websession:
      client = Client(
        '<YOUR API KEY>',
        '<YOUR APPLICATION KEY>',
        websession)

      # 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')
      client.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')
      client.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('Subscription data received: {0}'.format(data))
      client.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('Subscription data received: {0}'.format(data))
      client.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('Data received: {0}'.format(data))
      client.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('Data received: {0}'.format(data))
      client.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')
      client.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')
      client.websocket.async_on_disconnect(disconnect_coroutine)

      # Connect to the websocket:
      await client.websocket.connect()

      # At any point, disconnect from the websocket:
      await client.websocket.disconnect()


loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()

Contributing

  1. Check for open features/bugs or initiate a discussion on one.
  2. Fork the repository.
  3. Install the dev environment: make init.
  4. Enter the virtual environment: pipenv shell
  5. Code your new feature or bug fix.
  6. Write a test that covers your new functionality.
  7. Run tests and ensure 100% code coverage: make coverage
  8. Add yourself to AUTHORS.md.
  9. Submit a pull request!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioambient-0.3.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioambient-0.3.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file aioambient-0.3.2.tar.gz.

File metadata

  • Download URL: aioambient-0.3.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for aioambient-0.3.2.tar.gz
Algorithm Hash digest
SHA256 417768cb9f790e8adf5afb24258bd8694292049619af1140524c33607ae7c63e
MD5 666192ff4a3d0f256061c0ab85dc0e22
BLAKE2b-256 ad0e26a97553d45e01a79be47a7378e524d114fbf1de0849f4d228dc9634dbb4

See more details on using hashes here.

File details

Details for the file aioambient-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: aioambient-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for aioambient-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1c666aa0f2df9da05b9fb0c690b37171d2f5d51b6306cc3369e801424a895dae
MD5 64ed87f23822a610857eeb9e7eb68091
BLAKE2b-256 00ac1d0a26072bacf324bcea75ffc7802c909d051c02327ce3fe3214c8d7346b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page