Skip to content

Commit 045dc70

Browse files
committed
Update documentation and docstrings for v2 API surface
- Rewrite usage.rst async section: replace use_async=True/async_ prefix pattern with separate AsyncClient and AsyncWebsocketClient classes - Fix advanced.rst: update caching params (cache_session -> session), remove obsolete "disabling caching" section, add use_cache docs - Fix README.md: update async example to use AsyncClient, remove "async not supported" note for WebSocket - Standardize docstrings across all four client classes: consistent voice, correct class references (Entity->AsyncEntity etc.), fix "homeassistant" -> "Home Assistant" naming - Fix minor issues: typos, Python version (3.9->3.11), zuban in CONTRIBUTING.rst tooling list
1 parent 19ff068 commit 045dc70

9 files changed

Lines changed: 171 additions & 167 deletions

File tree

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,22 @@ with Client(
2525
'<API Server URL>', # i.e. 'http://homeassistant.local:8123/api/'
2626
'<Your Long Lived Access-Token>'
2727
) as client:
28-
light = client.trigger_service('light', 'turn_on', entity_id="light.living_room")
28+
client.trigger_service('light', 'turn_on', entity_id="light.living_room")
2929
```
3030

31-
All the methods also support async/await!
32-
Just prefix the method with `async_` and pass the `use_async=True` argument to the `Client` constructor.
33-
Then you can use the methods as coroutines
34-
(i.e. `await light.async_turn_on(...)`).
31+
All four client classes share the same method names.
32+
The async clients (`AsyncClient`, `AsyncWebsocketClient`) use `async def` methods that you `await`.
3533

3634
```py
3735
import asyncio
38-
from homeassistant_api import Client
36+
from homeassistant_api import AsyncClient
3937

