Skip to content

Commit

Permalink
Allow platforms to specify dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed May 12, 2015
1 parent d7ab761 commit e630476
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
31 changes: 31 additions & 0 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

ATTR_COMPONENT = "component"

PLATFORM_FORMAT = '{}.{}'


def setup_component(hass, domain, config=None):
""" Setup a component and all its dependencies. """
Expand Down Expand Up @@ -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):
"""
Expand Down
16 changes: 9 additions & 7 deletions homeassistant/helpers/entity_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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
Expand Down Expand Up @@ -45,7 +45,7 @@ def setup(self, config):
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(),
Expand Down Expand Up @@ -115,18 +115,20 @@ def _start_polling(self):
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)

Expand Down

0 comments on commit e630476

Please sign in to comment.