Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,34 @@ You can simply pass them to your client like so.
from homeassistant_api import Client
from aiohttp_client_cache import CachedSession, FileBackend

client = Client("<URL>", "<TOKEN>", cache_session=CachedSession(cache=FileBackend(), expire_after=timedelta(minutes=5)))
# CachedSession is activated by the `async with` statement.
client = Client(
"<URL>",
"<TOKEN>",
cache_session=CachedSession(cache=FileBackend(),
expire_after=timedelta(minutes=5)),
use_async=True
)
async def main():
async with client:
# Grab and update some cool entities and services inside your installation.
...
asyncio.run(main())


Why the heck is :py:class:`Client` a context manager?
********************************************************

The :py:class:`Client` is a context manager because it activates the cache session and pings Home Assistant to make sure its running.
You might not want this behavior, if you don't then don't use the :code:`with` or :code:`async with` statement.
You can still use the client without it, but you will have to manually activate the cache session before you use it.

Disabling Caching
******************

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.
Otherwise the default cache will be used by default when you use :code:`with client:` or :code:`async with client:`.


Response Processing
**********************
Home Assistant API uses functions called processors.
Expand All @@ -58,7 +77,7 @@ These functions take a Response object as a parameter and return the python data
How To Register Response Processors (Converters)
==================================================

To register a response processor you need to import the Processing class and then implement the decorator.
To register a response processor you need to import the :py:class:`Processing` class and then implement the decorator.


.. code-block:: python
Expand Down
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ Code Reference

.. automodule:: homeassistant_api
:platform: Linux, Windows, MacOS
:inherited-members:
:exclude-members: construct, copy, dict, from_orm, json, parse_file, parse_obj, parse_raw, parse_str, parse_url, schema, schema_json, schema_yaml, schema_yml, to_orm, update_forward_refs, validate, validate_file, validate_obj, validate_raw, validate_str, validate_url


9 changes: 7 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ The most commonly used features of this library include triggering services and
client = Client("https://foobarhomeassistant.duckdns.org:8123/api", "mylongtokenpasswordthingyfoobar")

# In order to activate the requests session you to use the Client context manager like so.
# Using it as a context manager will automatically close the session when you're done with it.
# But also will *ping* your Home Assistant instance to make sure it's running.
with client:
while True:
sun = client.get_entity(entity_id="sun.sun")
Expand Down Expand Up @@ -106,8 +108,8 @@ Async Services
import asyncio
from homeassistant_api import Client

# Initialize client like usual
client = Client(url, token)
# Initialize client like usual, except with the :code:`use_async` keyword.
client = Client(url, token, use_async=True)

async def main():

Expand Down Expand Up @@ -157,6 +159,9 @@ Async Entities
# <State "My new state" entity_id="cover.garage_door">





What's Next?
#############

Expand Down
4 changes: 2 additions & 2 deletions homeassistant_api/rawclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def get_entity(

# Services and domain methods
def get_domains(self) -> Dict[str, Domain]:
"""Fetches all :py:class:`Service`s from the API."""
"""Fetches all :py:class:`Service` 's from the API."""
data = self.request("services")
domains = map(
lambda json: Domain.from_json(json, client=cast(Client, self)),
Expand All @@ -245,7 +245,7 @@ def get_domains(self) -> Dict[str, Domain]:
return {domain.domain_id: domain for domain in domains}

def get_domain(self, domain_id: str) -> Optional[Domain]:
"""Fetches all services under a particular domain."""
"""Fetches all :py:class:`Service`'s under a particular service :py:class:`Domain`."""
return self.get_domains().get(domain_id)

def trigger_service(
Expand Down