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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

## Python wrapper for Homeassistant's [REST API](https://developers.home-assistant.io/docs/api/rest/)

Please ⭐️ the repo if you find this project useful or cool!

Here is a quick example.
```py
from homeassistant_api import Client
Expand Down
28 changes: 15 additions & 13 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Code Reference
***************

Clients
Client
--------

.. autoclass:: homeassistant_api.Client
Expand All @@ -14,33 +14,35 @@ Data Models

.. automodule:: homeassistant_api.models

.. autoclass:: Domain
.. autopydantic_model:: Domain

.. autoclass:: Service
.. autopydantic_model:: Service

.. autoclass:: Group
.. autopydantic_model:: Group

.. autoclass:: Entity
.. autopydantic_model:: Entity

.. autoclass:: History
.. autopydantic_model:: History

.. autoclass:: State
.. autopydantic_model:: LogbookEntry

.. autoclass:: Event
.. autopydantic_model:: State

.. autopydantic_model:: Event



.. automodule:: homeassistant_api._async.models

.. autoclass:: AsyncDomain
.. autopydantic_model:: AsyncDomain

.. autoclass:: AsyncService
.. autopydantic_model:: AsyncService

.. autoclass:: AsyncGroup
.. autopydantic_model:: AsyncGroup

.. autoclass:: AsyncEntity
.. autopydantic_model:: AsyncEntity

.. autoclass:: AsyncEvent
.. autopydantic_model:: AsyncEvent


Processing
Expand Down
7 changes: 6 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
else "v" + version
)

extensions = ["sphinx.ext.autodoc", "resourcelinks", "sphinx_autodoc_typehints"]
extensions = [
"sphinx.ext.autodoc",
"resourcelinks",
"sphinx_autodoc_typehints",
"sphinxcontrib.autodoc_pydantic",
]

resource_links = {
"repo": "https://github.com/GrandMoff100/HomeassistantAPI/",
Expand Down
5 changes: 4 additions & 1 deletion docs/doc_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ appdirs
astroid
async-timeout
attrs
autodoc-pydantic
babel
black
brotli
Expand All @@ -25,6 +26,7 @@ frozenlist
identify
idna
imagesize
importlib-metadata
iniconfig
isort
itsdangerous
Expand Down Expand Up @@ -80,4 +82,5 @@ url-normalize
urllib3
virtualenv
wrapt
yarl
yarl
zipp
33 changes: 29 additions & 4 deletions homeassistant_api/_async/models/domains.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
"""File for Service and Domain data models"""

from typing import Any, Dict, Tuple, cast
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, cast

from ...models import Domain, Service, State
from pydantic import BaseModel, Field

from ...models import State
from ...models.domains import ServiceField

class AsyncDomain(Domain):
if TYPE_CHECKING:
from homeassistant_api import Client


class AsyncDomain(BaseModel):
"""A class representing the domain that services belong to."""

domain_id: str
client: "Client" = Field(exclude=True, repr=False)
services: Dict[str, "AsyncService"] = {}

def add_service(self, service_id: str, **data) -> None:
"""Registers services into a domain to be used or accessed"""
self.services.update(
Expand All @@ -24,10 +34,25 @@ def get_service(self, service_id: str):
"""Return a Service with the given service_id, returns None if no such service exists"""
return self.services.get(service_id, None)

def __getattr__(self, attr: str):
"""Allows services accessible as attributes"""
if attr in self.__dict__:
return super().__getattribute__(attr)
if attr in self.services:
return self.get_service(attr)
return super().__getattribute__(attr)

class AsyncService(Service):

class AsyncService(BaseModel):
"""Class representing services from homeassistant"""

service_id: str
domain: AsyncDomain
name: Optional[str] = None
description: Optional[str] = None
fields: Optional[Dict[str, ServiceField]] = None
target: Optional[Dict[str, dict]] = None

async def async_trigger(self, **service_data) -> Tuple[State, ...]:
"""Triggers the service associated with this object."""
data = await self.domain.client.async_trigger_service(
Expand Down
1 change: 0 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

Loading