Skip to main content

A simple Python 3 library for Notion Home Monitoring

Project description

📟 aionotion: a Python3, asyncio-friendly library for Notion® Home Monitoring

CI PyPI Version License Code Coverage Maintainability

Buy Me A Coffee

aionotion is a Python 3, asyncio-friendly library for interacting with Notion home monitoring sensors.

Installation

pip install aionotion

Python Versions

aionotion is currently supported on:

  • Python 3.10
  • Python 3.11
  • Python 3.12

Usage

import asyncio

from aiohttp import ClientSession

from aionotion import async_get_client_with_credentials


async def main() -> None:
    """Create the aiohttp session and run the example."""
    client = await async_get_client_with_credentials(
        "<EMAIL>", "<PASSWORD>", session=session
    )

    # Get the UUID of the authenticated user:
    client.user_uuid
    # >>> xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

    # Get the current refresh token of the authenticated user (BE CAREFUL):
    client.refresh_token
    # >>> abcde12345

    # Get all "households" associated with the account:
    systems = await client.system.async_all()
    # >>> [System(...), System(...), ...]

    # Get a system by ID:
    system = await client.system.async_get(12345)
    # >>> System(...)

    # Get all bridges associated with the account:
    bridges = await client.bridge.async_all()
    # >>> [Bridge(...), Bridge(...), ...]

    # Get a bridge by ID:
    bridge = await client.bridge.async_get(12345)
    # >>> Bridge(...)

    # Get all sensors:
    sensors = await client.sensor.async_all()
    # >>> [Sensor(...), Sensor(...), ...]

    # Get a sensor by ID:
    sensor = await client.sensor.async_get(12345)
    # >>> Sensor(...)

    # Get "listeners" (conditions that a sensor is monitoring) for all sensors:
    listeners = await client.listener.async_all()
    # >>> [Listener(...), Listener(...), ...]

    # Get all listener definitions supported by Notion:
    definitions = await client.listener.async_definitions()
    # >>> [ListenerDefinition(...), ListenerDefinition(...), ...]

    # Get user info:
    user_info = await client.user.async_info()
    # >>> User(...)

    # Get user preferences:
    user_preferences = await client.user.async_preferences()
    # >>> UserPreferences(...)


asyncio.run(main())

Using a Refresh Token

During the normal course of operations, aionotion will automatically maintain a refresh token and use it when needed. At times, you may wish to manage that token yourself (so that you can use it later)–aionotion provides a few useful capabilities there.

Refresh Token Callbacks

aionotion allows implementers to defining callbacks that get called when a new refresh token is generated. These callbacks accept a single string parameter (the refresh token):

import asyncio

from aiohttp import ClientSession

from aionotion import async_get_client_with_credentials


async def main() -> None:
    """Create the aiohttp session and run the example."""
    client = await async_get_client_with_credentials(
        "<EMAIL>", "<PASSWORD>", session=session
    )

    def do_somethng_with_refresh_token(refresh_token: str) -> None:
        """Do something interesting."""
        pass

    # Attach the callback to the client:
    remove_callback = client.add_refresh_token_callback(do_somethng_with_refresh_token)

    # Later, if you want to remove the callback:
    remove_callback()


asyncio.run(main())

Getting a Client via a Refresh Token

All of previous examples retrieved an authenticated client with async_get_client_with_credentials. However, implementers may also create an authenticated client by providing a previously retrieved user UUID and refresh token:

import asyncio

from aiohttp import ClientSession

from aionotion import async_get_client_with_refresh_token


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as session:
        # Create a Notion API client:
        client = await async_get_client_with_refresh_token(
            "<USER UUID>", "<REFRESH TOKEN>", session=session
        )

        # Get to work...


asyncio.run(main())

Connection Pooling

By default, the library creates a new connection to Notion 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 aionotion import async_get_client_with_credentials


async def main() -> None:
    """Create the aiohttp session and run the example."""
    async with ClientSession() as session:
        # Create a Notion API client:
        client = await async_get_client_with_credentials(
            "<EMAIL>", "<PASSWORD>", session=session
        )

        # Get to work...


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!

  1. Check for open features/bugs or initiate a discussion on one.
  2. Fork the repository.
  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 aionotion tests
  9. Update README.md with any new documentation.
  10. 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

aionotion-2024.3.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

aionotion-2024.3.1-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file aionotion-2024.3.1.tar.gz.

File metadata

  • Download URL: aionotion-2024.3.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1015-azure

File hashes

Hashes for aionotion-2024.3.1.tar.gz
Algorithm Hash digest
SHA256 a2166865735ce624569ad0829571e83f682996f145433d53b2be3d5a67dc8618
MD5 9ac8d41d64d2cf2913d78ceaf565c598
BLAKE2b-256 9eeb6b7ee05789a64dfaaecf3d4aa94dbdd6cf7681ed7d9daa6b005c843b64c8

See more details on using hashes here.

File details

Details for the file aionotion-2024.3.1-py3-none-any.whl.

File metadata

  • Download URL: aionotion-2024.3.1-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1015-azure

File hashes

Hashes for aionotion-2024.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 97024184d51737768f7c086380e1d20e630a31adee06ae56fe67e49030a71f99
MD5 54ca2ed16c4a40b852a61a0ea60cff67
BLAKE2b-256 849083a44c8634179ad9f85313056844e9df0d3be53aadb016c4260ab279f0a9

See more details on using hashes here.

Supported by

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