From 36b7e8569ec0f9ca93b079523b60501bb38d254b Mon Sep 17 00:00:00 2001 From: Andre Lengwenus Date: Sat, 31 Aug 2024 11:42:22 +0200 Subject: [PATCH] Send entity name or original name to LCN frontend (#124518) * Send name or original name to frontend * Use walrus operator * Fix docstring * Fix mutated config_entry.data --- homeassistant/components/lcn/websocket.py | 28 ++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/lcn/websocket.py b/homeassistant/components/lcn/websocket.py index b418e362b27..65896cc78d1 100644 --- a/homeassistant/components/lcn/websocket.py +++ b/homeassistant/components/lcn/websocket.py @@ -158,7 +158,13 @@ async def websocket_get_entity_configs( else: entity_configs = config_entry.data[CONF_ENTITIES] - connection.send_result(msg["id"], entity_configs) + result_entity_configs = [ + {**entity_config, CONF_NAME: entity.name or entity.original_name} + for entity_config in entity_configs[:] + if (entity := get_entity_entry(hass, entity_config, config_entry)) is not None + ] + + connection.send_result(msg["id"], result_entity_configs) @websocket_api.require_admin @@ -438,3 +444,23 @@ async def async_create_or_update_device_in_config_entry( await async_update_device_config(device_connection, device_config) hass.config_entries.async_update_entry(config_entry, data=data) + + +def get_entity_entry( + hass: HomeAssistant, entity_config: dict, config_entry: ConfigEntry +) -> er.RegistryEntry | None: + """Get entity RegistryEntry from entity_config.""" + entity_registry = er.async_get(hass) + domain_name = entity_config[CONF_DOMAIN] + domain_data = entity_config[CONF_DOMAIN_DATA] + resource = get_resource(domain_name, domain_data).lower() + unique_id = generate_unique_id( + config_entry.entry_id, + entity_config[CONF_ADDRESS], + resource, + ) + if ( + entity_id := entity_registry.async_get_entity_id(domain_name, DOMAIN, unique_id) + ) is None: + return None + return entity_registry.async_get(entity_id)