From 881437c085b532a10f16842823f882db5fe9f179 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 29 Jan 2020 14:46:48 -0800 Subject: [PATCH] Catch error when searching for scenes or automations (#31288) --- homeassistant/components/search/__init__.py | 17 ++++++++++++----- tests/components/search/test_init.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/search/__init__.py b/homeassistant/components/search/__init__.py index 51de916f456..47e7f6ef28d 100644 --- a/homeassistant/components/search/__init__.py +++ b/homeassistant/components/search/__init__.py @@ -59,6 +59,8 @@ class Searcher: # These types won't be further explored. Config entries + Output types. DONT_RESOLVE = {"scene", "automation", "script", "group", "config_entry", "area"} + # These types exist as an entity and so need cleanup in results + EXIST_AS_ENTITY = {"script", "scene", "automation", "group"} def __init__( self, @@ -85,13 +87,18 @@ class Searcher: # Clean up entity_id items, from the general "entity" type result, # that are also found in the specific entity domain type. - self.results["entity"] -= self.results["script"] - self.results["entity"] -= self.results["scene"] - self.results["entity"] -= self.results["automation"] - self.results["entity"] -= self.results["group"] + for result_type in self.EXIST_AS_ENTITY: + self.results["entity"] -= self.results[result_type] # Remove entry into graph from search results. - self.results[item_type].remove(item_id) + to_remove_item_type = item_type + if item_type == "entity": + domain = split_entity_id(item_id)[0] + + if domain in self.EXIST_AS_ENTITY: + to_remove_item_type = domain + + self.results[to_remove_item_type].remove(item_id) # Filter out empty sets. return {key: val for key, val in self.results.items() if val} diff --git a/tests/components/search/test_init.py b/tests/components/search/test_init.py index cce98faa290..5762468ff1d 100644 --- a/tests/components/search/test_init.py +++ b/tests/components/search/test_init.py @@ -189,6 +189,23 @@ async def test_search(hass): results == expected_combined ), f"Results for {search_type}/{search_id} do not match up" + for search_type, search_id in ( + ("entity", "automation.non_existing"), + ("entity", "scene.non_existing"), + ("entity", "group.non_existing"), + ("entity", "script.non_existing"), + ("entity", "light.non_existing"), + ("area", "non_existing"), + ("config_entry", "non_existing"), + ("device", "non_existing"), + ("group", "group.non_existing"), + ("scene", "scene.non_existing"), + ("script", "script.non_existing"), + ("automation", "automation.non_existing"), + ): + searcher = search.Searcher(hass, device_reg, entity_reg) + assert searcher.async_search(search_type, search_id) == {} + async def test_ws_api(hass, hass_ws_client): """Test WS API."""