From e2b69fc470a76e25b3fd1d34ff2cfc95829d9c71 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 25 May 2023 22:04:38 -0500 Subject: [PATCH] Significantly improve performance of conversation default_agent listening for new states (#93577) Use the async_track_state_added_domain helper instead of tracking all state changes and rejecting them as it is already optimized for this job --- .../components/conversation/default_agent.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/conversation/default_agent.py b/homeassistant/components/conversation/default_agent.py index dccf394ab3f..f0946010b12 100644 --- a/homeassistant/components/conversation/default_agent.py +++ b/homeassistant/components/conversation/default_agent.py @@ -31,7 +31,7 @@ from homeassistant.helpers import ( template, translation, ) -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_added_domain from homeassistant.util.json import JsonObjectType, json_loads_object from .agent import AbstractConversationAgent, ConversationInput, ConversationResult @@ -83,22 +83,16 @@ def async_setup(hass: core.HomeAssistant) -> None: async_should_expose(hass, DOMAIN, entity_id) @core.callback - def async_entity_state_listener( - changed_entity: str, - old_state: core.State | None, - new_state: core.State | None, - ): + def async_entity_state_listener(event: core.Event) -> None: """Set expose flag on new entities.""" - if old_state is not None or new_state is None: - return - async_should_expose(hass, DOMAIN, changed_entity) + async_should_expose(hass, DOMAIN, event.data["entity_id"]) @core.callback def async_hass_started(hass: core.HomeAssistant) -> None: """Set expose flag on all entities.""" for state in hass.states.async_all(): async_should_expose(hass, DOMAIN, state.entity_id) - async_track_state_change(hass, MATCH_ALL, async_entity_state_listener) + async_track_state_added_domain(hass, MATCH_ALL, async_entity_state_listener) start.async_at_started(hass, async_hass_started)