mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 05:47:10 +00:00
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:
parent
b0d3cc150f
commit
567a179084
@ -207,25 +207,25 @@ class Config:
|
|||||||
return state.attributes.get(ATTR_EMULATED_HUE_NAME, state.name) # type: ignore[no-any-return]
|
return state.attributes.get(ATTR_EMULATED_HUE_NAME, state.name) # type: ignore[no-any-return]
|
||||||
|
|
||||||
@cache # pylint: disable=method-cache-max-size-none
|
@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."""
|
"""Return a list of exposed states."""
|
||||||
state_machine = self.hass.states
|
state_machine = self.hass.states
|
||||||
if self.expose_by_default:
|
if self.expose_by_default:
|
||||||
return [
|
return [
|
||||||
state
|
state.entity_id
|
||||||
for state in state_machine.async_all()
|
for state in state_machine.async_all()
|
||||||
if self.is_state_exposed(state)
|
if self.is_state_exposed(state)
|
||||||
]
|
]
|
||||||
states: list[State] = []
|
return [
|
||||||
for entity_id in self.entities:
|
entity_id
|
||||||
if (state := state_machine.get(entity_id)) and self.is_state_exposed(state):
|
for entity_id in self.entities
|
||||||
states.append(state)
|
if (state := state_machine.get(entity_id)) and self.is_state_exposed(state)
|
||||||
return states
|
]
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _clear_exposed_cache(self, event: EventType[EventStateChangedData]) -> None:
|
def _clear_exposed_cache(self, event: EventType[EventStateChangedData]) -> None:
|
||||||
"""Clear the cache of exposed states."""
|
"""Clear the cache of exposed entity ids."""
|
||||||
self.get_exposed_states.cache_clear()
|
self.get_exposed_entity_ids.cache_clear()
|
||||||
|
|
||||||
def is_state_exposed(self, state: State) -> bool:
|
def is_state_exposed(self, state: State) -> bool:
|
||||||
"""Cache determine if an entity should be exposed on the emulated bridge."""
|
"""Cache determine if an entity should be exposed on the emulated bridge."""
|
||||||
|
@ -890,18 +890,11 @@ def create_config_model(config: Config, request: web.Request) -> dict[str, Any]:
|
|||||||
def create_list_of_entities(config: Config, request: web.Request) -> dict[str, Any]:
|
def create_list_of_entities(config: Config, request: web.Request) -> dict[str, Any]:
|
||||||
"""Create a list of all entities."""
|
"""Create a list of all entities."""
|
||||||
hass: core.HomeAssistant = request.app["hass"]
|
hass: core.HomeAssistant = request.app["hass"]
|
||||||
|
return {
|
||||||
json_response: dict[str, Any] = {}
|
config.entity_id_to_number(entity_id): state_to_json(config, state)
|
||||||
for cached_state in config.get_exposed_states():
|
for entity_id in config.get_exposed_entity_ids()
|
||||||
entity_id = cached_state.entity_id
|
if (state := hass.states.get(entity_id))
|
||||||
state = hass.states.get(entity_id)
|
}
|
||||||
assert state is not None
|
|
||||||
|
|
||||||
json_response[config.entity_id_to_number(entity_id)] = state_to_json(
|
|
||||||
config, state
|
|
||||||
)
|
|
||||||
|
|
||||||
return json_response
|
|
||||||
|
|
||||||
|
|
||||||
def hue_brightness_to_hass(value: int) -> int:
|
def hue_brightness_to_hass(value: int) -> int:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user