From 7746ecc9fb66548bf0cbf925069bc2fee9706efe Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 30 Jun 2020 13:22:17 -0500 Subject: [PATCH] Cache checking for entity exposure in emulated_hue (#37260) Since we now base all of exposure checks on data that will not change, we can cache the result instead of calculating it every loop. This change complements the work done in #32718 --- .../components/emulated_hue/__init__.py | 19 +++++++++++++++++++ .../components/emulated_hue/hue_api.py | 7 +++---- tests/components/emulated_hue/test_hue_api.py | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/emulated_hue/__init__.py b/homeassistant/components/emulated_hue/__init__.py index 5dbd52d09b1..9b4da5cfb21 100644 --- a/homeassistant/components/emulated_hue/__init__.py +++ b/homeassistant/components/emulated_hue/__init__.py @@ -174,6 +174,7 @@ class Config: self.type = conf.get(CONF_TYPE) self.numbers = None self.cached_states = {} + self._exposed_cache = {} if self.type == TYPE_ALEXA: _LOGGER.warning( @@ -279,6 +280,24 @@ class Config: return entity.attributes.get(ATTR_EMULATED_HUE_NAME, entity.name) def is_entity_exposed(self, entity): + """Cache determine if an entity should be exposed on the emulated bridge.""" + entity_id = entity.entity_id + if entity_id not in self._exposed_cache: + self._exposed_cache[entity_id] = self._is_entity_exposed(entity) + return self._exposed_cache[entity_id] + + def filter_exposed_entities(self, states): + """Filter a list of all states down to exposed entities.""" + exposed = [] + for entity in states: + entity_id = entity.entity_id + if entity_id not in self._exposed_cache: + self._exposed_cache[entity_id] = self._is_entity_exposed(entity) + if self._exposed_cache[entity_id]: + exposed.append(entity) + return exposed + + def _is_entity_exposed(self, entity): """Determine if an entity should be exposed on the emulated bridge. Async friendly. diff --git a/homeassistant/components/emulated_hue/hue_api.py b/homeassistant/components/emulated_hue/hue_api.py index 15d3f323092..c30af5e1cc7 100644 --- a/homeassistant/components/emulated_hue/hue_api.py +++ b/homeassistant/components/emulated_hue/hue_api.py @@ -759,10 +759,9 @@ def create_list_of_entities(config, request): hass = request.app["hass"] json_response = {} - for entity in hass.states.async_all(): - if config.is_entity_exposed(entity): - number = config.entity_id_to_number(entity.entity_id) - json_response[number] = entity_to_json(config, entity) + for entity in config.filter_exposed_entities(hass.states.async_all()): + number = config.entity_id_to_number(entity.entity_id) + json_response[number] = entity_to_json(config, entity) return json_response diff --git a/tests/components/emulated_hue/test_hue_api.py b/tests/components/emulated_hue/test_hue_api.py index fa97cd2f417..efda09d54ce 100644 --- a/tests/components/emulated_hue/test_hue_api.py +++ b/tests/components/emulated_hue/test_hue_api.py @@ -181,6 +181,8 @@ def hue_client(loop, hass_hue, aiohttp_client): "climate.hvac": {emulated_hue.CONF_ENTITY_HIDDEN: False}, # Expose HeatPump "climate.heatpump": {emulated_hue.CONF_ENTITY_HIDDEN: False}, + # No expose setting (use default of not exposed) + "climate.nosetting": {}, }, }, )