HomeAssistant-API 3.0.0__tar.gz → 3.0.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/PKG-INFO +19 -16
- HomeAssistant-API-3.0.3/README.md +39 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/asyncclient.py +8 -24
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/domains.py +29 -4
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/entity.py +3 -1
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/events.py +3 -1
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/mixins.py +3 -3
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/models/__init__.py +1 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/models/domains.py +3 -1
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/models/entity.py +3 -1
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/models/events.py +3 -1
- HomeAssistant-API-3.0.3/homeassistant_api/models/logbook.py +17 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/processing.py +5 -6
- HomeAssistant-API-3.0.3/homeassistant_api/py.typed +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/rawapi.py +37 -17
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/rawclient.py +10 -25
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/pyproject.toml +33 -41
- HomeAssistant-API-3.0.3/setup.py +39 -0
- HomeAssistant-API-3.0.0/README.md +0 -36
- HomeAssistant-API-3.0.0/setup.py +0 -38
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/LICENSE +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/__init__.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/__init__.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/__init__.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/client.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/const.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/errors.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/models/base.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/models/history.py +0 -0
- {HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/models/states.py +0 -0
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: homeassistant-api
|
|
3
|
-
Version: 3.0.
|
|
3
|
+
Version: 3.0.3
|
|
4
4
|
Summary: Python Wrapper for Homeassistant's REST API
|
|
5
5
|
Home-page: https://github.com/GrandMoff100/HomeAssistantAPI
|
|
6
6
|
License: GPL-3.0-or-later
|
|
7
7
|
Author: GrandMoff100
|
|
8
8
|
Author-email: minecraftcrusher100@gmail.com
|
|
9
|
-
Requires-Python: >=3.
|
|
9
|
+
Requires-Python: >=3.8,<4.0
|
|
10
10
|
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
|
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
14
13
|
Classifier: Programming Language :: Python :: 3.8
|
|
15
14
|
Classifier: Programming Language :: Python :: 3.9
|
|
16
15
|
Requires-Dist: aiohttp (>=3.8.1,<4.0.0)
|
|
17
16
|
Requires-Dist: aiohttp-client-cache (>=0.6.1,<0.7.0)
|
|
17
|
+
Requires-Dist: autodoc-pydantic (>=1.6.1,<2.0.0)
|
|
18
18
|
Requires-Dist: pydantic (>=1.9.0,<2.0.0)
|
|
19
19
|
Requires-Dist: requests (>=2.26.0,<3.0.0)
|
|
20
20
|
Requires-Dist: requests-cache (>=0.9.2,<0.10.0)
|
|
@@ -25,19 +25,14 @@ Description-Content-Type: text/markdown
|
|
|
25
25
|
|
|
26
26
|
# HomeassistantAPI
|
|
27
27
|
|
|
28
|
-

|
|
28
|
+
[](https://pypi.org/project/homeassistant_api)
|
|
30
29
|

|
|
31
|
-
](https://img.shields.io/github/v/release/GrandMoff100/HomeassistantAPI?style=for-the-badge)
|
|
33
|
-

|
|
30
|
+
[](https://homeassistantapi.readthedocs.io/en/latest/?badge=latest)
|
|
31
|
+
[](https://github.com/GrandMoff100/HomeassistantAPI/releases)
|
|
34
32
|
|
|
35
|
-
](https://home-assistant.io)
|
|
36
34
|
|
|
37
|
-
Python
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Please ⭐️ the repo if you find this project useful or cool!
|
|
35
|
+
## Python wrapper for Homeassistant's [REST API](https://developers.home-assistant.io/docs/api/rest/)
|
|
41
36
|
|
|
42
37
|
Here is a quick example.
|
|
43
38
|
```py
|
|
@@ -52,11 +47,19 @@ with Client(
|
|
|
52
47
|
|
|
53
48
|
light.turn_on(entity_id='light.living_room_lamp')
|
|
54
49
|
```
|
|
50
|
+
All the methods also support async!
|
|
55
51
|
|
|
56
52
|
## Documentation
|
|
57
|
-
All documentation, API reference,
|
|
53
|
+
All documentation, API reference, contribution guidelines and pretty much everything else
|
|
54
|
+
you'd want to know is on our readthedocs site [here](https://homeassistantapi.readthedocs.io)
|
|
55
|
+
|
|
56
|
+
If there is something missing, open an issue and let us know! Thanks!
|
|
57
|
+
|
|
58
|
+
Go make some cool stuff! Maybe come back and tell us about it in a
|
|
59
|
+
[discussion](https://github.com/GrandMoff100/HomeAssistantAPI/discussions)?
|
|
60
|
+
We'd love to hear about how you use our library!!
|
|
58
61
|
|
|
59
|
-
|
|
62
|
+
## License
|
|
60
63
|
|
|
61
|
-
|
|
64
|
+
This project is under the GNU GPLv3 license, as defined by the Free Software Foundation.
|
|
62
65
|
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# HomeassistantAPI
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/homeassistant_api)
|
|
4
|
+

|
|
5
|
+
[](https://homeassistantapi.readthedocs.io/en/latest/?badge=latest)
|
|
6
|
+
[](https://github.com/GrandMoff100/HomeassistantAPI/releases)
|
|
7
|
+
|
|
8
|
+
[](https://home-assistant.io)
|
|
9
|
+
|
|
10
|
+
## Python wrapper for Homeassistant's [REST API](https://developers.home-assistant.io/docs/api/rest/)
|
|
11
|
+
|
|
12
|
+
Here is a quick example.
|
|
13
|
+
```py
|
|
14
|
+
from homeassistant_api import Client
|
|
15
|
+
|
|
16
|
+
with Client(
|
|
17
|
+
'<API Server URL>',
|
|
18
|
+
'<Your Long Lived Access-Token>'
|
|
19
|
+
) as client:
|
|
20
|
+
|
|
21
|
+
light = client.get_domain("light")
|
|
22
|
+
|
|
23
|
+
light.turn_on(entity_id='light.living_room_lamp')
|
|
24
|
+
```
|
|
25
|
+
All the methods also support async!
|
|
26
|
+
|
|
27
|
+
## Documentation
|
|
28
|
+
All documentation, API reference, contribution guidelines and pretty much everything else
|
|
29
|
+
you'd want to know is on our readthedocs site [here](https://homeassistantapi.readthedocs.io)
|
|
30
|
+
|
|
31
|
+
If there is something missing, open an issue and let us know! Thanks!
|
|
32
|
+
|
|
33
|
+
Go make some cool stuff! Maybe come back and tell us about it in a
|
|
34
|
+
[discussion](https://github.com/GrandMoff100/HomeAssistantAPI/discussions)?
|
|
35
|
+
We'd love to hear about how you use our library!!
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
This project is under the GNU GPLv3 license, as defined by the Free Software Foundation.
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
"""Module for interacting with Home Assistant asyncronously."""
|
|
2
2
|
import asyncio
|
|
3
|
-
from datetime import datetime
|
|
4
3
|
from os.path import join
|
|
5
4
|
from typing import Any, AsyncGenerator, Dict, List, Optional, Tuple, Union, cast
|
|
6
5
|
|
|
7
6
|
import aiohttp
|
|
8
7
|
from aiohttp_client_cache import CachedSession
|
|
9
8
|
|
|
10
|
-
from ..const import DATE_FMT
|
|
11
9
|
from ..errors import APIConfigurationError, MalformedDataError, RequestError
|
|
12
10
|
from ..mixins import JsonProcessingMixin
|
|
13
|
-
from ..models import Domain, Event, History, State
|
|
11
|
+
from ..models import Domain, Event, History, LogbookEntry, State
|
|
14
12
|
from ..processing import Processing
|
|
15
13
|
from ..rawapi import RawWrapper
|
|
16
14
|
from .models import AsyncEntity, AsyncGroup
|
|
@@ -85,28 +83,14 @@ class RawAsyncClient(RawWrapper, JsonProcessingMixin):
|
|
|
85
83
|
|
|
86
84
|
async def async_logbook_entries(
|
|
87
85
|
self,
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
) -> Tuple[Dict[str, Any], ...]:
|
|
86
|
+
*args,
|
|
87
|
+
**kwargs,
|
|
88
|
+
) -> AsyncGenerator[LogbookEntry, None]:
|
|
92
89
|
"""Returns a list of logbook entries from homeassistant."""
|
|
93
|
-
params
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if isinstance(end_timestamp, datetime):
|
|
98
|
-
end_timestamp = end_timestamp.strftime(DATE_FMT)
|
|
99
|
-
params.update(end_time=end_timestamp)
|
|
100
|
-
if start_timestamp is not None:
|
|
101
|
-
if isinstance(start_timestamp, datetime):
|
|
102
|
-
formatted_timestamp = start_timestamp.strftime(DATE_FMT)
|
|
103
|
-
url = join("logbook", formatted_timestamp)
|
|
104
|
-
else:
|
|
105
|
-
url = "logbook"
|
|
106
|
-
return cast(
|
|
107
|
-
Tuple[Dict[str, Any], ...],
|
|
108
|
-
await self.async_request(url, params=params),
|
|
109
|
-
)
|
|
90
|
+
params, url = self.prepare_get_logbook_entry_params(*args, **kwargs)
|
|
91
|
+
data = await self.async_request(url, params=params)
|
|
92
|
+
for entry in data:
|
|
93
|
+
yield LogbookEntry.parse_obj(entry)
|
|
110
94
|
|
|
111
95
|
async def async_get_entity_histories(
|
|
112
96
|
self,
|
{HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/domains.py
RENAMED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
"""File for Service and Domain data models"""
|
|
2
2
|
|
|
3
|
-
from typing import Any, Dict, Tuple, cast
|
|
3
|
+
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, cast
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
6
|
|
|
7
|
+
from ...models import State
|
|
8
|
+
from ...models.domains import ServiceField
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from homeassistant_api import Client
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class AsyncDomain(BaseModel):
|
|
9
15
|
"""A class representing the domain that services belong to."""
|
|
10
16
|
|
|
17
|
+
domain_id: str
|
|
18
|
+
client: "Client" = Field(exclude=True, repr=False)
|
|
19
|
+
services: Dict[str, "AsyncService"] = {}
|
|
20
|
+
|
|
11
21
|
def add_service(self, service_id: str, **data) -> None:
|
|
12
22
|
"""Registers services into a domain to be used or accessed"""
|
|
13
23
|
self.services.update(
|
|
@@ -24,10 +34,25 @@ class AsyncDomain(Domain):
|
|
|
24
34
|
"""Return a Service with the given service_id, returns None if no such service exists"""
|
|
25
35
|
return self.services.get(service_id, None)
|
|
26
36
|
|
|
37
|
+
def __getattr__(self, attr: str):
|
|
38
|
+
"""Allows services accessible as attributes"""
|
|
39
|
+
if attr in self.__dict__:
|
|
40
|
+
return super().__getattribute__(attr)
|
|
41
|
+
if attr in self.services:
|
|
42
|
+
return self.get_service(attr)
|
|
43
|
+
return super().__getattribute__(attr)
|
|
27
44
|
|
|
28
|
-
|
|
45
|
+
|
|
46
|
+
class AsyncService(BaseModel):
|
|
29
47
|
"""Class representing services from homeassistant"""
|
|
30
48
|
|
|
49
|
+
service_id: str
|
|
50
|
+
domain: AsyncDomain
|
|
51
|
+
name: Optional[str] = None
|
|
52
|
+
description: Optional[str] = None
|
|
53
|
+
fields: Optional[Dict[str, ServiceField]] = None
|
|
54
|
+
target: Optional[Dict[str, dict]] = None
|
|
55
|
+
|
|
31
56
|
async def async_trigger(self, **service_data) -> Tuple[State, ...]:
|
|
32
57
|
"""Triggers the service associated with this object."""
|
|
33
58
|
data = await self.domain.client.async_trigger_service(
|
{HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/entity.py
RENAMED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
from os.path import join
|
|
3
3
|
from typing import TYPE_CHECKING, Any, Dict, Optional, cast
|
|
4
4
|
|
|
5
|
+
from pydantic import Field
|
|
6
|
+
|
|
5
7
|
from ...models import BaseModel, History, State
|
|
6
8
|
|
|
7
9
|
if TYPE_CHECKING:
|
|
@@ -12,7 +14,7 @@ class AsyncGroup(BaseModel):
|
|
|
12
14
|
"""Represents the groups that entities belong to"""
|
|
13
15
|
|
|
14
16
|
group_id: str
|
|
15
|
-
client: "Client"
|
|
17
|
+
client: "Client" = Field(exclude=True, repr=False)
|
|
16
18
|
entities: Dict[str, "AsyncEntity"] = {}
|
|
17
19
|
|
|
18
20
|
def add_entity(self, entity_slug: str, state: State) -> None:
|
{HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/events.py
RENAMED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""Event Model File"""
|
|
2
2
|
from typing import TYPE_CHECKING, Dict, cast
|
|
3
3
|
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
|
|
4
6
|
from ...models import BaseModel
|
|
5
7
|
|
|
6
8
|
if TYPE_CHECKING:
|
|
@@ -17,7 +19,7 @@ class AsyncEvent(BaseModel):
|
|
|
17
19
|
|
|
18
20
|
event_type: str
|
|
19
21
|
listener_count: int
|
|
20
|
-
client: "Client"
|
|
22
|
+
client: "Client" = Field(exclude=True, repr=False)
|
|
21
23
|
|
|
22
24
|
async def async_fire(self, **event_data) -> str:
|
|
23
25
|
"""Fires the event type in homeassistant. Ex. `on_startup`"""
|
|
@@ -21,7 +21,7 @@ class JsonProcessingMixin:
|
|
|
21
21
|
@staticmethod
|
|
22
22
|
def process_state_json(json: Dict[str, Any]) -> State:
|
|
23
23
|
"""Constructs State model from json data"""
|
|
24
|
-
return State(
|
|
24
|
+
return State.parse_obj(json)
|
|
25
25
|
|
|
26
26
|
def process_event_json(self, json: Dict[str, Any]) -> Event:
|
|
27
27
|
"""Constructs Event model from json data"""
|
|
@@ -43,11 +43,11 @@ class JsonProcessingMixin:
|
|
|
43
43
|
@staticmethod
|
|
44
44
|
async def async_process_state_json(json: Dict[str, Any]) -> State:
|
|
45
45
|
"""Constructs State model from json data"""
|
|
46
|
-
return State(
|
|
46
|
+
return State.parse_obj(json)
|
|
47
47
|
|
|
48
48
|
async def async_process_event_json(
|
|
49
49
|
self,
|
|
50
50
|
json: Dict[str, Any],
|
|
51
51
|
) -> AsyncEvent:
|
|
52
|
-
"""Constructs Event model from json data"""
|
|
52
|
+
"""Constructs Event model from json data."""
|
|
53
53
|
return AsyncEvent(**json, client=self)
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""File for Service and Domain data models"""
|
|
2
2
|
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
|
|
3
3
|
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
|
|
4
6
|
from .base import BaseModel
|
|
5
7
|
from .states import State
|
|
6
8
|
|
|
@@ -12,7 +14,7 @@ class Domain(BaseModel):
|
|
|
12
14
|
"""Model representing the domain that services belong to."""
|
|
13
15
|
|
|
14
16
|
domain_id: str
|
|
15
|
-
client: "Client"
|
|
17
|
+
client: "Client" = Field(exclude=True, repr=False)
|
|
16
18
|
services: Dict[str, "Service"] = {}
|
|
17
19
|
|
|
18
20
|
def add_service(self, service_id: str, **data) -> None:
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
from os.path import join
|
|
4
4
|
from typing import TYPE_CHECKING, Any, Dict, Optional, cast
|
|
5
5
|
|
|
6
|
+
from pydantic import Field
|
|
7
|
+
|
|
6
8
|
from .base import BaseModel
|
|
7
9
|
from .history import History
|
|
8
10
|
from .states import State
|
|
@@ -15,7 +17,7 @@ class Group(BaseModel):
|
|
|
15
17
|
"""Represents the groups that entities belong to."""
|
|
16
18
|
|
|
17
19
|
group_id: str
|
|
18
|
-
client: "Client"
|
|
20
|
+
client: "Client" = Field(exclude=True, repr=False)
|
|
19
21
|
entities: Dict[str, "Entity"] = {}
|
|
20
22
|
|
|
21
23
|
def add_entity(self, entity_slug: str, state: State) -> None:
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from typing import TYPE_CHECKING, Dict, cast
|
|
4
4
|
|
|
5
|
+
from pydantic import Field
|
|
6
|
+
|
|
5
7
|
from .base import BaseModel
|
|
6
8
|
|
|
7
9
|
if TYPE_CHECKING:
|
|
@@ -18,7 +20,7 @@ class Event(BaseModel):
|
|
|
18
20
|
|
|
19
21
|
event_type: str
|
|
20
22
|
listener_count: int
|
|
21
|
-
client: "Client"
|
|
23
|
+
client: "Client" = Field(exclude=True, repr=False)
|
|
22
24
|
|
|
23
25
|
def fire(self, **event_data) -> str:
|
|
24
26
|
"""Fires the corresponding event in Home Assistant."""
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Module for the Logbook Entry model."""
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from .base import BaseModel
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class LogbookEntry(BaseModel):
|
|
9
|
+
"""Model representing"""
|
|
10
|
+
|
|
11
|
+
when: datetime
|
|
12
|
+
name: str
|
|
13
|
+
entity_id: str
|
|
14
|
+
state: Optional[str] = None
|
|
15
|
+
domain: Optional[str] = None
|
|
16
|
+
context_id: Optional[str] = None
|
|
17
|
+
icon: Optional[str] = None
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import inspect
|
|
4
4
|
import json
|
|
5
|
-
from typing import Callable, Dict, Tuple, Union
|
|
5
|
+
from typing import Callable, ClassVar, Dict, Tuple, Union
|
|
6
6
|
|
|
7
7
|
import simplejson
|
|
8
8
|
from aiohttp import ClientResponse
|
|
@@ -26,7 +26,7 @@ class Processing(BaseModel):
|
|
|
26
26
|
"""Uses to processor functions to convert json data into common python data types."""
|
|
27
27
|
|
|
28
28
|
response: Union[Response, CachedResponse, ClientResponse, AsyncCachedResponse]
|
|
29
|
-
_processors: Dict[str, Tuple[Callable, ...]] = {}
|
|
29
|
+
_processors: ClassVar[Dict[str, Tuple[Callable, ...]]] = {}
|
|
30
30
|
|
|
31
31
|
@staticmethod
|
|
32
32
|
def processor(mimetype: str):
|
|
@@ -59,11 +59,10 @@ class Processing(BaseModel):
|
|
|
59
59
|
|
|
60
60
|
def process(self):
|
|
61
61
|
"""Validates the http status code before starting to process the repsonse content"""
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
elif _async := isinstance(self.response, AsyncCachedResponse):
|
|
62
|
+
_async = isinstance(self.response, (ClientResponse, AsyncCachedResponse))
|
|
63
|
+
if _async:
|
|
65
64
|
status_code = self.response.status
|
|
66
|
-
elif
|
|
65
|
+
elif isinstance(self.response, Response):
|
|
67
66
|
status_code = self.response.status_code
|
|
68
67
|
else:
|
|
69
68
|
raise ValueError(
|
|
File without changes
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"""Module for parent RawWrapper class"""
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
|
+
import re
|
|
4
5
|
from datetime import datetime
|
|
5
|
-
from typing import Dict, Optional, Tuple
|
|
6
|
+
from typing import Dict, Optional, Tuple, Union
|
|
6
7
|
|
|
7
8
|
from .const import DATE_FMT
|
|
8
|
-
from .errors import MalformedInputError
|
|
9
9
|
from .models import Entity
|
|
10
10
|
|
|
11
11
|
|
|
@@ -26,8 +26,6 @@ class RawWrapper:
|
|
|
26
26
|
) -> None:
|
|
27
27
|
if global_request_kwargs is None:
|
|
28
28
|
global_request_kwargs = {}
|
|
29
|
-
if not self.api_url.endswith("/"):
|
|
30
|
-
self.api_url += "/"
|
|
31
29
|
if cache_backend is None:
|
|
32
30
|
cache_backend = "memory"
|
|
33
31
|
if cache_expire_after is None:
|
|
@@ -39,6 +37,9 @@ class RawWrapper:
|
|
|
39
37
|
self.cache_backend = cache_backend
|
|
40
38
|
self.cache_expire_after = cache_expire_after
|
|
41
39
|
|
|
40
|
+
if not api_url.endswith("/"):
|
|
41
|
+
self.api_url += "/"
|
|
42
|
+
|
|
42
43
|
def endpoint(self, path: str) -> str:
|
|
43
44
|
"""Joins the api base url with a local path to an absolute url"""
|
|
44
45
|
return os.path.join(self.api_url, path)
|
|
@@ -71,16 +72,13 @@ class RawWrapper:
|
|
|
71
72
|
"""Custom method for constructing non-standard query strings"""
|
|
72
73
|
return "&".join([k if v is None else f"{k}={v}" for k, v in params.items()])
|
|
73
74
|
|
|
74
|
-
@
|
|
75
|
-
def
|
|
76
|
-
"""
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
entity_id.lower() == entity_id,
|
|
82
|
-
]
|
|
83
|
-
return True in checks
|
|
75
|
+
@staticmethod
|
|
76
|
+
def format_entity_id(entity_id: str) -> str:
|
|
77
|
+
"""Takes in a string and formats it into valid snake_case."""
|
|
78
|
+
entity_id = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", entity_id)
|
|
79
|
+
entity_id = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", entity_id)
|
|
80
|
+
entity_id = entity_id.replace("-", "_")
|
|
81
|
+
return entity_id.lower()
|
|
84
82
|
|
|
85
83
|
def prepare_entity_id(
|
|
86
84
|
self,
|
|
@@ -99,9 +97,7 @@ class RawWrapper:
|
|
|
99
97
|
entity_id = group + "." + slug
|
|
100
98
|
elif entity_id is None:
|
|
101
99
|
raise ValueError("Neither group and slug or entity_id provided.")
|
|
102
|
-
|
|
103
|
-
raise MalformedInputError(f"The entity_id, {entity_id!r}, is malformed")
|
|
104
|
-
return entity_id
|
|
100
|
+
return self.format_entity_id(entity_id)
|
|
105
101
|
|
|
106
102
|
@staticmethod
|
|
107
103
|
def prepare_get_entity_histories_params(
|
|
@@ -132,3 +128,27 @@ class RawWrapper:
|
|
|
132
128
|
else:
|
|
133
129
|
url = "history/period"
|
|
134
130
|
return params, url
|
|
131
|
+
|
|
132
|
+
@staticmethod
|
|
133
|
+
def prepare_get_logbook_entry_params(
|
|
134
|
+
filter_entity: Optional[Entity] = None,
|
|
135
|
+
start_timestamp: Optional[
|
|
136
|
+
Union[str, datetime]
|
|
137
|
+
] = None, # Defaults to 1 day before
|
|
138
|
+
end_timestamp: Optional[Union[str, datetime]] = None,
|
|
139
|
+
) -> Tuple[Dict[str, str], str]:
|
|
140
|
+
"""Prepares the query string and url path for retrieving logbook entries."""
|
|
141
|
+
params: Dict[str, str] = {}
|
|
142
|
+
if filter_entity is not None:
|
|
143
|
+
params.update(entity=filter_entity.entity_id)
|
|
144
|
+
if end_timestamp is not None:
|
|
145
|
+
if isinstance(end_timestamp, datetime):
|
|
146
|
+
end_timestamp = end_timestamp.strftime(DATE_FMT)
|
|
147
|
+
params.update(end_time=end_timestamp)
|
|
148
|
+
if start_timestamp is not None:
|
|
149
|
+
if isinstance(start_timestamp, datetime):
|
|
150
|
+
formatted_timestamp = start_timestamp.strftime(DATE_FMT)
|
|
151
|
+
url = os.path.join("logbook", formatted_timestamp)
|
|
152
|
+
else:
|
|
153
|
+
url = "logbook"
|
|
154
|
+
return params, url
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
"""Module for all interaction with homeassistant."""
|
|
2
2
|
|
|
3
|
-
from datetime import datetime
|
|
4
3
|
from os.path import join
|
|
5
4
|
from typing import Any, Dict, Generator, List, Optional, Tuple, Union, cast
|
|
6
5
|
|
|
7
6
|
import requests
|
|
8
7
|
from requests_cache import CachedSession
|
|
9
8
|
|
|
10
|
-
from .const import DATE_FMT
|
|
11
9
|
from .errors import APIConfigurationError, RequestError
|
|
12
10
|
from .mixins import JsonProcessingMixin
|
|
13
|
-
from .models import Domain, Entity, Event, Group, History, State
|
|
11
|
+
from .models import Domain, Entity, Event, Group, History, LogbookEntry, State
|
|
14
12
|
from .processing import Processing
|
|
15
13
|
from .rawapi import RawWrapper
|
|
16
14
|
|
|
@@ -87,27 +85,14 @@ class RawClient(RawWrapper, JsonProcessingMixin):
|
|
|
87
85
|
|
|
88
86
|
def logbook_entries(
|
|
89
87
|
self,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
] = None, # Defaults to 1 day before
|
|
94
|
-
end_timestamp: Optional[Union[str, datetime]] = None,
|
|
95
|
-
) -> List[Dict[str, Any]]:
|
|
88
|
+
*args,
|
|
89
|
+
**kwargs,
|
|
90
|
+
) -> Generator[LogbookEntry, None, None]:
|
|
96
91
|
"""Returns a list of logbook entries from homeassistant."""
|
|
97
|
-
params
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if isinstance(end_timestamp, datetime):
|
|
102
|
-
end_timestamp = end_timestamp.strftime(DATE_FMT)
|
|
103
|
-
params.update(end_time=end_timestamp)
|
|
104
|
-
if start_timestamp is not None:
|
|
105
|
-
if isinstance(start_timestamp, datetime):
|
|
106
|
-
formatted_timestamp = start_timestamp.strftime(DATE_FMT)
|
|
107
|
-
url = join("logbook", formatted_timestamp)
|
|
108
|
-
else:
|
|
109
|
-
url = "logbook"
|
|
110
|
-
return cast(List[Dict[str, Any]], self.request(url, params=params))
|
|
92
|
+
params, url = self.prepare_get_logbook_entry_params(*args, **kwargs)
|
|
93
|
+
data = self.request(url, params=params)
|
|
94
|
+
for entry in data:
|
|
95
|
+
yield LogbookEntry.parse_obj(entry)
|
|
111
96
|
|
|
112
97
|
def get_entity_histories(self, *args, **kwargs) -> Generator[History, None, None]:
|
|
113
98
|
"""
|
|
@@ -252,8 +237,8 @@ class RawClient(RawWrapper, JsonProcessingMixin):
|
|
|
252
237
|
**payload,
|
|
253
238
|
) -> State:
|
|
254
239
|
"""
|
|
255
|
-
|
|
256
|
-
|
|
240
|
+
This method sets the representation of a device within Home Assistant and will not communicate with the actual device.
|
|
241
|
+
To communicate with the device, use :py:meth:`homeassistant_api.Service.trigger` or :py:meth:`homeassistant_api.AsyncService.trigger`
|
|
257
242
|
"""
|
|
258
243
|
entity_id = self.prepare_entity_id(
|
|
259
244
|
group=group,
|
|
@@ -1,61 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "poetry.core.masonry.api"
|
|
3
|
+
requires = ["poetry-core>=1.0.0"]
|
|
4
|
+
|
|
5
|
+
[tool]
|
|
6
|
+
[tool.bandit]
|
|
7
|
+
skips = ["B105"]
|
|
8
|
+
[tool.isort]
|
|
9
|
+
profile = "black"
|
|
1
10
|
[tool.poetry]
|
|
2
|
-
name = "HomeAssistant-API"
|
|
3
|
-
version = "3.0.0"
|
|
4
|
-
description = "Python Wrapper for Homeassistant's REST API"
|
|
5
11
|
authors = ["GrandMoff100 <minecraftcrusher100@gmail.com>"]
|
|
12
|
+
description = "Python Wrapper for Homeassistant's REST API"
|
|
13
|
+
documentation = "https://homeassistantapi.readthedocs.io"
|
|
14
|
+
homepage = "https://github.com/GrandMoff100/HomeAssistantAPI"
|
|
6
15
|
license = "GPL-3.0-or-later"
|
|
16
|
+
name = "HomeAssistant-API"
|
|
7
17
|
readme = "README.md"
|
|
8
|
-
|
|
18
|
+
include = ["homeassistant_api/py.typed"]
|
|
9
19
|
repository = "https://github.com/GrandMoff100/HomeAssistantAPI"
|
|
10
|
-
|
|
11
|
-
packages = [
|
|
12
|
-
{ include = "homeassistant_api" },
|
|
13
|
-
]
|
|
14
|
-
|
|
20
|
+
version = "3.0.3"
|
|
15
21
|
[tool.poetry.dependencies]
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
aiohttp = "^3.8.1"
|
|
23
|
+
aiohttp-client-cache = "^0.6.1"
|
|
24
|
+
autodoc-pydantic = "^1.6.1"
|
|
19
25
|
pydantic = "^1.9.0"
|
|
26
|
+
python = "^3.8"
|
|
27
|
+
requests = "^2.26.0"
|
|
20
28
|
requests-cache = "^0.9.2"
|
|
21
|
-
|
|
22
|
-
aiohttp = "^3.8.1"
|
|
23
|
-
|
|
29
|
+
simplejson = "^3.17.6"
|
|
24
30
|
[tool.poetry.dev-dependencies]
|
|
25
|
-
|
|
31
|
+
black = "^21.12b0"
|
|
26
32
|
flake8 = "^4.0.1"
|
|
27
33
|
isort = "^5.10.1"
|
|
28
|
-
|
|
34
|
+
pre-commit = "^2.17.0"
|
|
35
|
+
pylint = "^2.12.2"
|
|
29
36
|
pytest = "^6.2.5"
|
|
37
|
+
sphinx-autodoc-typehints = "^1.17.0"
|
|
38
|
+
sphinx-rtd-theme = "^1.0.0"
|
|
39
|
+
toml = "^0.10.2"
|
|
30
40
|
types-docutils = "^0.17.5"
|
|
31
41
|
types-requests = "^2.27.9"
|
|
32
42
|
types-simplejson = "^3.17.3"
|
|
33
|
-
pylint = "^2.12.2"
|
|
34
|
-
sphinx-rtd-theme = "^1.0.0"
|
|
35
|
-
toml = "^0.10.2"
|
|
36
43
|
types-toml = "^0.10.4"
|
|
37
|
-
|
|
38
|
-
pre-commit = "^2.17.0"
|
|
39
|
-
|
|
40
|
-
[tool.isort]
|
|
41
|
-
profile = "black"
|
|
42
|
-
|
|
43
|
-
[tool.pylint.messages_control]
|
|
44
|
-
disable = [
|
|
45
|
-
"invalid-name",
|
|
46
|
-
"duplicate-code",
|
|
47
|
-
"no-member",
|
|
48
|
-
"too-few-public-methods",
|
|
49
|
-
"too-many-arguments"
|
|
50
|
-
]
|
|
44
|
+
mypy = "^0.931"
|
|
51
45
|
|
|
46
|
+
[[tool.poetry.packages]]
|
|
47
|
+
include = "homeassistant_api"
|
|
48
|
+
[tool.pylint]
|
|
52
49
|
[tool.pylint.master]
|
|
53
50
|
extension-pkg-whitelist = ["pydantic"]
|
|
54
51
|
ignore-paths = ["examples"]
|
|
55
|
-
|
|
56
|
-
[
|
|
57
|
-
skips = ["B105"]
|
|
58
|
-
|
|
59
|
-
[build-system]
|
|
60
|
-
requires = ["poetry-core>=1.0.0"]
|
|
61
|
-
build-backend = "poetry.core.masonry.api"
|
|
52
|
+
[tool.pylint.messages_control]
|
|
53
|
+
disable = ["invalid-name", "duplicate-code", "no-member", "too-few-public-methods", "too-many-arguments"]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from setuptools import setup
|
|
3
|
+
|
|
4
|
+
packages = \
|
|
5
|
+
['homeassistant_api',
|
|
6
|
+
'homeassistant_api._async',
|
|
7
|
+
'homeassistant_api._async.models',
|
|
8
|
+
'homeassistant_api.models']
|
|
9
|
+
|
|
10
|
+
package_data = \
|
|
11
|
+
{'': ['*']}
|
|
12
|
+
|
|
13
|
+
install_requires = \
|
|
14
|
+
['aiohttp-client-cache>=0.6.1,<0.7.0',
|
|
15
|
+
'aiohttp>=3.8.1,<4.0.0',
|
|
16
|
+
'autodoc-pydantic>=1.6.1,<2.0.0',
|
|
17
|
+
'pydantic>=1.9.0,<2.0.0',
|
|
18
|
+
'requests-cache>=0.9.2,<0.10.0',
|
|
19
|
+
'requests>=2.26.0,<3.0.0',
|
|
20
|
+
'simplejson>=3.17.6,<4.0.0']
|
|
21
|
+
|
|
22
|
+
setup_kwargs = {
|
|
23
|
+
'name': 'homeassistant-api',
|
|
24
|
+
'version': '3.0.3',
|
|
25
|
+
'description': "Python Wrapper for Homeassistant's REST API",
|
|
26
|
+
'long_description': '# HomeassistantAPI\n\n[](https://pypi.org/project/homeassistant_api)\n\n[](https://homeassistantapi.readthedocs.io/en/latest/?badge=latest)\n[](https://github.com/GrandMoff100/HomeassistantAPI/releases)\n\n[](https://home-assistant.io)\n\n## Python wrapper for Homeassistant\'s [REST API](https://developers.home-assistant.io/docs/api/rest/)\n\nHere is a quick example.\n```py\nfrom homeassistant_api import Client\n\nwith Client(\n \'<API Server URL>\',\n \'<Your Long Lived Access-Token>\'\n) as client:\n\n light = client.get_domain("light")\n\n light.turn_on(entity_id=\'light.living_room_lamp\')\n```\nAll the methods also support async!\n\n## Documentation\nAll documentation, API reference, contribution guidelines and pretty much everything else\nyou\'d want to know is on our readthedocs site [here](https://homeassistantapi.readthedocs.io)\n\nIf there is something missing, open an issue and let us know! Thanks!\n\nGo make some cool stuff! Maybe come back and tell us about it in a\n[discussion](https://github.com/GrandMoff100/HomeAssistantAPI/discussions)?\nWe\'d love to hear about how you use our library!!\n\n## License\n\nThis project is under the GNU GPLv3 license, as defined by the Free Software Foundation.\n',
|
|
27
|
+
'author': 'GrandMoff100',
|
|
28
|
+
'author_email': 'minecraftcrusher100@gmail.com',
|
|
29
|
+
'maintainer': None,
|
|
30
|
+
'maintainer_email': None,
|
|
31
|
+
'url': 'https://github.com/GrandMoff100/HomeAssistantAPI',
|
|
32
|
+
'packages': packages,
|
|
33
|
+
'package_data': package_data,
|
|
34
|
+
'install_requires': install_requires,
|
|
35
|
+
'python_requires': '>=3.8,<4.0',
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
setup(**setup_kwargs)
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# HomeassistantAPI
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-

|
|
5
|
-

|
|
6
|
-

|
|
7
|
-

|
|
8
|
-

|
|
9
|
-
|
|
10
|
-

|
|
11
|
-
|
|
12
|
-
Python Wrapper for Homeassistant's [REST API](https://developers.home-assistant.io/docs/api/rest/)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Please ⭐️ the repo if you find this project useful or cool!
|
|
16
|
-
|
|
17
|
-
Here is a quick example.
|
|
18
|
-
```py
|
|
19
|
-
from homeassistant_api import Client
|
|
20
|
-
|
|
21
|
-
with Client(
|
|
22
|
-
'<API Server URL>',
|
|
23
|
-
'<Your Long Lived Access-Token>'
|
|
24
|
-
) as client:
|
|
25
|
-
|
|
26
|
-
light = client.get_domain("light")
|
|
27
|
-
|
|
28
|
-
light.turn_on(entity_id='light.living_room_lamp')
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Documentation
|
|
32
|
-
All documentation, API reference, Contribution guidelines and pretty much everything else you'd want to know is on our readthedocs site [here](https://homeassistantapi.readthedocs.io)
|
|
33
|
-
|
|
34
|
-
If theres something missing open an issue and let us know! Thanks!
|
|
35
|
-
|
|
36
|
-
Go make some cool stuff! Maybe come back and tell us about it in a [discussion](https://github.com/GrandMoff100/HomeAssistantAPI/discussions)? We'd love to hear about how you use our library!!
|
HomeAssistant-API-3.0.0/setup.py
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from setuptools import setup
|
|
3
|
-
|
|
4
|
-
packages = \
|
|
5
|
-
['homeassistant_api',
|
|
6
|
-
'homeassistant_api._async',
|
|
7
|
-
'homeassistant_api._async.models',
|
|
8
|
-
'homeassistant_api.models']
|
|
9
|
-
|
|
10
|
-
package_data = \
|
|
11
|
-
{'': ['*']}
|
|
12
|
-
|
|
13
|
-
install_requires = \
|
|
14
|
-
['aiohttp-client-cache>=0.6.1,<0.7.0',
|
|
15
|
-
'aiohttp>=3.8.1,<4.0.0',
|
|
16
|
-
'pydantic>=1.9.0,<2.0.0',
|
|
17
|
-
'requests-cache>=0.9.2,<0.10.0',
|
|
18
|
-
'requests>=2.26.0,<3.0.0',
|
|
19
|
-
'simplejson>=3.17.6,<4.0.0']
|
|
20
|
-
|
|
21
|
-
setup_kwargs = {
|
|
22
|
-
'name': 'homeassistant-api',
|
|
23
|
-
'version': '3.0.0',
|
|
24
|
-
'description': "Python Wrapper for Homeassistant's REST API",
|
|
25
|
-
'long_description': '# HomeassistantAPI\n\n\n\n\n\n\n\n\n\n\nPython Wrapper for Homeassistant\'s [REST API](https://developers.home-assistant.io/docs/api/rest/)\n\n\nPlease ⭐️ the repo if you find this project useful or cool!\n\nHere is a quick example.\n```py\nfrom homeassistant_api import Client\n\nwith Client(\n \'<API Server URL>\',\n \'<Your Long Lived Access-Token>\'\n) as client:\n\n light = client.get_domain("light")\n\n light.turn_on(entity_id=\'light.living_room_lamp\')\n```\n\n## Documentation\nAll documentation, API reference, Contribution guidelines and pretty much everything else you\'d want to know is on our readthedocs site [here](https://homeassistantapi.readthedocs.io)\n\nIf theres something missing open an issue and let us know! Thanks!\n\nGo make some cool stuff! Maybe come back and tell us about it in a [discussion](https://github.com/GrandMoff100/HomeAssistantAPI/discussions)? We\'d love to hear about how you use our library!!\n',
|
|
26
|
-
'author': 'GrandMoff100',
|
|
27
|
-
'author_email': 'minecraftcrusher100@gmail.com',
|
|
28
|
-
'maintainer': None,
|
|
29
|
-
'maintainer_email': None,
|
|
30
|
-
'url': 'https://github.com/GrandMoff100/HomeAssistantAPI',
|
|
31
|
-
'packages': packages,
|
|
32
|
-
'package_data': package_data,
|
|
33
|
-
'install_requires': install_requires,
|
|
34
|
-
'python_requires': '>=3.7,<4.0',
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
setup(**setup_kwargs)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{HomeAssistant-API-3.0.0 → HomeAssistant-API-3.0.3}/homeassistant_api/_async/models/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|