Skip to content

Commit

Permalink
Small performance improvements to config entry setup retry (home-assi…
Browse files Browse the repository at this point in the history
…stant#110448)

* Small performance improvements to config entry setup retry

- cache some properties that never change
- avoid loader.async_get_integration when we already have it
- avoid multiple integration.domain checks

* tweaks
  • Loading branch information
bdraco authored Feb 13, 2024
1 parent 6812596 commit 01c3205
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ async def async_setup(
if self.source == SOURCE_IGNORE or self.disabled_by:
return

if integration is None:
if integration is None and not (integration := self._integration_for_domain):
integration = await loader.async_get_integration(hass, self.domain)
self._integration_for_domain = integration

# Only store setup result as state if it was not forwarded.
if self.domain == integration.domain:
if domain_is_integration := self.domain == integration.domain:
self._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None)

if self.supports_unload is None:
Expand All @@ -409,13 +409,13 @@ async def async_setup(
self.domain,
err,
)
if self.domain == integration.domain:
if domain_is_integration:
self._async_set_state(
hass, ConfigEntryState.SETUP_ERROR, "Import error"
)
return

if self.domain == integration.domain:
if domain_is_integration:
try:
integration.get_platform("config_flow")
except ImportError as err:
Expand Down Expand Up @@ -475,12 +475,12 @@ async def async_setup(
self.async_start_reauth(hass)
result = False
except ConfigEntryNotReady as exc:
self._async_set_state(hass, ConfigEntryState.SETUP_RETRY, str(exc) or None)
message = str(exc)
self._async_set_state(hass, ConfigEntryState.SETUP_RETRY, message or None)
wait_time = 2 ** min(self._tries, 4) * 5 + (
randint(RANDOM_MICROSECOND_MIN, RANDOM_MICROSECOND_MAX) / 1000000
)
self._tries += 1
message = str(exc)
ready_message = f"ready yet: {message}" if message else "ready yet"
_LOGGER.debug(
(
Expand Down Expand Up @@ -513,7 +513,7 @@ async def async_setup(
result = False

# Only store setup result as state if it was not forwarded.
if self.domain != integration.domain:
if not domain_is_integration:
return

#
Expand Down
20 changes: 10 additions & 10 deletions homeassistant/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,52 +665,52 @@ def disabled(self) -> str | None:
"""Return reason integration is disabled."""
return self.manifest.get("disabled")

@property
@cached_property
def domain(self) -> str:
"""Return domain."""
return self.manifest["domain"]

@property
@cached_property
def dependencies(self) -> list[str]:
"""Return dependencies."""
return self.manifest.get("dependencies", [])

@property
@cached_property
def after_dependencies(self) -> list[str]:
"""Return after_dependencies."""
return self.manifest.get("after_dependencies", [])

@property
@cached_property
def requirements(self) -> list[str]:
"""Return requirements."""
return self.manifest.get("requirements", [])

@property
@cached_property
def config_flow(self) -> bool:
"""Return config_flow."""
return self.manifest.get("config_flow") or False

@property
@cached_property
def documentation(self) -> str | None:
"""Return documentation."""
return self.manifest.get("documentation")

@property
@cached_property
def issue_tracker(self) -> str | None:
"""Return issue tracker link."""
return self.manifest.get("issue_tracker")

@property
@cached_property
def loggers(self) -> list[str] | None:
"""Return list of loggers used by the integration."""
return self.manifest.get("loggers")

@property
@cached_property
def quality_scale(self) -> str | None:
"""Return Integration Quality Scale."""
return self.manifest.get("quality_scale")

@property
@cached_property
def iot_class(self) -> str | None:
"""Return the integration IoT Class."""
return self.manifest.get("iot_class")
Expand Down

0 comments on commit 01c3205

Please sign in to comment.