From 788edc21fbc5d973ab913f6e530be7ae723e459f Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 16 Jan 2023 22:06:52 +0100 Subject: [PATCH] Deduplicate some entity registry code (#85541) --- .../components/config/entity_registry.py | 28 ++---------- homeassistant/helpers/entity_registry.py | 43 +++++++++++-------- 2 files changed, 27 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/config/entity_registry.py b/homeassistant/components/config/entity_registry.py index 90ec415703f..43e433a4f36 100644 --- a/homeassistant/components/config/entity_registry.py +++ b/homeassistant/components/config/entity_registry.py @@ -46,9 +46,9 @@ def websocket_list_entities( msg_json = ( msg_json_prefix + ",".join( - entry.json_repr + entry.partial_json_repr for entry in registry.entities.values() - if entry.json_repr is not None + if entry.partial_json_repr is not None ) + "]}" ) @@ -259,32 +259,10 @@ def websocket_remove_entity( connection.send_message(websocket_api.result_message(msg["id"])) -@callback -def _entry_dict(entry: er.RegistryEntry) -> dict[str, Any]: - """Convert entry to API format.""" - return { - "area_id": entry.area_id, - "config_entry_id": entry.config_entry_id, - "device_id": entry.device_id, - "disabled_by": entry.disabled_by, - "entity_category": entry.entity_category, - "entity_id": entry.entity_id, - "has_entity_name": entry.has_entity_name, - "hidden_by": entry.hidden_by, - "icon": entry.icon, - "id": entry.id, - "name": entry.name, - "original_name": entry.original_name, - "platform": entry.platform, - "translation_key": entry.translation_key, - "unique_id": entry.unique_id, - } - - @callback def _entry_ext_dict(entry: er.RegistryEntry) -> dict[str, Any]: """Convert entry to API format.""" - data = _entry_dict(entry) + data = entry.as_partial_dict data["aliases"] = entry.aliases data["capabilities"] = entry.capabilities data["device_class"] = entry.device_class diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index 4222d38ecd4..08130500f11 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -153,29 +153,34 @@ class RegistryEntry: return self.hidden_by is not None @property - def json_repr(self) -> str | None: - """Return a cached JSON representation of the entry.""" + def as_partial_dict(self) -> dict[str, Any]: + """Return a partial dict representation of the entry.""" + return { + "area_id": self.area_id, + "config_entry_id": self.config_entry_id, + "device_id": self.device_id, + "disabled_by": self.disabled_by, + "entity_category": self.entity_category, + "entity_id": self.entity_id, + "has_entity_name": self.has_entity_name, + "hidden_by": self.hidden_by, + "icon": self.icon, + "id": self.id, + "name": self.name, + "original_name": self.original_name, + "platform": self.platform, + "translation_key": self.translation_key, + "unique_id": self.unique_id, + } + + @property + def partial_json_repr(self) -> str | None: + """Return a cached partial JSON representation of the entry.""" if self._json_repr is not None: return self._json_repr try: - dict_repr = { - "area_id": self.area_id, - "config_entry_id": self.config_entry_id, - "device_id": self.device_id, - "disabled_by": self.disabled_by, - "entity_category": self.entity_category, - "entity_id": self.entity_id, - "has_entity_name": self.has_entity_name, - "hidden_by": self.hidden_by, - "icon": self.icon, - "id": self.id, - "name": self.name, - "original_name": self.original_name, - "platform": self.platform, - "translation_key": self.translation_key, - "unique_id": self.unique_id, - } + dict_repr = self.as_partial_dict object.__setattr__(self, "_json_repr", JSON_DUMP(dict_repr)) except (ValueError, TypeError): _LOGGER.error(