Components+configuration now loaded dynamically

A major change to the bootstrapping of Home Assistant decoupling the
knowledge in bootstrap for a more dynamic approach. This refactoring
also prepares the code for different configuration backends and the
loading components from different places.
This commit is contained in:
Paulus Schoutsen committed 2014-08-13 14:28:45 +02:00
1 parent cb33b3bf24
commit 997c2e8ef6
19 files changed
+473 -309

No files matched your search

+38 -8
View File
@@ -15,6 +15,7 @@ Each component should publish services only under its own domain.
"""
import itertools as it
import logging
import importlib
import homeassistant as ha
@@ -44,23 +45,51 @@ SERVICE_MEDIA_NEXT_TRACK = "media_next_track"
SERVICE_MEDIA_PREV_TRACK = "media_prev_track"
def _get_component(component):
""" Returns requested component. """
def get_component(component, logger=None):
""" Tries to load specified component.
Only returns it if also found to be valid."""
try:
return importlib.import_module(
comp = importlib.import_module(
'homeassistant.components.{}'.format(component))
except ImportError:
# If we got a bogus component the input will fail
if logger:
logger.error(
"Failed to find component {}".format(component))
return None
# Validation if component has required methods and attributes
errors = []
if not hasattr(comp, 'DOMAIN'):
errors.append("Missing DOMAIN attribute")
if not hasattr(comp, 'DEPENDENCIES'):
errors.append("Missing DEPENDENCIES attribute")
if not hasattr(comp, 'setup'):
errors.append("Missing setup method")
if errors:
if logger:
logger.error("Found invalid component {}: {}".format(
component, ", ".join(errors)))
return None
else:
return comp
def is_on(hass, entity_id=None):
""" Loads up the module to call the is_on method.
If there is no entity id given we will check all. """
logger = logging.getLogger(__name__)
if entity_id:
group = _get_component('group')
group = get_component('group', logger)
entity_ids = group.expand_entity_ids([entity_id])
else:
@@ -69,7 +98,7 @@ def is_on(hass, entity_id=None):
for entity_id in entity_ids:
domain = util.split_entity_id(entity_id)[0]
module = _get_component(domain)
module = get_component(domain, logger)
try:
if module.is_on(hass, entity_id):
@@ -100,7 +129,7 @@ def extract_entity_ids(hass, service):
entity_ids = []
if service.data and ATTR_ENTITY_ID in service.data:
group = _get_component('group')
group = get_component('group')
# Entity ID attr can be a list or a string
service_ent_id = service.data[ATTR_ENTITY_ID]
@@ -117,7 +146,8 @@ def extract_entity_ids(hass, service):
return entity_ids
def setup(hass):
# pylint: disable=unused-argument
def setup(hass, config):
""" Setup general services related to homeassistant. """
def handle_turn_service(service):