Skip to content

Commit

Permalink
Restore __slots__ to registry entries
Browse files Browse the repository at this point in the history
same as home-assistant#127441 but for the registries
  • Loading branch information
bdraco committed Oct 3, 2024
1 parent 3a76dbe commit 75c3b59
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
7 changes: 4 additions & 3 deletions homeassistant/helpers/area_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import datetime
from typing import Any, Literal, TypedDict

from propcache import cached_property
from propcache import under_cached_property

from homeassistant.core import HomeAssistant, callback
from homeassistant.util.dt import utc_from_timestamp, utcnow
Expand Down Expand Up @@ -63,7 +63,7 @@ class EventAreaRegistryUpdatedData(TypedDict):
area_id: str


@dataclass(frozen=True, kw_only=True)
@dataclass(frozen=True, kw_only=True, slots=True)
class AreaEntry(NormalizedNameBaseRegistryEntry):
"""Area Registry Entry."""

Expand All @@ -73,8 +73,9 @@ class AreaEntry(NormalizedNameBaseRegistryEntry):
id: str
labels: set[str] = field(default_factory=set)
picture: str | None
_cache: dict[str, Any] = field(default_factory=dict, compare=False)

@cached_property
@under_cached_property
def json_fragment(self) -> json_fragment:
"""Return a JSON representation of this AreaEntry."""
return json_fragment(
Expand Down
14 changes: 8 additions & 6 deletions homeassistant/helpers/device_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import TYPE_CHECKING, Any, Literal, TypedDict

import attr
from propcache import cached_property
from propcache import under_cached_property
from yarl import URL

from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
Expand Down Expand Up @@ -278,7 +278,7 @@ def _validate_configuration_url(value: Any) -> str | None:
return url_as_str


@attr.s(frozen=True)
@attr.s(frozen=True, slots=True)
class DeviceEntry:
"""Device Registry Entry."""

Expand Down Expand Up @@ -306,6 +306,7 @@ class DeviceEntry:
via_device_id: str | None = attr.ib(default=None)
# This value is not stored, just used to keep track of events to fire.
is_new: bool = attr.ib(default=False)
_cache: dict[str, Any] = attr.ib(factory=dict, eq=False)

@property
def disabled(self) -> bool:
Expand Down Expand Up @@ -342,7 +343,7 @@ def dict_repr(self) -> dict[str, Any]:
"via_device_id": self.via_device_id,
}

@cached_property
@under_cached_property
def json_repr(self) -> bytes | None:
"""Return a cached JSON representation of the entry."""
try:
Expand All @@ -358,7 +359,7 @@ def json_repr(self) -> bytes | None:
)
return None

@cached_property
@under_cached_property
def as_storage_fragment(self) -> json_fragment:
"""Return a json fragment for storage."""
return json_fragment(
Expand Down Expand Up @@ -390,7 +391,7 @@ def as_storage_fragment(self) -> json_fragment:
)


@attr.s(frozen=True)
@attr.s(frozen=True, slots=True)
class DeletedDeviceEntry:
"""Deleted Device Registry Entry."""

Expand All @@ -401,6 +402,7 @@ class DeletedDeviceEntry:
orphaned_timestamp: float | None = attr.ib()
created_at: datetime = attr.ib(factory=utcnow)
modified_at: datetime = attr.ib(factory=utcnow)
_cache: dict[str, Any] = attr.ib(factory=dict, eq=False)

def to_device_entry(
self,
Expand All @@ -419,7 +421,7 @@ def to_device_entry(
is_new=True,
)

@cached_property
@under_cached_property
def as_storage_fragment(self) -> json_fragment:
"""Return a json fragment for storage."""
return json_fragment(
Expand Down
20 changes: 11 additions & 9 deletions homeassistant/helpers/entity_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import TYPE_CHECKING, Any, Literal, NotRequired, TypedDict

import attr
from propcache import cached_property
from propcache import under_cached_property
import voluptuous as vol

from homeassistant.const import (
Expand Down Expand Up @@ -162,7 +162,7 @@ def _protect_entity_options(
return ReadOnlyDict({key: ReadOnlyDict(val) for key, val in data.items()})


@attr.s(frozen=True)
@attr.s(frozen=True, slots=True)
class RegistryEntry:
"""Entity Registry Entry."""

Expand Down Expand Up @@ -201,6 +201,7 @@ class RegistryEntry:
supported_features: int = attr.ib(default=0)
translation_key: str | None = attr.ib(default=None)
unit_of_measurement: str | None = attr.ib(default=None)
_cache: dict[str, Any] = attr.ib(factory=dict, eq=False)

@domain.default
def _domain_default(self) -> str:
Expand Down Expand Up @@ -247,7 +248,7 @@ def _as_display_dict(self) -> dict[str, Any] | None:
display_dict["dp"] = precision
return display_dict

@cached_property
@under_cached_property
def display_json_repr(self) -> bytes | None:
"""Return a cached partial JSON representation of the entry.
Expand All @@ -267,7 +268,7 @@ def display_json_repr(self) -> bytes | None:
return None
return json_repr

@cached_property
@under_cached_property
def as_partial_dict(self) -> dict[str, Any]:
"""Return a partial dict representation of the entry."""
# Convert sets and tuples to lists
Expand Down Expand Up @@ -296,7 +297,7 @@ def as_partial_dict(self) -> dict[str, Any]:
"unique_id": self.unique_id,
}

@cached_property
@under_cached_property
def extended_dict(self) -> dict[str, Any]:
"""Return a extended dict representation of the entry."""
# Convert sets and tuples to lists
Expand All @@ -311,7 +312,7 @@ def extended_dict(self) -> dict[str, Any]:
"original_icon": self.original_icon,
}

@cached_property
@under_cached_property
def partial_json_repr(self) -> bytes | None:
"""Return a cached partial JSON representation of the entry."""
try:
Expand All @@ -327,7 +328,7 @@ def partial_json_repr(self) -> bytes | None:
)
return None

@cached_property
@under_cached_property
def as_storage_fragment(self) -> json_fragment:
"""Return a json fragment for storage."""
return json_fragment(
Expand Down Expand Up @@ -394,7 +395,7 @@ def write_unavailable_state(self, hass: HomeAssistant) -> None:
hass.states.async_set(self.entity_id, STATE_UNAVAILABLE, attrs)


@attr.s(frozen=True)
@attr.s(frozen=True, slots=True)
class DeletedRegistryEntry:
"""Deleted Entity Registry Entry."""

Expand All @@ -407,13 +408,14 @@ class DeletedRegistryEntry:
orphaned_timestamp: float | None = attr.ib()
created_at: datetime = attr.ib(factory=utcnow)
modified_at: datetime = attr.ib(factory=utcnow)
_cache: dict[str, Any] = attr.ib(factory=dict, eq=False)

@domain.default
def _domain_default(self) -> str:
"""Compute domain value."""
return split_entity_id(self.entity_id)[0]

@cached_property
@under_cached_property
def as_storage_fragment(self) -> json_fragment:
"""Return a json fragment for storage."""
return json_fragment(
Expand Down

0 comments on commit 75c3b59

Please sign in to comment.