4038
async def main():
41-
with Client(
39+
async with AsyncClient(
4240
'<REST API Server URL>', # i.e. 'http://homeassistant.local:8123/api/'
4341
'<Your Long Lived Access-Token>',
44-
use_async=True
4542
) as client:
46-
light = await client.async_trigger_service('light', 'turn_on', entity_id="light.living_room")
43+
await client.trigger_service('light', 'turn_on', entity_id="light.living_room")
4744

4845
asyncio.run(main())
4946
```
@@ -57,11 +54,9 @@ with WebsocketClient(
5754
'<WS API Server URL>', # i.e. 'ws://homeassistant.local:8123/api/websocket'
5855
'<Your Long Lived Access-Token>'
5956
) as ws_client:
60-
light = ws_client.trigger_service('light', 'turn_on', entity_id="light.living_room")
57+
ws_client.trigger_service('light', 'turn_on', entity_id="light.living_room")
6158
```
6259

63-
> Note: The Websocket API is not yet supported in async/await mode.
64-
6560
## Documentation
6661

6762
All documentation, API reference, contribution guidelines and pretty much everything else

docs/CONTRIBUTING.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Next run in your terminal.
3737
Step Three: Installing Dependencies
3838
======================================
3939

40-
Firstly, you need to have Python 3.9 or newer installed.
40+
Firstly, you need to have Python 3.11 or newer installed.
4141
Download the latest Python Version from `here <https://www.python.org/>`__.
4242
Then you need to install :code:`uv`, a fast Python package manager.
4343
Checkout the `uv Docs <https://docs.astral.sh/uv/>`__.
@@ -69,7 +69,7 @@ Code Styling Guidelines
6969
In order to make sure that our code is easy to read, and navigate.
7070
As well as to stop stupid mistakes like typos, undefined variables, etc.
7171
We enforce code standards.
72-
Using the tools, :code:`ruff`, :code:`pytest`, and :code:`docker`, we make make sure that our code quality is top notch, and that are changes work everywhere.
72+
Using the tools, :code:`ruff`, :code:`zuban`, :code:`pytest`, and :code:`docker`, we make make sure that our code quality is top notch, and that are changes work everywhere.
7373
You can those tools manually yourself, but they also run automatically when you open a PR.
7474

7575
Merging Your Contributions

docs/advanced.rst

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,74 @@
22
Advanced Section
33
*******************
44

5-
Persistent Caching
6-
********************
5+
Caching
6+
**********
7+
8+
By default, caching is **disabled**. You can enable the built-in in-memory cache by passing :code:`use_cache=True`:
9+
10+
.. code-block:: python
711
8-
Persistent caching is exactly what it means. It makes your requests cache persist or stay around between :py:class:`Client` objects, and between runs, and contexts (:code:`with client:` statements).
9-
Rather than the default behavior, which is saving the cache to memory or not at all and erasing it after each context and run.
12+
from homeassistant_api import Client
1013
14+
client = Client("<API_URL>", "<TOKEN>", use_cache=True)
1115
12-
If you want to persist your requests cache you can pass your own custom cached session to :py:class:`Client`'s init method.
13-
You can pass a variety of options to your cached session like how fast to expire the cache, where to cache it (the cache backend), and what to do when the cache is expired.
16+
This creates an in-memory cache that expires after 300 seconds.
1417

15-
Depending on whether you are using this in an async or sync project you will want to use either :py:class:`aiohttp_client_cache.backends.CachedSession` or :py:class:`requests_cache.CachedSession` respectively.
16-
See the docs for `requests_cache <https://requests-cache.readthedocs.io/en/latest/>`__ and `aiohttp_client_cache <https://aiohttp-client-cache.readthedocs.io/en/latest/>`__ for how to implement these backends, options, and much more.
18+
Persistent Caching
19+
********************
1720

18-
You can simply pass them to your client like so.
21+
If you want your cache to persist between runs (e.g. to a filesystem), you can pass your own custom cached session via the :code:`session` parameter.
22+
23+
Depending on whether you are using a sync or async client you will want to use either :py:class:`requests_cache.CachedSession` or :py:class:`aiohttp_client_cache.session.CachedSession` respectively.
24+
See the docs for `requests_cache <https://requests-cache.readthedocs.io/en/latest/>`__ and `aiohttp_client_cache <https://aiohttp-client-cache.readthedocs.io/en/latest/>`__ for backend options and more.
1925

2026
.. code-block:: python
2127
28+
from datetime import timedelta
2229
from homeassistant_api import Client
2330
from requests_cache import CachedSession
2431
2532
client = Client(
2633
"<API_URL>",
2734
"<TOKEN>",
28-
cache_session=CachedSession(
35+
session=CachedSession(
2936
backend="filesystem",
30-
expire_after=timedelta(minutes=5)
31-
)
37+
expire_after=timedelta(minutes=5),
38+
),
3239
)
3340
34-
# CachedSession is activated by the `with` statement.
3541
with client:
3642
# Grab and update some cool entities and services inside your installation.
3743
...
3844
45+
.. code-block:: python
46+
3947
# Or an example for async
4048
import asyncio
41-
from homeassistant_api import Client
49+
from datetime import timedelta
50+
from homeassistant_api import AsyncClient
4251
from aiohttp_client_cache import CachedSession, FileBackend
4352
44-
client = Client(
45-
"<URL>",
53+
client = AsyncClient(
54+
"<API_URL>",
4655
"<TOKEN>",
47-
cache_session=CachedSession(
56+
session=CachedSession(
4857
cache=FileBackend(
49-
expire_after=timedelta(minutes=5)
50-
)
58+
expire_after=timedelta(minutes=5),
59+
),
5160
),
52-
use_async=True
5361
)
62+
5463
async def main():
5564
async with client:
5665
# Grab and update some cool entities and services inside your installation.
5766
...
67+
5868
asyncio.run(main())
5969
6070
6171
Why the heck is :py:class:`Client` a context manager?
6272
********************************************************
6373

64-
The :py:class:`Client` is a context manager because it activates the cache session and pings Home Assistant to make sure its running.
65-
You might not want this behavior, if you don't then don't use the :code:`with` or :code:`async with` statement.
66-
You can still use the client without it, but you will have to manually activate the cache session before you use it.
67-
68-
Disabling Caching
69-
******************
70-
71-
To explicitly disable the default cache you can pass :code:`cache_session=False` or :code:`async_cache_session=False` to :py:class:`Client`'s init method depending on your use case.
72-
Otherwise the default cache will be used by default when you use :code:`with client:` or :code:`async with client:`.
74+
The :py:class:`Client` is a context manager because it manages the underlying HTTP session and pings Home Assistant to make sure it's running.
75+
You don't have to use the context manager — the client works without it — but you'll need to manage the session lifecycle yourself.

docs/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Features
3030
----------
3131

3232
- Full consumption of the Home Assistant REST API endpoints.
33-
- Full consumption of the Home Assistant Websocket API (all of the documented commands and some undocumented ones)
33+
- Full consumption of the Home Assistant Websocket API (all of the documented commands and some undocumented ones).
3434
- Convenient Pydantic Models for data validation.
35-
- Syncrononous and Asynchronous support for integrating with all applications and/or libraries.
35+
- Synchronous and asynchronous support for both REST and WebSocket clients.
3636
- Modular design for intuitive readability.
37-
- Request caching for more efficient repeative requests.
37+
- Request caching for more efficient repetitive requests.
3838

3939
Getting Started
4040
-------------------

0 commit comments

Comments
 (0)