1
mirror of https://github.com/home-assistant/core synced 2024-08-28 03:36:46 +02:00

Improve performance of config/entity_registry/get* calls (#101984)

This commit is contained in:
J. Nick Koston 2023-10-14 08:40:16 -10:00 committed by GitHub
parent 2d1afc1c7d
commit 7b2aa3a369
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 16 deletions

View File

@ -112,7 +112,7 @@ def websocket_get_entity(
return
connection.send_message(
websocket_api.result_message(msg["id"], _entry_ext_dict(entry))
websocket_api.result_message(msg["id"], entry.extended_dict)
)
@ -138,7 +138,7 @@ def websocket_get_entities(
entries: dict[str, dict[str, Any] | None] = {}
for entity_id in entity_ids:
entry = registry.entities.get(entity_id)
entries[entity_id] = _entry_ext_dict(entry) if entry else None
entries[entity_id] = entry.extended_dict if entry else None
connection.send_message(websocket_api.result_message(msg["id"], entries))
@ -248,7 +248,7 @@ def websocket_update_entity(
)
return
result: dict[str, Any] = {"entity_entry": _entry_ext_dict(entity_entry)}
result: dict[str, Any] = {"entity_entry": entity_entry.extended_dict}
if "disabled_by" in changes and changes["disabled_by"] is None:
# Enabling an entity requires a config entry reload, or HA restart
if (
@ -289,15 +289,3 @@ def websocket_remove_entity(
registry.async_remove(msg["entity_id"])
connection.send_message(websocket_api.result_message(msg["id"]))
@callback
def _entry_ext_dict(entry: er.RegistryEntry) -> dict[str, Any]:
"""Convert entry to API format."""
data = entry.as_partial_dict
data["aliases"] = entry.aliases
data["capabilities"] = entry.capabilities
data["device_class"] = entry.device_class
data["original_device_class"] = entry.original_device_class
data["original_icon"] = entry.original_icon
return data

View File

@ -246,7 +246,7 @@ class RegistryEntry:
return None
@property
@cached_property
def as_partial_dict(self) -> dict[str, Any]:
"""Return a partial dict representation of the entry."""
return {
@ -268,6 +268,18 @@ class RegistryEntry:
"unique_id": self.unique_id,
}
@cached_property
def extended_dict(self) -> dict[str, Any]:
"""Return a extended dict representation of the entry."""
return {
**self.as_partial_dict,
"aliases": self.aliases,
"capabilities": self.capabilities,
"device_class": self.device_class,
"original_device_class": self.original_device_class,
"original_icon": self.original_icon,
}
@cached_property
def partial_json_repr(self) -> str | None:
"""Return a cached partial JSON representation of the entry."""