Skip to content

Commit e0e903a

Browse files
authored
Merge pull request #106 from GrandMoff100/105-clientget_domain-doesnt-return-before-eating-4+-gb-of-ram
Reverted pydantic to `1.9.0` and removed super contained objects from representation in Service models
2 parents 01081dc + 40408c0 commit e0e903a

7 files changed

Lines changed: 80 additions & 51 deletions

File tree

homeassistant_api/_async/asyncclient.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ async def async_get_domains(self) -> Tuple[Domain, ...]:
206206
)
207207
return tuple(services)
208208

209+
async def async_get_domain(self, domain_id: str) -> Optional[Domain]:
210+
"""Fetchers all services under a particular domain."""
211+
domains = await self.async_get_domains()
212+
for domain in domains:
213+
if domain.domain_id == domain_id:
214+
return domain
215+
return None
216+
209217
async def async_trigger_service(
210218
self,
211219
domain: str,

homeassistant_api/_async/models/domains.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

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

5-
from pydantic import BaseModel, Field
5+
from pydantic import Field, validator
66

7-
from ...models import State
8-
from ...models.domains import ServiceField
7+
from ...models import ServiceField, State, base
98

109
if TYPE_CHECKING:
1110
from homeassistant_api import Client
1211

1312

14-
class AsyncDomain(BaseModel):
13+
class AsyncDomain(base.BaseModel):
1514
"""A class representing the domain that services belong to."""
1615

1716
domain_id: str
@@ -30,7 +29,7 @@ def add_service(self, service_id: str, **data) -> None:
3029
}
3130
)
3231

33-
def get_service(self, service_id: str):
32+
def get_service(self, service_id: str) -> Optional["AsyncService"]:
3433
"""Return a Service with the given service_id, returns None if no such service exists"""
3534
return self.services.get(service_id)
3635

@@ -43,16 +42,25 @@ def __getattr__(self, attr: str):
4342
return super().__getattribute__(attr)
4443

4544

46-
class AsyncService(BaseModel):
45+
class AsyncService(base.BaseModel):
4746
"""Class representing services from homeassistant"""
4847

4948
service_id: str
50-
domain: AsyncDomain
49+
domain: AsyncDomain = Field(exlude=True, repr=False)
5150
name: Optional[str] = None
5251
description: Optional[str] = None
5352
fields: Optional[Dict[str, ServiceField]] = None
5453
target: Optional[Dict[str, dict]] = None
5554

55+
@classmethod
56+
@validator("domain")
57+
def validate_domain(cls, domain: AsyncDomain) -> AsyncDomain:
58+
"""
59+
Explicitly do nothing to validate the parent domain.
60+
Elimintates recursive validation errors.
61+
"""
62+
return domain
63+
5664
async def async_trigger(self, **service_data) -> Tuple[State, ...]:
5765
"""Triggers the service associated with this object."""
5866
data = await self.domain.client.async_trigger_service(

homeassistant_api/_async/models/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AsyncEntity(BaseModel):
3434

3535
slug: str
3636
state: State
37-
group: AsyncGroup
37+
group: AsyncGroup = Field(exclude=True, repr=False)
3838

3939
async def async_get_state(self) -> State:
4040
"""Asks Home Assistant for the state of the entity and sets it locally"""

homeassistant_api/models/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""The Model objects for the entire library."""
22
from .base import BaseModel
3-
from .domains import Domain, Service
3+
from .domains import Domain, Service, ServiceField
44
from .entity import Entity, Group
55
from .events import Event
66
from .history import History
@@ -10,6 +10,10 @@
1010
__all__ = (
1111
"Domain",
1212
"Service",
13+
"BaseModel",
14+
"Domain",
15+
"Service",
16+
"ServiceField",
1317
"Entity",
1418
"Group",
1519
"Event",

homeassistant_api/models/domains.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""File for Service and Domain data models"""
22
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
33

4-
from pydantic import Field
4+
from pydantic import Field, validator
55

66
from .base import BaseModel
77
from .states import State
@@ -29,7 +29,7 @@ def add_service(self, service_id: str, **data) -> None:
2929
}
3030
)
3131

32-
def get_service(self, service_id: str):
32+
def get_service(self, service_id: str) -> Optional["Service"]:
3333
"""Return a Service with the given service_id, returns None if no such service exists"""
3434
return self.services.get(service_id)
3535

@@ -56,12 +56,21 @@ class Service(BaseModel):
5656
"""Model representing services from homeassistant"""
5757

5858
service_id: str
59-
domain: Domain
59+
domain: Domain = Field(exlucde=True, repr=False)
6060
name: Optional[str] = None
6161
description: Optional[str] = None
6262
fields: Optional[Dict[str, ServiceField]] = None
6363
target: Optional[Dict[str, dict]] = None
6464

65+
@classmethod
66+
@validator("domain")
67+
def validate_domain(cls, domain: Domain) -> Domain:
68+
"""
69+
Explicitly do nothing to validate the parent domain.
70+
Elimintates recursive validation errors.
71+
"""
72+
return domain
73+
6574
def trigger(self, **service_data) -> Tuple[State, ...]:
6675
"""Triggers the service associated with this object."""
6776
return self.domain.client.trigger_service(

poetry.lock

Lines changed: 38 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ version = "3.0.3.post5"
2121
[tool.poetry.dependencies]
2222
aiohttp = "^3.8.1"
2323
aiohttp-client-cache = "^0.6.1"
24-
pydantic = "^1.9.0"
24+
pydantic = "<=1.9.0"
2525
python = "^3.8"
2626
requests = "^2.26.0"
2727
requests-cache = "^0.9.2"

0 commit comments

Comments
 (0)