Simplify emulated_hue exposed entities cache (#109890)

Also avoids holding stale States in memory which can prevent
garbage collection of linked contexts
This commit is contained in:
J. Nick Koston 2024-02-09 20:43:46 -06:00 committed by GitHub
parent b0d3cc150f
commit 567a179084
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 21 deletions

View file

@ -207,25 +207,25 @@ class Config:
return state.attributes.get(ATTR_EMULATED_HUE_NAME, state.name) # type: ignore[no-any-return]
@cache # pylint: disable=method-cache-max-size-none
def get_exposed_states(self) -> list[State]:
def get_exposed_entity_ids(self) -> list[str]:
"""Return a list of exposed states."""
state_machine = self.hass.states
if self.expose_by_default:
return [
state
state.entity_id
for state in state_machine.async_all()
if self.is_state_exposed(state)
]
states: list[State] = []
for entity_id in self.entities:
if (state := state_machine.get(entity_id)) and self.is_state_exposed(state):
states.append(state)
return states
return [
entity_id
for entity_id in self.entities
if (state := state_machine.get(entity_id)) and self.is_state_exposed(state)
]
@callback
def _clear_exposed_cache(self, event: EventType[EventStateChangedData]) -> None:
"""Clear the cache of exposed states."""
self.get_exposed_states.cache_clear()
"""Clear the cache of exposed entity ids."""
self.get_exposed_entity_ids.cache_clear()
def is_state_exposed(self, state: State) -> bool:
"""Cache determine if an entity should be exposed on the emulated bridge."""