From 68c2b539ee2a757955eb398a186cda510bad382c Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 28 Sep 2015 23:09:05 -0700 Subject: [PATCH] More flexible domain config extraction --- homeassistant/bootstrap.py | 4 ++-- homeassistant/helpers/__init__.py | 10 +++++++++- tests/helpers/test_init.py | 19 +++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index b2e5fa51540..daee13914fd 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -186,8 +186,8 @@ def from_config_dict(config, hass=None, config_dir=None, enable_log=True, dict, {key: value or {} for key, value in config.items()}) # Filter out the repeating and common config section [homeassistant] - components = (key for key in config.keys() - if ' ' not in key and key != core.DOMAIN) + components = set(key.split(' ')[0] for key in config.keys() + if key != core.DOMAIN) if not core_components.setup(hass, config): _LOGGER.error('Home Assistant core failed to initialize. ' diff --git a/homeassistant/helpers/__init__.py b/homeassistant/helpers/__init__.py index 286eed4654e..021146d1c32 100644 --- a/homeassistant/helpers/__init__.py +++ b/homeassistant/helpers/__init__.py @@ -1,6 +1,8 @@ """ Helper methods for components within Home Assistant. """ +import re + from homeassistant.loader import get_component from homeassistant.const import ( ATTR_ENTITY_ID, CONF_PLATFORM, DEVICE_DEFAULT_NAME) @@ -73,7 +75,7 @@ def config_per_platform(config, domain, logger): config_key = domain found = 1 - while config_key in config: + for config_key in extract_domain_configs(config, domain): platform_config = config[config_key] if not isinstance(platform_config, list): platform_config = [platform_config] @@ -89,3 +91,9 @@ def config_per_platform(config, domain, logger): found += 1 config_key = "{} {}".format(domain, found) + + +def extract_domain_configs(config, domain): + """ Extract keys from config for given domain name. """ + pattern = re.compile(r'^{}(| .+)$'.format(domain)) + return (key for key in config.keys() if pattern.match(key)) diff --git a/tests/helpers/test_init.py b/tests/helpers/test_init.py index 0e7c310d91f..5899ef3a943 100644 --- a/tests/helpers/test_init.py +++ b/tests/helpers/test_init.py @@ -8,9 +8,8 @@ Tests component helpers. import unittest import homeassistant.core as ha -import homeassistant.loader as loader +from homeassistant import loader, helpers from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID -from homeassistant.helpers import extract_entity_ids from tests.common import get_test_home_assistant @@ -39,10 +38,22 @@ class TestComponentsCore(unittest.TestCase): {ATTR_ENTITY_ID: 'light.Bowl'}) self.assertEqual(['light.bowl'], - extract_entity_ids(self.hass, call)) + helpers.extract_entity_ids(self.hass, call)) call = ha.ServiceCall('light', 'turn_on', {ATTR_ENTITY_ID: 'group.test'}) self.assertEqual(['light.ceiling', 'light.kitchen'], - extract_entity_ids(self.hass, call)) + helpers.extract_entity_ids(self.hass, call)) + + def test_extract_domain_configs(self): + config = { + 'zone': None, + 'zoner': None, + 'zone ': None, + 'zone Hallo': None, + 'zone 100': None, + } + + self.assertEqual(set(['zone', 'zone Hallo', 'zone 100']), + set(helpers.extract_domain_configs(config, 'zone')))