Refactor _async_setup_component to remove need for C901 (#115553)

This commit is contained in:
J. Nick Koston 2024-04-13 19:46:37 -05:00 committed by GitHub
parent 8da7de1fea
commit 41f5325ce3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@ from collections.abc import Awaitable, Callable, Generator, Mapping
import contextlib import contextlib
import contextvars import contextvars
from enum import StrEnum from enum import StrEnum
from functools import partial
import logging.handlers import logging.handlers
import time import time
from types import ModuleType from types import ModuleType
@ -253,34 +254,39 @@ async def _async_process_dependencies(
return failed return failed
async def _async_setup_component( # noqa: C901 def _log_error_setup_error(
hass: HomeAssistant,
domain: str,
integration: loader.Integration | None,
msg: str,
exc_info: Exception | None = None,
) -> None:
"""Log helper."""
if integration is None:
custom = ""
link = None
else:
custom = "" if integration.is_built_in else "custom integration "
link = integration.documentation
_LOGGER.error("Setup failed for %s'%s': %s", custom, domain, msg, exc_info=exc_info)
async_notify_setup_error(hass, domain, link)
async def _async_setup_component(
hass: core.HomeAssistant, domain: str, config: ConfigType hass: core.HomeAssistant, domain: str, config: ConfigType
) -> bool: ) -> bool:
"""Set up a component for Home Assistant. """Set up a component for Home Assistant.
This method is a coroutine. This method is a coroutine.
""" """
integration: loader.Integration | None = None
def log_error(msg: str, exc_info: Exception | None = None) -> None:
"""Log helper."""
if integration is None:
custom = ""
link = None
else:
custom = "" if integration.is_built_in else "custom integration "
link = integration.documentation
_LOGGER.error(
"Setup failed for %s'%s': %s", custom, domain, msg, exc_info=exc_info
)
async_notify_setup_error(hass, domain, link)
try: try:
integration = await loader.async_get_integration(hass, domain) integration = await loader.async_get_integration(hass, domain)
except loader.IntegrationNotFound: except loader.IntegrationNotFound:
log_error("Integration not found.") _log_error_setup_error(hass, domain, None, "Integration not found.")
return False return False
log_error = partial(_log_error_setup_error, hass, domain, integration)
if integration.disabled: if integration.disabled:
log_error(f"Dependency is disabled - {integration.disabled}") log_error(f"Dependency is disabled - {integration.disabled}")
return False return False
@ -451,8 +457,7 @@ async def _async_setup_component( # noqa: C901
) )
# Cleanup # Cleanup
if domain in hass.data[DATA_SETUP]: hass.data[DATA_SETUP].pop(domain, None)
hass.data[DATA_SETUP].pop(domain)
hass.bus.async_fire(EVENT_COMPONENT_LOADED, EventComponentLoaded(component=domain)) hass.bus.async_fire(EVENT_COMPONENT_LOADED, EventComponentLoaded(component=domain))