Skip to content

Commit

Permalink
Made bootstrap.setup_component more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Mar 22, 2015
1 parent 58812b3 commit 2863c2d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
36 changes: 32 additions & 4 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,46 @@


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)
Expand Down Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions homeassistant/components/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions homeassistant/components/scheduler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
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']
Expand All @@ -47,9 +44,7 @@ def setup_listener(schedule, event_data):
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

Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/wink.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/zwave.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def value_added(node, value):
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, {
Expand Down

0 comments on commit 2863c2d

Please sign in to comment.