mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Made bootstrap.setup_component more robust
This commit is contained in:
parent
58812b326c
commit
2863c2d593
@ -31,18 +31,46 @@ ATTR_COMPONENT = "component"
|
|||||||
|
|
||||||
|
|
||||||
def setup_component(hass, domain, config=None):
|
def setup_component(hass, domain, config=None):
|
||||||
""" Setup a component for Home Assistant. """
|
""" Setup a component and all its dependencies. """
|
||||||
# Check if already loaded
|
|
||||||
if domain in hass.config.components:
|
if domain in hass.config.components:
|
||||||
return
|
return True
|
||||||
|
|
||||||
_ensure_loader_prepared(hass)
|
_ensure_loader_prepared(hass)
|
||||||
|
|
||||||
if config is None:
|
if config is None:
|
||||||
config = defaultdict(dict)
|
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)
|
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:
|
try:
|
||||||
if component.setup(hass, config):
|
if component.setup(hass, config):
|
||||||
hass.config.components.append(component.DOMAIN)
|
hass.config.components.append(component.DOMAIN)
|
||||||
@ -102,7 +130,7 @@ def from_config_dict(config, hass=None):
|
|||||||
|
|
||||||
# Setup the components
|
# Setup the components
|
||||||
for domain in loader.load_order_components(components):
|
for domain in loader.load_order_components(components):
|
||||||
setup_component(hass, domain, config)
|
_setup_component(hass, domain, config)
|
||||||
|
|
||||||
return hass
|
return hass
|
||||||
|
|
||||||
|
@ -70,12 +70,17 @@ def setup(hass, config):
|
|||||||
def new_service_listener(service, info):
|
def new_service_listener(service, info):
|
||||||
""" Called when a new service is found. """
|
""" Called when a new service is found. """
|
||||||
with lock:
|
with lock:
|
||||||
component = SERVICE_HANDLERS.get(service)
|
|
||||||
|
|
||||||
logger.info("Found new service: %s %s", service, info)
|
logger.info("Found new service: %s %s", service, info)
|
||||||
|
|
||||||
if component and component not in hass.config.components:
|
component = SERVICE_HANDLERS.get(service)
|
||||||
bootstrap.setup_component(hass, component, config)
|
|
||||||
|
# We do not know how to handle this service
|
||||||
|
if not component:
|
||||||
|
return
|
||||||
|
|
||||||
|
# This component cannot be setup.
|
||||||
|
if not bootstrap.setup_component(hass, component, config):
|
||||||
|
return
|
||||||
|
|
||||||
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
||||||
ATTR_SERVICE: service,
|
ATTR_SERVICE: service,
|
||||||
|
@ -35,9 +35,6 @@ _SCHEDULE_FILE = 'schedule.json'
|
|||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
""" Create the schedules """
|
""" Create the schedules """
|
||||||
|
|
||||||
if DOMAIN in hass.config.components:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def setup_listener(schedule, event_data):
|
def setup_listener(schedule, event_data):
|
||||||
""" Creates the event listener based on event_data """
|
""" Creates the event listener based on event_data """
|
||||||
event_type = event_data['type']
|
event_type = event_data['type']
|
||||||
@ -47,9 +44,7 @@ def setup(hass, config):
|
|||||||
if event_type in ['time']:
|
if event_type in ['time']:
|
||||||
component = 'scheduler.{}'.format(event_type)
|
component = 'scheduler.{}'.format(event_type)
|
||||||
|
|
||||||
elif component not in hass.config.components and \
|
elif not bootstrap.setup_component(hass, component, config):
|
||||||
not bootstrap.setup_component(hass, component, config):
|
|
||||||
|
|
||||||
_LOGGER.warn("Could setup event listener for %s", component)
|
_LOGGER.warn("Could setup event listener for %s", component)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -41,8 +41,7 @@ def setup(hass, config):
|
|||||||
component = get_component(component_name)
|
component = get_component(component_name)
|
||||||
|
|
||||||
# Ensure component is loaded
|
# 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
|
# Fire discovery event
|
||||||
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
||||||
|
@ -96,8 +96,7 @@ def setup(hass, config):
|
|||||||
for component, discovery_service, command_ids in DISCOVERY_COMPONENTS:
|
for component, discovery_service, command_ids in DISCOVERY_COMPONENTS:
|
||||||
if value.command_class in command_ids:
|
if value.command_class in command_ids:
|
||||||
# Ensure component is loaded
|
# 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
|
# Fire discovery event
|
||||||
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user