2017-04-30 07:04:49 +02:00
|
|
|
"""Provide methods to bootstrap a Home Assistant instance."""
|
2019-03-27 22:06:20 +08:00
|
|
|
import asyncio
|
2013-10-21 22:06:22 -07:00
|
|
|
import logging
|
2015-09-04 17:22:42 -05:00
|
|
|
import logging.handlers
|
2015-11-15 02:05:46 -08:00
|
|
|
import os
|
|
|
|
import sys
|
2017-03-01 05:33:19 +01:00
|
|
|
from time import time
|
2016-11-19 08:18:33 -08:00
|
|
|
from collections import OrderedDict
|
2019-03-20 22:49:27 +08:00
|
|
|
from typing import Any, Optional, Dict, Set
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2016-03-27 18:48:51 -07:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-26 05:38:33 -07:00
|
|
|
from homeassistant import core, config as conf_util, config_entries, loader
|
2017-02-13 06:24:07 +01:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE
|
2017-03-05 01:41:54 -08:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-12-17 00:51:06 +01:00
|
|
|
from homeassistant.util.logging import AsyncHandler
|
2018-06-16 10:48:41 -04:00
|
|
|
from homeassistant.util.package import async_get_user_site, is_virtual_env
|
2016-08-20 13:39:56 -06:00
|
|
|
from homeassistant.util.yaml import clear_secret_cache
|
2016-04-09 18:25:01 -04:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2014-01-23 21:34:08 -08:00
|
|
|
|
2015-01-09 00:07:58 -08:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
ERROR_LOG_FILENAME = "home-assistant.log"
|
2017-09-15 22:25:32 -07:00
|
|
|
|
|
|
|
# hass.data key for logging information.
|
2019-07-31 12:25:30 -07:00
|
|
|
DATA_LOGGING = "logging"
|
2017-09-15 22:25:32 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DEBUGGER_INTEGRATIONS = {"ptvsd"}
|
|
|
|
CORE_INTEGRATIONS = ("homeassistant", "persistent_notification")
|
|
|
|
LOGGING_INTEGRATIONS = {"logger", "system_log"}
|
2019-04-15 16:45:46 -07:00
|
|
|
STAGE_1_INTEGRATIONS = {
|
|
|
|
# To record data
|
2019-07-31 12:25:30 -07:00
|
|
|
"recorder",
|
2019-04-15 16:45:46 -07:00
|
|
|
# To make sure we forward data to other instances
|
2019-07-31 12:25:30 -07:00
|
|
|
"mqtt_eventstream",
|
2019-03-08 14:47:10 -08:00
|
|
|
}
|
2015-05-11 22:23:20 -07:00
|
|
|
|
2015-01-09 00:07:58 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def async_from_config_dict(
|
|
|
|
config: Dict[str, Any],
|
|
|
|
hass: core.HomeAssistant,
|
|
|
|
config_dir: Optional[str] = None,
|
|
|
|
enable_log: bool = True,
|
|
|
|
verbose: bool = False,
|
|
|
|
skip_pip: bool = False,
|
|
|
|
log_rotate_days: Any = None,
|
|
|
|
log_file: Any = None,
|
|
|
|
log_no_color: bool = False,
|
|
|
|
) -> Optional[core.HomeAssistant]:
|
2017-07-18 16:23:57 +02:00
|
|
|
"""Try to configure Home Assistant from a configuration dictionary.
|
2016-10-27 09:16:23 +02:00
|
|
|
|
|
|
|
Dynamically loads required components and its dependencies.
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2017-03-01 05:33:19 +01:00
|
|
|
start = time()
|
2017-10-05 21:47:51 -07:00
|
|
|
|
|
|
|
if enable_log:
|
2019-07-31 12:25:30 -07:00
|
|
|
async_enable_logging(hass, verbose, log_rotate_days, log_file, log_no_color)
|
2017-10-05 21:47:51 -07:00
|
|
|
|
2019-02-28 10:01:10 -08:00
|
|
|
hass.config.skip_pip = skip_pip
|
|
|
|
if skip_pip:
|
2019-07-31 12:25:30 -07:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Skipping pip installation of required modules. " "This may cause issues"
|
|
|
|
)
|
2019-02-28 10:01:10 -08:00
|
|
|
|
2016-05-07 22:24:04 -07:00
|
|
|
core_config = config.get(core.DOMAIN, {})
|
|
|
|
|
2016-03-27 18:48:51 -07:00
|
|
|
try:
|
2019-10-14 14:56:45 -07:00
|
|
|
await conf_util.async_process_ha_core_config(hass, core_config)
|
2018-08-28 11:54:01 -07:00
|
|
|
except vol.Invalid as config_err:
|
2019-07-31 12:25:30 -07:00
|
|
|
conf_util.async_log_exception(config_err, "homeassistant", core_config, hass)
|
2018-08-28 11:54:01 -07:00
|
|
|
return None
|
|
|
|
except HomeAssistantError:
|
2019-07-31 12:25:30 -07:00
|
|
|
_LOGGER.error(
|
|
|
|
"Home Assistant core failed to initialize. "
|
|
|
|
"Further initialization aborted"
|
|
|
|
)
|
2016-03-27 18:48:51 -07:00
|
|
|
return None
|
|
|
|
|
2018-03-09 05:34:24 +02:00
|
|
|
# Make a copy because we are mutating it.
|
2018-03-10 20:02:04 +02:00
|
|
|
config = OrderedDict(config)
|
2018-03-09 05:34:24 +02:00
|
|
|
|
2017-01-14 08:01:47 +02:00
|
|
|
# Merge packages
|
2019-04-14 22:02:49 -04:00
|
|
|
await conf_util.merge_packages_config(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass, config, core_config.get(conf_util.CONF_PACKAGES, {})
|
|
|
|
)
|
2017-01-14 08:01:47 +02:00
|
|
|
|
2018-02-16 14:07:38 -08:00
|
|
|
hass.config_entries = config_entries.ConfigEntries(hass, config)
|
2019-02-28 20:27:20 -08:00
|
|
|
await hass.config_entries.async_initialize()
|
2018-02-16 14:07:38 -08:00
|
|
|
|
2019-04-16 13:40:21 -07:00
|
|
|
await _async_set_up_integrations(hass, config)
|
2016-11-30 13:02:45 -08:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
stop = time()
|
2019-07-31 12:25:30 -07:00
|
|
|
_LOGGER.info("Home Assistant initialized in %.2fs", stop - start)
|
2017-03-01 05:33:19 +01:00
|
|
|
|
2019-10-15 11:04:58 +03:00
|
|
|
if sys.version_info[:3] < (3, 7, 0):
|
|
|
|
msg = (
|
|
|
|
"Python 3.6 support is deprecated and will "
|
|
|
|
"be removed in the first release after December 15, 2019. Please "
|
|
|
|
"upgrade Python to 3.7.0 or higher."
|
|
|
|
)
|
|
|
|
_LOGGER.warning(msg)
|
|
|
|
hass.components.persistent_notification.async_create(
|
|
|
|
msg, "Python version", "python_version"
|
|
|
|
)
|
|
|
|
|
2014-08-13 14:28:45 +02:00
|
|
|
return hass
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-01-23 17:46:29 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def async_from_config_file(
|
|
|
|
config_path: str,
|
|
|
|
hass: core.HomeAssistant,
|
|
|
|
verbose: bool = False,
|
|
|
|
skip_pip: bool = True,
|
|
|
|
log_rotate_days: Any = None,
|
|
|
|
log_file: Any = None,
|
|
|
|
log_no_color: bool = False,
|
|
|
|
) -> Optional[core.HomeAssistant]:
|
2016-10-27 09:16:23 +02:00
|
|
|
"""Read the configuration file and try to start all the functionality.
|
|
|
|
|
|
|
|
Will add functionality to 'hass' parameter.
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2015-03-18 23:02:58 -07:00
|
|
|
# Set config dir to directory holding config file
|
2015-08-29 22:19:52 -04:00
|
|
|
config_dir = os.path.abspath(os.path.dirname(config_path))
|
|
|
|
hass.config.config_dir = config_dir
|
2018-06-16 10:48:41 -04:00
|
|
|
|
|
|
|
if not is_virtual_env():
|
|
|
|
await async_mount_local_lib_path(config_dir)
|
2014-09-20 21:19:39 -05:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async_enable_logging(hass, verbose, log_rotate_days, log_file, log_no_color)
|
2015-08-29 23:02:07 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.async_add_executor_job(conf_util.process_ha_config_upgrade, hass)
|
2019-04-16 14:27:07 -07:00
|
|
|
|
2016-04-09 18:25:01 -04:00
|
|
|
try:
|
2018-07-13 13:24:51 +03:00
|
|
|
config_dict = await hass.async_add_executor_job(
|
2019-07-31 12:25:30 -07:00
|
|
|
conf_util.load_yaml_config_file, config_path
|
|
|
|
)
|
2017-03-01 06:56:23 +02:00
|
|
|
except HomeAssistantError as err:
|
2017-07-18 16:23:57 +02:00
|
|
|
_LOGGER.error("Error loading %s: %s", config_path, err)
|
2016-04-09 18:25:01 -04:00
|
|
|
return None
|
2016-08-20 13:39:56 -06:00
|
|
|
finally:
|
|
|
|
clear_secret_cache()
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2018-06-16 10:48:41 -04:00
|
|
|
return await async_from_config_dict(
|
2019-07-31 12:25:30 -07:00
|
|
|
config_dict, hass, enable_log=False, skip_pip=skip_pip
|
|
|
|
)
|
2015-01-17 22:23:07 -08:00
|
|
|
|
|
|
|
|
2017-02-13 06:24:07 +01:00
|
|
|
@core.callback
|
2019-07-31 12:25:30 -07:00
|
|
|
def async_enable_logging(
|
|
|
|
hass: core.HomeAssistant,
|
|
|
|
verbose: bool = False,
|
|
|
|
log_rotate_days: Optional[int] = None,
|
|
|
|
log_file: Optional[str] = None,
|
|
|
|
log_no_color: bool = False,
|
|
|
|
) -> None:
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Set up the logging.
|
2016-10-27 09:16:23 +02:00
|
|
|
|
2017-02-13 06:24:07 +01:00
|
|
|
This method must be run in the event loop.
|
2016-10-27 09:16:23 +02:00
|
|
|
"""
|
2019-07-31 12:25:30 -07:00
|
|
|
fmt = "%(asctime)s %(levelname)s (%(threadName)s) " "[%(name)s] %(message)s"
|
|
|
|
datefmt = "%Y-%m-%d %H:%M:%S"
|
2016-10-17 15:14:10 -04:00
|
|
|
|
2018-04-18 07:18:44 -07:00
|
|
|
if not log_no_color:
|
|
|
|
try:
|
|
|
|
from colorlog import ColoredFormatter
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2018-04-18 07:18:44 -07:00
|
|
|
# basicConfig must be called after importing colorlog in order to
|
|
|
|
# ensure that the handlers it sets up wraps the correct streams.
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2019-08-23 18:53:33 +02:00
|
|
|
colorfmt = f"%(log_color)s{fmt}%(reset)s"
|
2019-07-31 12:25:30 -07:00
|
|
|
logging.getLogger().handlers[0].setFormatter(
|
|
|
|
ColoredFormatter(
|
|
|
|
colorfmt,
|
|
|
|
datefmt=datefmt,
|
|
|
|
reset=True,
|
|
|
|
log_colors={
|
|
|
|
"DEBUG": "cyan",
|
|
|
|
"INFO": "green",
|
|
|
|
"WARNING": "yellow",
|
|
|
|
"ERROR": "red",
|
|
|
|
"CRITICAL": "red",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2018-04-18 07:18:44 -07:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# If the above initialization failed for any reason, setup the default
|
|
|
|
# formatting. If the above succeeds, this wil result in a no-op.
|
|
|
|
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO)
|
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Suppress overly verbose logs from libraries that aren't helpful
|
2019-07-31 12:25:30 -07:00
|
|
|
logging.getLogger("requests").setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("aiohttp.access").setLevel(logging.WARNING)
|
2016-10-17 15:14:10 -04:00
|
|
|
|
2015-01-17 22:23:07 -08:00
|
|
|
# Log errors to a file if we have write access to file or config dir
|
2017-09-13 21:22:42 -07:00
|
|
|
if log_file is None:
|
|
|
|
err_log_path = hass.config.path(ERROR_LOG_FILENAME)
|
|
|
|
else:
|
|
|
|
err_log_path = os.path.abspath(log_file)
|
|
|
|
|
2015-01-17 22:23:07 -08:00
|
|
|
err_path_exists = os.path.isfile(err_log_path)
|
2017-09-13 21:22:42 -07:00
|
|
|
err_dir = os.path.dirname(err_log_path)
|
2015-01-17 22:23:07 -08:00
|
|
|
|
|
|
|
# Check if we can write to the error log if it exists or that
|
|
|
|
# we can create files in the containing directory if not.
|
2019-07-31 12:25:30 -07:00
|
|
|
if (err_path_exists and os.access(err_log_path, os.W_OK)) or (
|
|
|
|
not err_path_exists and os.access(err_dir, os.W_OK)
|
|
|
|
):
|
2015-01-17 22:23:07 -08:00
|
|
|
|
2015-09-04 17:22:42 -05:00
|
|
|
if log_rotate_days:
|
2019-09-04 05:36:04 +02:00
|
|
|
err_handler: logging.FileHandler = logging.handlers.TimedRotatingFileHandler(
|
2019-07-31 12:25:30 -07:00
|
|
|
err_log_path, when="midnight", backupCount=log_rotate_days
|
2019-09-04 05:36:04 +02:00
|
|
|
)
|
2015-09-04 17:22:42 -05:00
|
|
|
else:
|
2019-07-31 12:25:30 -07:00
|
|
|
err_handler = logging.FileHandler(err_log_path, mode="w", delay=True)
|
2015-01-17 22:23:07 -08:00
|
|
|
|
2015-09-01 02:12:00 -04:00
|
|
|
err_handler.setLevel(logging.INFO if verbose else logging.WARNING)
|
2017-01-20 00:31:44 -05:00
|
|
|
err_handler.setFormatter(logging.Formatter(fmt, datefmt=datefmt))
|
2016-12-17 00:51:06 +01:00
|
|
|
|
|
|
|
async_handler = AsyncHandler(hass.loop, err_handler)
|
2017-02-13 06:24:07 +01:00
|
|
|
|
2018-07-23 11:24:39 +03:00
|
|
|
async def async_stop_async_handler(_: Any) -> None:
|
2017-02-13 06:24:07 +01:00
|
|
|
"""Cleanup async handler."""
|
2019-07-31 12:25:30 -07:00
|
|
|
logging.getLogger("").removeHandler(async_handler) # type: ignore
|
2018-04-29 01:26:20 +02:00
|
|
|
await async_handler.async_close(blocking=True)
|
2017-02-13 06:24:07 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, async_stop_async_handler)
|
2016-12-17 00:51:06 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
logger = logging.getLogger("")
|
2018-05-13 00:44:53 +03:00
|
|
|
logger.addHandler(async_handler) # type: ignore
|
2015-12-22 18:39:46 -08:00
|
|
|
logger.setLevel(logging.INFO)
|
2015-01-17 22:23:07 -08:00
|
|
|
|
2017-09-15 22:25:32 -07:00
|
|
|
# Save the log file location for access by other components.
|
|
|
|
hass.data[DATA_LOGGING] = err_log_path
|
2015-01-17 22:23:07 -08:00
|
|
|
else:
|
2019-07-31 12:25:30 -07:00
|
|
|
_LOGGER.error("Unable to set up error log %s (access denied)", err_log_path)
|
2015-01-29 23:56:04 -08:00
|
|
|
|
2015-01-30 08:26:06 -08:00
|
|
|
|
2018-06-16 10:48:41 -04:00
|
|
|
async def async_mount_local_lib_path(config_dir: str) -> str:
|
2016-10-27 09:16:23 +02:00
|
|
|
"""Add local library to Python Path.
|
|
|
|
|
2017-07-14 04:26:21 +02:00
|
|
|
This function is a coroutine.
|
2016-10-27 09:16:23 +02:00
|
|
|
"""
|
2019-07-31 12:25:30 -07:00
|
|
|
deps_dir = os.path.join(config_dir, "deps")
|
2018-06-16 10:48:41 -04:00
|
|
|
lib_dir = await async_get_user_site(deps_dir)
|
2017-07-14 04:26:21 +02:00
|
|
|
if lib_dir not in sys.path:
|
|
|
|
sys.path.insert(0, lib_dir)
|
2016-08-10 08:54:34 +02:00
|
|
|
return deps_dir
|
2019-03-19 11:33:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
@core.callback
|
2019-04-09 09:30:32 -07:00
|
|
|
def _get_domains(hass: core.HomeAssistant, config: Dict[str, Any]) -> Set[str]:
|
|
|
|
"""Get domains of components to set up."""
|
2019-03-19 11:33:50 -07:00
|
|
|
# Filter out the repeating and common config section [homeassistant]
|
2019-07-31 12:25:30 -07:00
|
|
|
domains = set(key.split(" ")[0] for key in config.keys() if key != core.DOMAIN)
|
2019-03-19 11:33:50 -07:00
|
|
|
|
|
|
|
# Add config entry domains
|
2019-10-18 13:06:33 -07:00
|
|
|
domains.update(hass.config_entries.async_domains())
|
2019-03-19 11:33:50 -07:00
|
|
|
|
|
|
|
# Make sure the Hass.io component is loaded
|
2019-07-31 12:25:30 -07:00
|
|
|
if "HASSIO" in os.environ:
|
|
|
|
domains.add("hassio")
|
2019-03-19 11:33:50 -07:00
|
|
|
|
2019-04-09 09:30:32 -07:00
|
|
|
return domains
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
|
|
|
|
async def _async_set_up_integrations(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass: core.HomeAssistant, config: Dict[str, Any]
|
|
|
|
) -> None:
|
2019-04-16 13:40:21 -07:00
|
|
|
"""Set up all the integrations."""
|
|
|
|
domains = _get_domains(hass, config)
|
|
|
|
|
2019-05-01 06:07:34 +08:00
|
|
|
# Start up debuggers. Start these first in case they want to wait.
|
|
|
|
debuggers = domains & DEBUGGER_INTEGRATIONS
|
|
|
|
if debuggers:
|
|
|
|
_LOGGER.debug("Starting up debuggers %s", debuggers)
|
2019-07-31 12:25:30 -07:00
|
|
|
await asyncio.gather(
|
|
|
|
*(async_setup_component(hass, domain, config) for domain in debuggers)
|
|
|
|
)
|
2019-05-01 06:07:34 +08:00
|
|
|
domains -= DEBUGGER_INTEGRATIONS
|
|
|
|
|
2019-04-16 13:40:21 -07:00
|
|
|
# Resolve all dependencies of all components so we can find the logging
|
|
|
|
# and integrations that need faster initialization.
|
2019-07-31 12:25:30 -07:00
|
|
|
resolved_domains_task = asyncio.gather(
|
|
|
|
*(loader.async_component_dependencies(hass, domain) for domain in domains),
|
|
|
|
return_exceptions=True,
|
|
|
|
)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
# Set up core.
|
|
|
|
_LOGGER.debug("Setting up %s", CORE_INTEGRATIONS)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if not all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*(
|
|
|
|
async_setup_component(hass, domain, config)
|
|
|
|
for domain in CORE_INTEGRATIONS
|
|
|
|
)
|
|
|
|
)
|
|
|
|
):
|
|
|
|
_LOGGER.error(
|
|
|
|
"Home Assistant core failed to initialize. "
|
|
|
|
"Further initialization aborted"
|
|
|
|
)
|
2019-04-16 13:40:21 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
_LOGGER.debug("Home Assistant core initialized")
|
|
|
|
|
|
|
|
# Finish resolving domains
|
|
|
|
for dep_domains in await resolved_domains_task:
|
|
|
|
# Result is either a set or an exception. We ignore exceptions
|
|
|
|
# It will be properly handled during setup of the domain.
|
|
|
|
if isinstance(dep_domains, set):
|
|
|
|
domains.update(dep_domains)
|
|
|
|
|
|
|
|
# setup components
|
|
|
|
logging_domains = domains & LOGGING_INTEGRATIONS
|
|
|
|
stage_1_domains = domains & STAGE_1_INTEGRATIONS
|
|
|
|
stage_2_domains = domains - logging_domains - stage_1_domains
|
|
|
|
|
|
|
|
if logging_domains:
|
2019-05-01 06:07:34 +08:00
|
|
|
_LOGGER.info("Setting up %s", logging_domains)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await asyncio.gather(
|
|
|
|
*(async_setup_component(hass, domain, config) for domain in logging_domains)
|
|
|
|
)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
# Kick off loading the registries. They don't need to be awaited.
|
|
|
|
asyncio.gather(
|
|
|
|
hass.helpers.device_registry.async_get_registry(),
|
|
|
|
hass.helpers.entity_registry.async_get_registry(),
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.helpers.area_registry.async_get_registry(),
|
|
|
|
)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
if stage_1_domains:
|
2019-07-31 12:25:30 -07:00
|
|
|
await asyncio.gather(
|
|
|
|
*(async_setup_component(hass, domain, config) for domain in stage_1_domains)
|
|
|
|
)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
# Load all integrations
|
2019-09-04 05:36:04 +02:00
|
|
|
after_dependencies: Dict[str, Set[str]] = {}
|
2019-04-16 13:40:21 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
for int_or_exc in await asyncio.gather(
|
|
|
|
*(loader.async_get_integration(hass, domain) for domain in stage_2_domains),
|
|
|
|
return_exceptions=True,
|
|
|
|
):
|
2019-04-16 13:40:21 -07:00
|
|
|
# Exceptions are handled in async_setup_component.
|
2019-07-31 12:25:30 -07:00
|
|
|
if isinstance(int_or_exc, loader.Integration) and int_or_exc.after_dependencies:
|
|
|
|
after_dependencies[int_or_exc.domain] = set(int_or_exc.after_dependencies)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
last_load = None
|
|
|
|
while stage_2_domains:
|
|
|
|
domains_to_load = set()
|
|
|
|
|
|
|
|
for domain in stage_2_domains:
|
|
|
|
after_deps = after_dependencies.get(domain)
|
|
|
|
# Load if integration has no after_dependencies or they are
|
|
|
|
# all loaded
|
2019-07-31 12:25:30 -07:00
|
|
|
if not after_deps or not after_deps - hass.config.components:
|
2019-04-16 13:40:21 -07:00
|
|
|
domains_to_load.add(domain)
|
|
|
|
|
|
|
|
if not domains_to_load or domains_to_load == last_load:
|
|
|
|
break
|
|
|
|
|
|
|
|
_LOGGER.debug("Setting up %s", domains_to_load)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await asyncio.gather(
|
|
|
|
*(async_setup_component(hass, domain, config) for domain in domains_to_load)
|
|
|
|
)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
last_load = domains_to_load
|
|
|
|
stage_2_domains -= domains_to_load
|
|
|
|
|
|
|
|
# These are stage 2 domains that never have their after_dependencies
|
|
|
|
# satisfied.
|
|
|
|
if stage_2_domains:
|
|
|
|
_LOGGER.debug("Final set up: %s", stage_2_domains)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await asyncio.gather(
|
|
|
|
*(async_setup_component(hass, domain, config) for domain in stage_2_domains)
|
|
|
|
)
|
2019-04-16 13:40:21 -07:00
|
|
|
|
|
|
|
# Wrap up startup
|
|
|
|
await hass.async_block_till_done()
|