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
pull/37273/head
J. Nick Koston 2020-06-30 13:22:17 -05:00 committed by GitHub
parent 24289d5dbb
commit 7746ecc9fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 4 deletions

View File

@ -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.

View File

@ -759,8 +759,7 @@ 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):
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)

View File

@ -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": {},
},
},
)