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: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ 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
:exclude-members: model_json_schema, model_copy, model_rebuild, model_dump, 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


14 changes: 7 additions & 7 deletions homeassistant_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
from .models import Domain, Entity, Event, Group, History, LogbookEntry, Service, State
from .processing import Processing

Domain.update_forward_refs(**locals())
Entity.update_forward_refs(**locals())
Event.update_forward_refs(**locals())
Group.update_forward_refs(**locals())
History.update_forward_refs(**locals())
Service.update_forward_refs(**locals())
State.update_forward_refs(**locals())
Domain.model_rebuild()
Entity.model_rebuild()
Event.model_rebuild()
Group.model_rebuild()
History.model_rebuild()
Service.model_rebuild()
State.model_rebuild()
24 changes: 11 additions & 13 deletions homeassistant_api/models/base.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
"""Module for Global Base Model Configuration inheritance."""

from datetime import datetime
from typing import Annotated

from pydantic import BaseModel as PydanticBaseModel
from pydantic import ConfigDict, BaseModel as PydanticBaseModel, PlainSerializer


class BaseModel(PydanticBaseModel):
"""Base model that all Library Models inherit from."""

class Config: # pylint: disable=too-few-public-methods
"""Pydantic config class for all library models."""
DatetimeIsoField = Annotated[
datetime, PlainSerializer(lambda x: x.isoformat(), return_type=str, when_used='json')
]

arbitrary_types_allowed = True
smart_union = True
validate_assignment = True
exclude_none = True

json_encoders = {
datetime: lambda timestamp: timestamp.isoformat(),
}
class BaseModel(PydanticBaseModel):
"""Base model that all Library Models inherit from."""
model_config = ConfigDict(
arbitrary_types_allowed=True,
validate_assignment=True,
)
5 changes: 2 additions & 3 deletions homeassistant_api/models/logbook.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""Module for the Logbook Entry model."""
from datetime import datetime
from typing import Optional

from pydantic import Field

from .base import BaseModel
from .base import BaseModel, DatetimeIsoField


class LogbookEntry(BaseModel):
"""Model representing entries in the Logbook."""

when: datetime = Field(..., description="When the entry was logged.")
when: DatetimeIsoField = Field(..., description="When the entry was logged.")
name: str = Field(..., description="The name of the entry.")
message: Optional[str] = Field(None, description="Optional message for the entry.")
entity_id: Optional[str] = Field(None, description="Optional relevant entity_id.")
Expand Down
8 changes: 4 additions & 4 deletions homeassistant_api/models/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pydantic import Field

from .base import BaseModel
from .base import BaseModel, DatetimeIsoField


class Context(BaseModel):
Expand All @@ -26,11 +26,11 @@ class State(BaseModel):
attributes: Dict[str, Any] = Field(
{}, description="A dictionary of extra attributes of the state."
)
last_changed: datetime = Field(
last_changed: DatetimeIsoField = Field(
default_factory=datetime.utcnow,
description="The last time the state was changed.",
)
last_updated: Optional[datetime] = Field(
last_updated: Optional[DatetimeIsoField] = Field(
default_factory=datetime.utcnow, description="The last time the state updated."
)
context: Optional[Context] = Field(
Expand All @@ -40,4 +40,4 @@ class State(BaseModel):
@classmethod
def from_json(cls, json: Dict[str, Any]) -> "State":
"""Constructs State model from json data"""
return cls.parse_obj(json)
return cls.model_validate(json)
2 changes: 1 addition & 1 deletion homeassistant_api/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def process(self) -> Any:
if status_code == 401:
raise UnauthorizedError()
if status_code == 404:
raise EndpointNotFoundError(str(self._response.url))
raise EndpointNotFoundError(self._response.url) # type: ignore
if status_code == 405:
if isinstance(self._response, (Response, CachedResponse)):
method = self._response.request.method
Expand Down
Loading