Avoid calling async_get_component twice for each component being setup (#112096)

We already have the component so we can pass it to
async_process_component_config to avoid having to
look it up again
This commit is contained in:
J. Nick Koston 2024-03-03 07:48:07 -10:00 committed by GitHub
parent 72efb3dab5
commit 9af12a0639
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 15 deletions

View file

@ -1430,6 +1430,7 @@ async def async_process_component_config( # noqa: C901
hass: HomeAssistant,
config: ConfigType,
integration: Integration,
component: ComponentProtocol | None = None,
) -> IntegrationConfigInfo:
"""Check component configuration.
@ -1441,18 +1442,19 @@ async def async_process_component_config( # noqa: C901
integration_docs = integration.documentation
config_exceptions: list[ConfigExceptionInfo] = []
try:
component = await integration.async_get_component()
except LOAD_EXCEPTIONS as exc:
exc_info = ConfigExceptionInfo(
exc,
ConfigErrorTranslationKey.COMPONENT_IMPORT_ERR,
domain,
config,
integration_docs,
)
config_exceptions.append(exc_info)
return IntegrationConfigInfo(None, config_exceptions)
if not component:
try:
component = await integration.async_get_component()
except LOAD_EXCEPTIONS as exc:
exc_info = ConfigExceptionInfo(
exc,
ConfigErrorTranslationKey.COMPONENT_IMPORT_ERR,
domain,
config,
integration_docs,
)
config_exceptions.append(exc_info)
return IntegrationConfigInfo(None, config_exceptions)
# Check if the integration has a custom config validator
config_validator = None

View file

@ -299,7 +299,7 @@ async def _async_setup_component( # noqa: C901
return False
integration_config_info = await conf_util.async_process_component_config(
hass, config, integration
hass, config, integration, component
)
conf_util.async_handle_component_errors(hass, integration_config_info, integration)
processed_config = conf_util.async_drop_config_annotations(

View file

@ -1075,11 +1075,11 @@ def assert_setup_component(count, domain=None):
"""
config = {}
async def mock_psc(hass, config_input, integration):
async def mock_psc(hass, config_input, integration, component=None):
"""Mock the prepare_setup_component to capture config."""
domain_input = integration.domain
integration_config_info = await async_process_component_config(
hass, config_input, integration
hass, config_input, integration, component
)
res = integration_config_info.config
config[domain_input] = None if res is None else res.get(domain_input)