2017-04-30 05:04:49 +00:00
|
|
|
"""All methods needed to bootstrap a Home Assistant instance."""
|
2017-03-05 09:41:54 +00:00
|
|
|
import asyncio
|
|
|
|
import logging.handlers
|
2017-06-02 05:44:44 +00:00
|
|
|
from timeit import default_timer as timer
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
from types import ModuleType
|
2018-07-23 08:24:39 +00:00
|
|
|
from typing import Optional, Dict, List
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2018-01-30 11:30:47 +00:00
|
|
|
from homeassistant import requirements, core, loader, config as conf_util
|
2017-11-03 14:43:30 +00:00
|
|
|
from homeassistant.config import async_notify_setup_error
|
2018-01-30 11:30:47 +00:00
|
|
|
from homeassistant.const import EVENT_COMPONENT_LOADED, PLATFORM_FORMAT
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2018-03-11 17:01:12 +00:00
|
|
|
from homeassistant.util.async_ import run_coroutine_threadsafe
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2018-01-30 11:30:47 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ATTR_COMPONENT = 'component'
|
|
|
|
|
|
|
|
DATA_SETUP = 'setup_tasks'
|
2018-01-30 11:30:47 +00:00
|
|
|
DATA_DEPS_REQS = 'deps_reqs_processed'
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2017-03-08 04:31:57 +00:00
|
|
|
SLOW_SETUP_WARNING = 10
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def setup_component(hass: core.HomeAssistant, domain: str,
|
2018-02-11 17:20:28 +00:00
|
|
|
config: Optional[Dict] = None) -> bool:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a component and all its dependencies."""
|
2018-07-13 17:14:45 +00:00
|
|
|
return run_coroutine_threadsafe( # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
async_setup_component(hass, domain, config), loop=hass.loop).result()
|
|
|
|
|
|
|
|
|
2018-02-25 11:38:46 +00:00
|
|
|
async def async_setup_component(hass: core.HomeAssistant, domain: str,
|
|
|
|
config: Optional[Dict] = None) -> bool:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a component and all its dependencies.
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
if domain in hass.config.components:
|
|
|
|
return True
|
|
|
|
|
|
|
|
setup_tasks = hass.data.get(DATA_SETUP)
|
|
|
|
|
|
|
|
if setup_tasks is not None and domain in setup_tasks:
|
2018-07-13 17:14:45 +00:00
|
|
|
return await setup_tasks[domain] # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
|
|
|
|
if setup_tasks is None:
|
|
|
|
setup_tasks = hass.data[DATA_SETUP] = {}
|
|
|
|
|
2018-07-13 10:24:51 +00:00
|
|
|
task = setup_tasks[domain] = hass.async_create_task(
|
2017-03-05 09:41:54 +00:00
|
|
|
_async_setup_component(hass, domain, config))
|
|
|
|
|
2018-07-13 17:14:45 +00:00
|
|
|
return await task # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def _async_process_dependencies(
|
|
|
|
hass: core.HomeAssistant, config: Dict, name: str,
|
|
|
|
dependencies: List[str]) -> bool:
|
2017-03-05 09:41:54 +00:00
|
|
|
"""Ensure all dependencies are set up."""
|
|
|
|
blacklisted = [dep for dep in dependencies
|
|
|
|
if dep in loader.DEPENDENCY_BLACKLIST]
|
|
|
|
|
|
|
|
if blacklisted:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to setup dependencies of %s: "
|
|
|
|
"found blacklisted dependencies: %s",
|
2017-03-05 09:41:54 +00:00
|
|
|
name, ', '.join(blacklisted))
|
|
|
|
return False
|
|
|
|
|
|
|
|
tasks = [async_setup_component(hass, dep, config) for dep
|
|
|
|
in dependencies]
|
|
|
|
|
|
|
|
if not tasks:
|
|
|
|
return True
|
|
|
|
|
2018-02-25 11:38:46 +00:00
|
|
|
results = await asyncio.gather(*tasks, loop=hass.loop)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
failed = [dependencies[idx] for idx, res
|
|
|
|
in enumerate(results) if not res]
|
|
|
|
|
|
|
|
if failed:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to setup dependencies of %s. "
|
|
|
|
"Setup failed for dependencies: %s",
|
2017-03-05 09:41:54 +00:00
|
|
|
name, ', '.join(failed))
|
|
|
|
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-02-25 11:38:46 +00:00
|
|
|
async def _async_setup_component(hass: core.HomeAssistant,
|
2018-07-23 08:24:39 +00:00
|
|
|
domain: str, config: Dict) -> bool:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a component for Home Assistant.
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2018-07-23 08:24:39 +00:00
|
|
|
def log_error(msg: str, link: bool = True) -> None:
|
2017-03-05 09:41:54 +00:00
|
|
|
"""Log helper."""
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Setup failed for %s: %s", domain, msg)
|
2017-03-05 09:41:54 +00:00
|
|
|
async_notify_setup_error(hass, domain, link)
|
|
|
|
|
2018-05-01 18:57:30 +00:00
|
|
|
component = loader.get_component(hass, domain)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
if not component:
|
2017-04-30 05:04:49 +00:00
|
|
|
log_error("Component not found.", False)
|
2017-03-05 09:41:54 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Validate no circular dependencies
|
2018-05-01 18:57:30 +00:00
|
|
|
components = loader.load_order_component(hass, domain)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
# OrderedSet is empty if component or dependencies could not be resolved
|
|
|
|
if not components:
|
2017-04-30 05:04:49 +00:00
|
|
|
log_error("Unable to resolve component or dependencies.")
|
2017-03-05 09:41:54 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
processed_config = \
|
|
|
|
conf_util.async_process_component_config(hass, config, domain)
|
|
|
|
|
|
|
|
if processed_config is None:
|
2017-04-30 05:04:49 +00:00
|
|
|
log_error("Invalid config.")
|
2017-03-05 09:41:54 +00:00
|
|
|
return False
|
|
|
|
|
2018-01-30 11:30:47 +00:00
|
|
|
try:
|
2018-02-25 11:38:46 +00:00
|
|
|
await async_process_deps_reqs(hass, config, domain, component)
|
2018-01-30 11:30:47 +00:00
|
|
|
except HomeAssistantError as err:
|
|
|
|
log_error(str(err))
|
|
|
|
return False
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2017-06-02 05:44:44 +00:00
|
|
|
start = timer()
|
2017-03-08 04:31:57 +00:00
|
|
|
_LOGGER.info("Setting up %s", domain)
|
2018-01-02 20:16:32 +00:00
|
|
|
|
|
|
|
if hasattr(component, 'PLATFORM_SCHEMA'):
|
|
|
|
# Entity components have their own warning
|
|
|
|
warn_task = None
|
|
|
|
else:
|
|
|
|
warn_task = hass.loop.call_later(
|
|
|
|
SLOW_SETUP_WARNING, _LOGGER.warning,
|
|
|
|
"Setup of %s is taking over %s seconds.",
|
|
|
|
domain, SLOW_SETUP_WARNING)
|
2017-03-08 04:31:57 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
try:
|
2018-01-30 11:30:47 +00:00
|
|
|
if hasattr(component, 'async_setup'):
|
2018-05-12 21:44:53 +00:00
|
|
|
result = await component.async_setup( # type: ignore
|
|
|
|
hass, processed_config)
|
2017-03-05 09:41:54 +00:00
|
|
|
else:
|
2018-07-13 10:24:51 +00:00
|
|
|
result = await hass.async_add_executor_job(
|
2018-05-12 21:44:53 +00:00
|
|
|
component.setup, hass, processed_config) # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.exception("Error during setup of component %s", domain)
|
2017-03-05 09:41:54 +00:00
|
|
|
async_notify_setup_error(hass, domain, True)
|
|
|
|
return False
|
2017-03-08 04:31:57 +00:00
|
|
|
finally:
|
2017-06-02 05:44:44 +00:00
|
|
|
end = timer()
|
2018-01-02 20:16:32 +00:00
|
|
|
if warn_task:
|
|
|
|
warn_task.cancel()
|
2017-06-02 05:44:44 +00:00
|
|
|
_LOGGER.info("Setup of domain %s took %.1f seconds.", domain, end - start)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
if result is False:
|
2017-04-30 05:04:49 +00:00
|
|
|
log_error("Component failed to initialize.")
|
2017-03-05 09:41:54 +00:00
|
|
|
return False
|
2018-07-23 08:16:05 +00:00
|
|
|
if result is not True:
|
2017-04-30 05:04:49 +00:00
|
|
|
log_error("Component did not return boolean if setup was successful. "
|
|
|
|
"Disabling component.")
|
2018-05-01 18:57:30 +00:00
|
|
|
loader.set_component(hass, domain, None)
|
2017-03-05 09:41:54 +00:00
|
|
|
return False
|
|
|
|
|
2018-07-17 22:28:44 +00:00
|
|
|
if hass.config_entries:
|
|
|
|
for entry in hass.config_entries.async_entries(domain):
|
|
|
|
await entry.async_setup(hass, component=component)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-05-12 21:44:53 +00:00
|
|
|
hass.config.components.add(component.DOMAIN) # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
# Cleanup
|
2017-03-05 09:41:54 +00:00
|
|
|
if domain in hass.data[DATA_SETUP]:
|
|
|
|
hass.data[DATA_SETUP].pop(domain)
|
|
|
|
|
|
|
|
hass.bus.async_fire(
|
2018-05-12 21:44:53 +00:00
|
|
|
EVENT_COMPONENT_LOADED,
|
|
|
|
{ATTR_COMPONENT: component.DOMAIN} # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def async_prepare_setup_platform(hass: core.HomeAssistant, config: Dict,
|
2018-02-25 11:38:46 +00:00
|
|
|
domain: str, platform_name: str) \
|
2017-03-05 09:41:54 +00:00
|
|
|
-> Optional[ModuleType]:
|
|
|
|
"""Load a platform and makes sure dependencies are setup.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
platform_path = PLATFORM_FORMAT.format(domain, platform_name)
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def log_error(msg: str) -> None:
|
2017-03-05 09:41:54 +00:00
|
|
|
"""Log helper."""
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Unable to prepare setup for platform %s: %s",
|
2017-03-05 09:41:54 +00:00
|
|
|
platform_path, msg)
|
|
|
|
async_notify_setup_error(hass, platform_path)
|
|
|
|
|
2018-05-01 18:57:30 +00:00
|
|
|
platform = loader.get_platform(hass, domain, platform_name)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
# Not found
|
|
|
|
if platform is None:
|
2017-04-30 05:04:49 +00:00
|
|
|
log_error("Platform not found.")
|
2017-03-05 09:41:54 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
# Already loaded
|
2018-07-23 08:16:05 +00:00
|
|
|
if platform_path in hass.config.components:
|
2017-03-05 09:41:54 +00:00
|
|
|
return platform
|
|
|
|
|
2018-01-30 11:30:47 +00:00
|
|
|
try:
|
2018-02-25 11:38:46 +00:00
|
|
|
await async_process_deps_reqs(
|
2018-02-16 22:07:38 +00:00
|
|
|
hass, config, platform_path, platform)
|
2018-01-30 11:30:47 +00:00
|
|
|
except HomeAssistantError as err:
|
|
|
|
log_error(str(err))
|
|
|
|
return None
|
|
|
|
|
|
|
|
return platform
|
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def async_process_deps_reqs(
|
|
|
|
hass: core.HomeAssistant, config: Dict, name: str,
|
|
|
|
module: ModuleType) -> None:
|
2018-01-30 11:30:47 +00:00
|
|
|
"""Process all dependencies and requirements for a module.
|
|
|
|
|
|
|
|
Module is a Python module of either a component or platform.
|
|
|
|
"""
|
|
|
|
processed = hass.data.get(DATA_DEPS_REQS)
|
|
|
|
|
|
|
|
if processed is None:
|
|
|
|
processed = hass.data[DATA_DEPS_REQS] = set()
|
|
|
|
elif name in processed:
|
|
|
|
return
|
|
|
|
|
|
|
|
if hasattr(module, 'DEPENDENCIES'):
|
2018-02-25 11:38:46 +00:00
|
|
|
dep_success = await _async_process_dependencies(
|
2018-07-23 08:24:39 +00:00
|
|
|
hass, config, name, module.DEPENDENCIES) # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
if not dep_success:
|
2018-01-30 11:30:47 +00:00
|
|
|
raise HomeAssistantError("Could not setup all dependencies.")
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2018-01-30 11:30:47 +00:00
|
|
|
if not hass.config.skip_pip and hasattr(module, 'REQUIREMENTS'):
|
2018-02-25 11:38:46 +00:00
|
|
|
req_success = await requirements.async_process_requirements(
|
2018-07-23 08:24:39 +00:00
|
|
|
hass, name, module.REQUIREMENTS) # type: ignore
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
if not req_success:
|
2018-01-30 11:30:47 +00:00
|
|
|
raise HomeAssistantError("Could not install all requirements.")
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2018-01-30 11:30:47 +00:00
|
|
|
processed.add(name)
|