Use cached_property in device registry (#100309)
parent
72f5c0741b
commit
fe5eba9b31
|
@ -12,6 +12,7 @@ from urllib.parse import urlparse
|
||||||
import attr
|
import attr
|
||||||
from yarl import URL
|
from yarl import URL
|
||||||
|
|
||||||
|
from homeassistant.backports.functools import cached_property
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import Event, HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
@ -211,7 +212,7 @@ def _validate_configuration_url(value: Any) -> str | None:
|
||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True, frozen=True)
|
@attr.s(frozen=True)
|
||||||
class DeviceEntry:
|
class DeviceEntry:
|
||||||
"""Device Registry Entry."""
|
"""Device Registry Entry."""
|
||||||
|
|
||||||
|
@ -234,8 +235,6 @@ class DeviceEntry:
|
||||||
# This value is not stored, just used to keep track of events to fire.
|
# This value is not stored, just used to keep track of events to fire.
|
||||||
is_new: bool = attr.ib(default=False)
|
is_new: bool = attr.ib(default=False)
|
||||||
|
|
||||||
_json_repr: str | None = attr.ib(cmp=False, default=None, init=False, repr=False)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def disabled(self) -> bool:
|
def disabled(self) -> bool:
|
||||||
"""Return if entry is disabled."""
|
"""Return if entry is disabled."""
|
||||||
|
@ -262,15 +261,12 @@ class DeviceEntry:
|
||||||
"via_device_id": self.via_device_id,
|
"via_device_id": self.via_device_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def json_repr(self) -> str | None:
|
def json_repr(self) -> str | None:
|
||||||
"""Return a cached JSON representation of the entry."""
|
"""Return a cached JSON representation of the entry."""
|
||||||
if self._json_repr is not None:
|
|
||||||
return self._json_repr
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dict_repr = self.dict_repr
|
dict_repr = self.dict_repr
|
||||||
object.__setattr__(self, "_json_repr", JSON_DUMP(dict_repr))
|
return JSON_DUMP(dict_repr)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Unable to serialize entry %s to JSON. Bad data found at %s",
|
"Unable to serialize entry %s to JSON. Bad data found at %s",
|
||||||
|
@ -279,7 +275,7 @@ class DeviceEntry:
|
||||||
find_paths_unserializable_data(dict_repr, dump=JSON_DUMP)
|
find_paths_unserializable_data(dict_repr, dump=JSON_DUMP)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
return self._json_repr
|
return None
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True, frozen=True)
|
@attr.s(slots=True, frozen=True)
|
||||||
|
|
|
@ -158,7 +158,6 @@ class HomeAssistantSnapshotSerializer(AmberDataSerializer):
|
||||||
)
|
)
|
||||||
if serialized["via_device_id"] is not None:
|
if serialized["via_device_id"] is not None:
|
||||||
serialized["via_device_id"] = ANY
|
serialized["via_device_id"] = ANY
|
||||||
serialized.pop("_json_repr")
|
|
||||||
return serialized
|
return serialized
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
Loading…
Reference in New Issue