diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 01093bdbded3ed0bce6597db40276ce155f64414..4a6aab534836e1f0645a9f0128287b14ae86913c 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -31,18 +31,46 @@ ATTR_COMPONENT = "component" def setup_component(hass, domain, config=None): - """ Setup a component for Home Assistant. """ - # Check if already loaded + """ Setup a component and all its dependencies. """ + if domain in hass.config.components: - return + return True _ensure_loader_prepared(hass) if config is None: config = defaultdict(dict) + components = loader.load_order_component(domain) + + # OrderedSet is empty if component or dependencies could not be resolved + if not components: + return False + + for component in components: + if component in hass.config.components: + continue + + if not _setup_component(hass, component, config): + return False + + return True + + +def _setup_component(hass, domain, config): + """ Setup a component for Home Assistant. """ component = loader.get_component(domain) + missing_deps = [dep for dep in component.DEPENDENCIES + if dep not in hass.config.components] + + if missing_deps: + _LOGGER.error( + "Not initializing %s because not all dependencies loaded: %s", + domain, ", ".join(missing_deps)) + + return False + try: if component.setup(hass, config): hass.config.components.append(component.DOMAIN) @@ -102,7 +130,7 @@ def from_config_dict(config, hass=None): # Setup the components for domain in loader.load_order_components(components): - setup_component(hass, domain, config) + _setup_component(hass, domain, config) return hass diff --git a/homeassistant/components/discovery.py b/homeassistant/components/discovery.py index c17a20f9414de23416e4b874106215edb669a62b..2a9cb64f6ecb602cabb2cfdc174471ae03dbac60 100644 --- a/homeassistant/components/discovery.py +++ b/homeassistant/components/discovery.py @@ -70,12 +70,17 @@ def setup(hass, config): def new_service_listener(service, info): """ Called when a new service is found. """ with lock: + logger.info("Found new service: %s %s", service, info) + component = SERVICE_HANDLERS.get(service) - logger.info("Found new service: %s %s", service, info) + # We do not know how to handle this service + if not component: + return - if component and component not in hass.config.components: - bootstrap.setup_component(hass, component, config) + # This component cannot be setup. + if not bootstrap.setup_component(hass, component, config): + return hass.bus.fire(EVENT_PLATFORM_DISCOVERED, { ATTR_SERVICE: service, diff --git a/homeassistant/components/scheduler/__init__.py b/homeassistant/components/scheduler/__init__.py index ac990543a3079628b2ecc93feaaa34bdf0258e4c..f84dafd5ec3ba0145d03e96d0a3fd2e631508b90 100644 --- a/homeassistant/components/scheduler/__init__.py +++ b/homeassistant/components/scheduler/__init__.py @@ -35,9 +35,6 @@ _SCHEDULE_FILE = 'schedule.json' def setup(hass, config): """ Create the schedules """ - if DOMAIN in hass.config.components: - return True - def setup_listener(schedule, event_data): """ Creates the event listener based on event_data """ event_type = event_data['type'] @@ -47,9 +44,7 @@ def setup(hass, config): if event_type in ['time']: component = 'scheduler.{}'.format(event_type) - elif component not in hass.config.components and \ - not bootstrap.setup_component(hass, component, config): - + elif not bootstrap.setup_component(hass, component, config): _LOGGER.warn("Could setup event listener for %s", component) return None diff --git a/homeassistant/components/wink.py b/homeassistant/components/wink.py index daa6c92a89396f928f4f36892f81e9d7eaa1b3c6..cfdbf9b1a92b86b3e1054889985e552399ee57e6 100644 --- a/homeassistant/components/wink.py +++ b/homeassistant/components/wink.py @@ -41,8 +41,7 @@ def setup(hass, config): component = get_component(component_name) # Ensure component is loaded - if component.DOMAIN not in hass.config.components: - bootstrap.setup_component(hass, component.DOMAIN, config) + bootstrap.setup_component(hass, component.DOMAIN, config) # Fire discovery event hass.bus.fire(EVENT_PLATFORM_DISCOVERED, { diff --git a/homeassistant/components/zwave.py b/homeassistant/components/zwave.py index 9304ba81a72da1f142727c38b34c053d4351280d..1d798746a6375c81b73bbdcd291dcc3674d067ed 100644 --- a/homeassistant/components/zwave.py +++ b/homeassistant/components/zwave.py @@ -96,8 +96,7 @@ def setup(hass, config): for component, discovery_service, command_ids in DISCOVERY_COMPONENTS: if value.command_class in command_ids: # Ensure component is loaded - if component not in hass.config.components: - bootstrap.setup_component(hass, component, config) + bootstrap.setup_component(hass, component, config) # Fire discovery event hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {