Package: aionotion
📟 aionotion: a Python3, asyncio-friendly library for Notion® Home Monitoringaionotionis a Python 3, asyncio-friendly library for interacting withNotionhome monitoring sensors.InstallationPython VersionsUsageContributingInstallationpipinstallaionotionPython Versionsaionotionis currently supported on:Python 3.10Python 3.11Python 3.12UsageimportasynciofromaiohttpimportClientSessionfromaionotionimportasync_get_client_with_credentialsasyncdefmain()->None:"""Create the aiohttp session and run the example."""client=awaitasync_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=awaitclient.system.async_all()# >>> [System(...), System(...), ...]# Get a system by ID:system=awaitclient.system.async_get(12345)# >>> System(...)# Get all bridges associated with the account:bridges=awaitclient.bridge.async_all()# >>> [Bridge(...), Bridge(...), ...]# Get a bridge by ID:bridge=awaitclient.bridge.async_get(12345)# >>> Bridge(...)# Get all sensors:sensors=awaitclient.sensor.async_all()# >>> [Sensor(...), Sensor(...), ...]# Get a sensor by ID:sensor=awaitclient.sensor.async_get(12345)# >>> Sensor(...)# Get "listeners" (conditions that a sensor is monitoring) for all sensors:listeners=awaitclient.listener.async_all()# >>> [Listener(...), Listener(...), ...]# Get all listener definitions supported by Notion:definitions=awaitclient.listener.async_definitions()# >>> [ListenerDefinition(...), ListenerDefinition(...), ...]# Get user info:user_info=awaitclient.user.async_info()# >>> User(...)# Get user preferences:user_preferences=awaitclient.user.async_preferences()# >>> UserPreferences(...)asyncio.run(main())Using a Refresh TokenDuring the normal course of operations,aionotionwill 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)–aionotionprovides a few useful capabilities there.Refresh Token Callbacksaionotionallows implementers to defining callbacks that get called when a new refresh
token is generated. These callbacks accept a single string parameter (the refresh
token):importasynciofromaiohttpimportClientSessionfromaionotionimportasync_get_client_with_credentialsasyncdefmain()->None:"""Create the aiohttp session and run the example."""client=awaitasync_get_client_with_credentials("<EMAIL>","<PASSWORD>",session=session)defdo_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 TokenAll of previous examples retrieved an authenticated client withasync_get_client_with_credentials. However, implementers may also create an
authenticated client by providing a previously retrieved user UUID and refresh token:importasynciofromaiohttpimportClientSessionfromaionotionimportasync_get_client_with_refresh_tokenasyncdefmain()->None:"""Create the aiohttp session and run the example."""asyncwithClientSession()assession:# Create a Notion API client:client=awaitasync_get_client_with_refresh_token("<USER UUID>","<REFRESH TOKEN>",session=session)# Get to work...asyncio.run(main())Connection PoolingBy 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), anaiohttpClientSessioncan be used for
connection pooling:importasynciofromaiohttpimportClientSessionfromaionotionimportasync_get_client_with_credentialsasyncdefmain()->None:"""Create the aiohttp session and run the example."""asyncwithClientSession()assession:# Create a Notion API client:client=awaitasync_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.ContributingThanks to all ofour contributorsso far!Check for open features/bugsorinitiate a discussion on one.Fork the repository.(optional, but highly recommended) Create a virtual environment:python3 -m venv .venv(optional, but highly recommended) Enter the virtual environment:source ./.venv/bin/activateInstall the dev environment:script/setupCode your new feature or bug fix on a new branch.Write tests that cover your new functionality.Run tests and ensure 100% code coverage:poetry run pytest --cov aionotion testsUpdateREADME.mdwith any new documentation.Submit a pull request!