Allow platforms to specify dependencies
This commit is contained in:
parent
d7ab76106d
commit
e630476f9f
2 changed files with 40 additions and 7 deletions
|
@ -30,6 +30,8 @@ _LOGGER = logging.getLogger(__name__)
|
|||
|
||||
ATTR_COMPONENT = "component"
|
||||
|
||||
PLATFORM_FORMAT = '{}.{}'
|
||||
|
||||
|
||||
def setup_component(hass, domain, config=None):
|
||||
""" Setup a component and all its dependencies. """
|
||||
|
@ -95,6 +97,35 @@ def _setup_component(hass, domain, config):
|
|||
return False
|
||||
|
||||
|
||||
def prepare_setup_platform(hass, config, domain, platform_name):
|
||||
""" Loads a platform and makes sure dependencies are setup. """
|
||||
_ensure_loader_prepared(hass)
|
||||
|
||||
platform_path = PLATFORM_FORMAT.format(domain, platform_name)
|
||||
|
||||
platform = loader.get_component(platform_path)
|
||||
|
||||
# Not found
|
||||
if platform is None:
|
||||
_LOGGER.error('Unable to find platform %s', platform_path)
|
||||
return None
|
||||
|
||||
# Already loaded or no dependencies
|
||||
elif (platform_path in hass.config.components or
|
||||
not hasattr(platform, 'DEPENDENCIES')):
|
||||
return platform
|
||||
|
||||
# Load dependencies
|
||||
for component in platform.DEPENDENCIES:
|
||||
if not setup_component(hass, component, config):
|
||||
_LOGGER.error(
|
||||
'Unable to prepare setup for platform %s because not all '
|
||||
'dependencies could be initialized', platform_path)
|
||||
return None
|
||||
|
||||
return platform
|
||||
|
||||
|
||||
# pylint: disable=too-many-branches, too-many-statements
|
||||
def from_config_dict(config, hass=None):
|
||||
"""
|
||||
|
|
|
@ -4,7 +4,7 @@ homeassistant.helpers.entity_component
|
|||
|
||||
Provides helpers for components that manage entities.
|
||||
"""
|
||||
from homeassistant.loader import get_component
|
||||
from homeassistant.bootstrap import prepare_setup_platform
|
||||
from homeassistant.helpers import (
|
||||
generate_entity_id, config_per_platform, extract_entity_ids)
|
||||
from homeassistant.components import group, discovery
|
||||
|
@ -45,7 +45,7 @@ class EntityComponent(object):
|
|||
for p_type, p_config in \
|
||||
config_per_platform(config, self.domain, self.logger):
|
||||
|
||||
self._setup_platform(p_type, p_config)
|
||||
self._setup_platform(config, p_type, p_config)
|
||||
|
||||
if self.discovery_platforms:
|
||||
discovery.listen(self.hass, self.discovery_platforms.keys(),
|
||||
|
@ -115,18 +115,20 @@ class EntityComponent(object):
|
|||
self._update_entity_states,
|
||||
second=range(0, 60, self.scan_interval))
|
||||
|
||||
def _setup_platform(self, platform_type, config, discovery_info=None):
|
||||
def _setup_platform(self, config, platform_type, platform_config,
|
||||
discovery_info=None):
|
||||
""" Tries to setup a platform for this component. """
|
||||
platform_name = '{}.{}'.format(self.domain, platform_type)
|
||||
platform = get_component(platform_name)
|
||||
platform = prepare_setup_platform(
|
||||
self.hass, config, self.domain, platform_type)
|
||||
|
||||
if platform is None:
|
||||
self.logger.error('Unable to find platform %s', platform_type)
|
||||
return
|
||||
|
||||
platform_name = '{}.{}'.format(self.domain, platform_type)
|
||||
|
||||
try:
|
||||
platform.setup_platform(
|
||||
self.hass, config, self.add_entities, discovery_info)
|
||||
self.hass, platform_config, self.add_entities, discovery_info)
|
||||
|
||||
self.hass.config.components.append(platform_name)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue