load cleanups (#23112)

* load cleanups

* Remove unused methods

* Allow importing requirements at the top of a file

* Fix test

* Lint

* Install reqs ASAP when loading platforms
This commit is contained in:
Paulus Schoutsen 2019-04-15 16:45:46 -07:00 committed by GitHub
parent 4d080f8b17
commit 6a2da9f9a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 146 deletions

View file

@ -26,49 +26,16 @@ ERROR_LOG_FILENAME = 'home-assistant.log'
# hass.data key for logging information.
DATA_LOGGING = 'logging'
LOGGING_COMPONENT = {'logger', 'system_log'}
LOGGING_INTEGRATIONS = {'logger', 'system_log'}
FIRST_INIT_COMPONENT = {
STAGE_1_INTEGRATIONS = {
# To record data
'recorder',
'mqtt',
# To make sure we forward data to other instances
'mqtt_eventstream',
'frontend',
'history',
}
def from_config_dict(config: Dict[str, Any],
hass: Optional[core.HomeAssistant] = None,
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]:
"""Try to configure Home Assistant from a configuration dictionary.
Dynamically loads required components and its dependencies.
"""
if hass is None:
hass = core.HomeAssistant()
if config_dir is not None:
config_dir = os.path.abspath(config_dir)
hass.config.config_dir = config_dir
if not is_virtual_env():
hass.loop.run_until_complete(
async_mount_local_lib_path(config_dir))
# run task
hass = hass.loop.run_until_complete(
async_from_config_dict(
config, hass, config_dir, enable_log, verbose, skip_pip,
log_rotate_days, log_file, log_no_color)
)
return hass
async def async_from_config_dict(config: Dict[str, Any],
hass: core.HomeAssistant,
config_dir: Optional[str] = None,
@ -126,15 +93,12 @@ async def async_from_config_dict(config: Dict[str, Any],
domains = _get_domains(hass, config)
# Resolve all dependencies of all components.
for dep_domains in await asyncio.gather(*[
loader.async_component_dependencies(hass, domain)
for domain in domains
], return_exceptions=True):
# 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)
# Resolve all dependencies of all components so we can find the logging
# and integrations that need faster initialization.
resolved_domains_task = asyncio.gather(*[
loader.async_component_dependencies(hass, domain)
for domain in domains
], return_exceptions=True)
# Set up core.
if not all(await asyncio.gather(
@ -147,14 +111,22 @@ async def async_from_config_dict(config: Dict[str, Any],
_LOGGER.debug("Home Assistant core initialized")
# setup components
# stage 0, load logging components
for domain in domains:
if domain in LOGGING_COMPONENT:
hass.async_create_task(
async_setup_component(hass, domain, config))
# 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)
await hass.async_block_till_done()
# setup components
logging_domains = domains & LOGGING_INTEGRATIONS
stage_1_domains = domains & STAGE_1_INTEGRATIONS
stage_2_domains = domains - logging_domains - stage_1_domains
await asyncio.gather(*[
async_setup_component(hass, domain, config)
for domain in logging_domains
])
# Kick off loading the registries. They don't need to be awaited.
asyncio.gather(
@ -162,19 +134,15 @@ async def async_from_config_dict(config: Dict[str, Any],
hass.helpers.entity_registry.async_get_registry(),
hass.helpers.area_registry.async_get_registry())
# stage 1
for domain in domains:
if domain in FIRST_INIT_COMPONENT:
hass.async_create_task(
async_setup_component(hass, domain, config))
await hass.async_block_till_done()
# stage 2
for domain in domains:
if domain in FIRST_INIT_COMPONENT or domain in LOGGING_COMPONENT:
# Continue setting up the components
for to_load in (stage_1_domains, stage_2_domains):
if not to_load:
continue
hass.async_create_task(async_setup_component(hass, domain, config))
await asyncio.gather(*[
async_setup_component(hass, domain, config)
for domain in to_load
])
await hass.async_block_till_done()
@ -229,32 +197,6 @@ async def async_from_config_dict(config: Dict[str, Any],
return hass
def from_config_file(config_path: str,
hass: Optional[core.HomeAssistant] = None,
verbose: bool = False,
skip_pip: bool = True,
log_rotate_days: Any = None,
log_file: Any = None,
log_no_color: bool = False)\
-> Optional[core.HomeAssistant]:
"""Read the configuration file and try to start all the functionality.
Will add functionality to 'hass' parameter if given,
instantiates a new Home Assistant object if 'hass' is not given.
"""
if hass is None:
hass = core.HomeAssistant()
# run task
hass = hass.loop.run_until_complete(
async_from_config_file(
config_path, hass, verbose, skip_pip,
log_rotate_days, log_file, log_no_color)
)
return hass
async def async_from_config_file(config_path: str,
hass: core.HomeAssistant,
verbose: bool = False,