mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Improve performance of config/entity_registry/get* calls (#101984)
This commit is contained in:
parent
2d1afc1c7d
commit
7b2aa3a369
@ -112,7 +112,7 @@ def websocket_get_entity(
|
|||||||
return
|
return
|
||||||
|
|
||||||
connection.send_message(
|
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] = {}
|
entries: dict[str, dict[str, Any] | None] = {}
|
||||||
for entity_id in entity_ids:
|
for entity_id in entity_ids:
|
||||||
entry = registry.entities.get(entity_id)
|
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))
|
connection.send_message(websocket_api.result_message(msg["id"], entries))
|
||||||
|
|
||||||
@ -248,7 +248,7 @@ def websocket_update_entity(
|
|||||||
)
|
)
|
||||||
return
|
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:
|
if "disabled_by" in changes and changes["disabled_by"] is None:
|
||||||
# Enabling an entity requires a config entry reload, or HA restart
|
# Enabling an entity requires a config entry reload, or HA restart
|
||||||
if (
|
if (
|
||||||
@ -289,15 +289,3 @@ def websocket_remove_entity(
|
|||||||
|
|
||||||
registry.async_remove(msg["entity_id"])
|
registry.async_remove(msg["entity_id"])
|
||||||
connection.send_message(websocket_api.result_message(msg["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
|
|
||||||
|
@ -246,7 +246,7 @@ class RegistryEntry:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def as_partial_dict(self) -> dict[str, Any]:
|
def as_partial_dict(self) -> dict[str, Any]:
|
||||||
"""Return a partial dict representation of the entry."""
|
"""Return a partial dict representation of the entry."""
|
||||||
return {
|
return {
|
||||||
@ -268,6 +268,18 @@ class RegistryEntry:
|
|||||||
"unique_id": self.unique_id,
|
"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
|
@cached_property
|
||||||
def partial_json_repr(self) -> str | None:
|
def partial_json_repr(self) -> str | None:
|
||||||
"""Return a cached partial JSON representation of the entry."""
|
"""Return a cached partial JSON representation of the entry."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user