# 📟 aionotion: a Python3, asyncio-friendly library for Notion® Home Monitoring
[![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>
`aionotion` is a Python 3, asyncio-friendly library for interacting with [Notion][notion]
home monitoring sensors.
- [Installation](#installation)
- [Python Versions](#python-versions)
- [Usage](#usage)
- [Contributing](#contributing)
# Installation
```bash
pip install aionotion
```
# Python Versions
`aionotion` is currently supported on:
- Python 3.10
- Python 3.11
- Python 3.12
# Usage
```python
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):
```python
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:
```python
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`][aiohttp] `ClientSession` can be used for
connection pooling:
```python
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][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 aionotion tests`
9. Update `README.md` with any new documentation.
10. Submit a pull request!
[aiohttp]: https://github.com/aio-libs/aiohttp
[ci-badge]: https://img.shields.io/github/actions/workflow/status/bachya/aionotion/test.yml
[ci]: https://github.com/bachya/aionotion/actions
[codecov-badge]: https://codecov.io/gh/bachya/aionotion/branch/dev/graph/badge.svg
[codecov]: https://codecov.io/gh/bachya/aionotion
[contributors]: https://github.com/bachya/aionotion/graphs/contributors
[fork]: https://github.com/bachya/aionotion/fork
[issues]: https://github.com/bachya/aionotion/issues
[license-badge]: https://img.shields.io/pypi/l/aionotion.svg
[license]: https://github.com/bachya/aionotion/blob/main/LICENSE
[maintainability-badge]: https://api.codeclimate.com/v1/badges/bd79edca07c8e4529cba/maintainability
[maintainability]: https://codeclimate.com/github/bachya/aionotion/maintainability
[new-issue]: https://github.com/bachya/aionotion/issues/new
[notion]: https://getnotion.com
[pypi-badge]: https://img.shields.io/pypi/v/aionotion.svg
[pypi]: https://pypi.python.org/pypi/aionotion
[version-badge]: https://img.shields.io/pypi/pyversions/aionotion.svg
[version]: https://pypi.python.org/pypi/aionotion
Raw data
{
"_id": null,
"home_page": "https://github.com/bachya/aionotion",
"name": "aionotion",
"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/9e/eb/6b7ee05789a64dfaaecf3d4aa94dbdd6cf7681ed7d9daa6b005c843b64c8/aionotion-2024.3.1.tar.gz",
"platform": null,
"description": "# \ud83d\udcdf aionotion: a Python3, asyncio-friendly library for Notion\u00ae Home Monitoring\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`aionotion` is a Python 3, asyncio-friendly library for interacting with [Notion][notion]\nhome monitoring sensors.\n\n- [Installation](#installation)\n- [Python Versions](#python-versions)\n- [Usage](#usage)\n- [Contributing](#contributing)\n\n# Installation\n\n```bash\npip install aionotion\n```\n\n# Python Versions\n\n`aionotion` is currently supported on:\n\n- Python 3.10\n- Python 3.11\n- Python 3.12\n\n# Usage\n\n```python\nimport asyncio\n\nfrom aiohttp import ClientSession\n\nfrom aionotion import async_get_client_with_credentials\n\n\nasync def main() -> None:\n \"\"\"Create the aiohttp session and run the example.\"\"\"\n client = await async_get_client_with_credentials(\n \"<EMAIL>\", \"<PASSWORD>\", session=session\n )\n\n # Get the UUID of the authenticated user:\n client.user_uuid\n # >>> xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n\n # Get the current refresh token of the authenticated user (BE CAREFUL):\n client.refresh_token\n # >>> abcde12345\n\n # Get all \"households\" associated with the account:\n systems = await client.system.async_all()\n # >>> [System(...), System(...), ...]\n\n # Get a system by ID:\n system = await client.system.async_get(12345)\n # >>> System(...)\n\n # Get all bridges associated with the account:\n bridges = await client.bridge.async_all()\n # >>> [Bridge(...), Bridge(...), ...]\n\n # Get a bridge by ID:\n bridge = await client.bridge.async_get(12345)\n # >>> Bridge(...)\n\n # Get all sensors:\n sensors = await client.sensor.async_all()\n # >>> [Sensor(...), Sensor(...), ...]\n\n # Get a sensor by ID:\n sensor = await client.sensor.async_get(12345)\n # >>> Sensor(...)\n\n # Get \"listeners\" (conditions that a sensor is monitoring) for all sensors:\n listeners = await client.listener.async_all()\n # >>> [Listener(...), Listener(...), ...]\n\n # Get all listener definitions supported by Notion:\n definitions = await client.listener.async_definitions()\n # >>> [ListenerDefinition(...), ListenerDefinition(...), ...]\n\n # Get user info:\n user_info = await client.user.async_info()\n # >>> User(...)\n\n # Get user preferences:\n user_preferences = await client.user.async_preferences()\n # >>> UserPreferences(...)\n\n\nasyncio.run(main())\n```\n\n## Using a Refresh Token\n\nDuring the normal course of operations, `aionotion` will automatically maintain a refresh\ntoken and use it when needed. At times, you may wish to manage that token yourself (so\nthat you can use it later)\u2013`aionotion` provides a few useful capabilities there.\n\n### Refresh Token Callbacks\n\n`aionotion` allows implementers to defining callbacks that get called when a new refresh\ntoken is generated. These callbacks accept a single string parameter (the refresh\ntoken):\n\n```python\nimport asyncio\n\nfrom aiohttp import ClientSession\n\nfrom aionotion import async_get_client_with_credentials\n\n\nasync def main() -> None:\n \"\"\"Create the aiohttp session and run the example.\"\"\"\n client = await async_get_client_with_credentials(\n \"<EMAIL>\", \"<PASSWORD>\", session=session\n )\n\n def do_somethng_with_refresh_token(refresh_token: str) -> None:\n \"\"\"Do something interesting.\"\"\"\n pass\n\n # Attach the callback to the client:\n remove_callback = client.add_refresh_token_callback(do_somethng_with_refresh_token)\n\n # Later, if you want to remove the callback:\n remove_callback()\n\n\nasyncio.run(main())\n```\n\n### Getting a Client via a Refresh Token\n\nAll of previous examples retrieved an authenticated client with\n`async_get_client_with_credentials`. However, implementers may also create an\nauthenticated client by providing a previously retrieved user UUID and refresh token:\n\n```python\nimport asyncio\n\nfrom aiohttp import ClientSession\n\nfrom aionotion import async_get_client_with_refresh_token\n\n\nasync def main() -> None:\n \"\"\"Create the aiohttp session and run the example.\"\"\"\n async with ClientSession() as session:\n # Create a Notion API client:\n client = await async_get_client_with_refresh_token(\n \"<USER UUID>\", \"<REFRESH TOKEN>\", session=session\n )\n\n # Get to work...\n\n\nasyncio.run(main())\n```\n\n## Connection Pooling\n\nBy default, the library creates a new connection to Notion with each coroutine. If you\nare calling a large number of coroutines (or merely want to squeeze out every second of\nruntime savings possible), an [`aiohttp`][aiohttp] `ClientSession` can be used for\nconnection pooling:\n\n```python\nimport asyncio\n\nfrom aiohttp import ClientSession\n\nfrom aionotion import async_get_client_with_credentials\n\n\nasync def main() -> None:\n \"\"\"Create the aiohttp session and run the example.\"\"\"\n async with ClientSession() as session:\n # Create a Notion API client:\n client = await async_get_client_with_credentials(\n \"<EMAIL>\", \"<PASSWORD>\", session=session\n )\n\n # Get to work...\n\n\nasyncio.run(main())\n```\n\nCheck out the examples, the tests, and the source files themselves for method\nsignatures and more examples.\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 aionotion tests`\n9. Update `README.md` with any new documentation.\n10. Submit a pull request!\n\n[aiohttp]: https://github.com/aio-libs/aiohttp\n[ci-badge]: https://img.shields.io/github/actions/workflow/status/bachya/aionotion/test.yml\n[ci]: https://github.com/bachya/aionotion/actions\n[codecov-badge]: https://codecov.io/gh/bachya/aionotion/branch/dev/graph/badge.svg\n[codecov]: https://codecov.io/gh/bachya/aionotion\n[contributors]: https://github.com/bachya/aionotion/graphs/contributors\n[fork]: https://github.com/bachya/aionotion/fork\n[issues]: https://github.com/bachya/aionotion/issues\n[license-badge]: https://img.shields.io/pypi/l/aionotion.svg\n[license]: https://github.com/bachya/aionotion/blob/main/LICENSE\n[maintainability-badge]: https://api.codeclimate.com/v1/badges/bd79edca07c8e4529cba/maintainability\n[maintainability]: https://codeclimate.com/github/bachya/aionotion/maintainability\n[new-issue]: https://github.com/bachya/aionotion/issues/new\n[notion]: https://getnotion.com\n[pypi-badge]: https://img.shields.io/pypi/v/aionotion.svg\n[pypi]: https://pypi.python.org/pypi/aionotion\n[version-badge]: https://img.shields.io/pypi/pyversions/aionotion.svg\n[version]: https://pypi.python.org/pypi/aionotion\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A simple Python 3 library for Notion Home Monitoring",
"version": "2024.3.1",
"project_urls": {
"Bug Tracker": "https://github.com/bachya/aionotion/issues",
"Changelog": "https://github.com/bachya/aionotion/releases",
"Homepage": "https://github.com/bachya/aionotion",
"Repository": "https://github.com/bachya/aionotion"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "849083a44c8634179ad9f85313056844e9df0d3be53aadb016c4260ab279f0a9",
"md5": "54ca2ed16c4a40b852a61a0ea60cff67",
"sha256": "97024184d51737768f7c086380e1d20e630a31adee06ae56fe67e49030a71f99"
},
"downloads": -1,
"filename": "aionotion-2024.3.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "54ca2ed16c4a40b852a61a0ea60cff67",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 17034,
"upload_time": "2024-03-13T01:48:09",
"upload_time_iso_8601": "2024-03-13T01:48:09.820606Z",
"url": "https://files.pythonhosted.org/packages/84/90/83a44c8634179ad9f85313056844e9df0d3be53aadb016c4260ab279f0a9/aionotion-2024.3.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9eeb6b7ee05789a64dfaaecf3d4aa94dbdd6cf7681ed7d9daa6b005c843b64c8",
"md5": "9ac8d41d64d2cf2913d78ceaf565c598",
"sha256": "a2166865735ce624569ad0829571e83f682996f145433d53b2be3d5a67dc8618"
},
"downloads": -1,
"filename": "aionotion-2024.3.1.tar.gz",
"has_sig": false,
"md5_digest": "9ac8d41d64d2cf2913d78ceaf565c598",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 14377,
"upload_time": "2024-03-13T01:48:11",
"upload_time_iso_8601": "2024-03-13T01:48:11.855469Z",
"url": "https://files.pythonhosted.org/packages/9e/eb/6b7ee05789a64dfaaecf3d4aa94dbdd6cf7681ed7d9daa6b005c843b64c8/aionotion-2024.3.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-13 01:48:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bachya",
"github_project": "aionotion",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "aionotion"
}