More flexible domain config extraction

This commit is contained in:
Paulus Schoutsen 2015-09-28 23:09:05 -07:00
parent 755234369d
commit 68c2b539ee
3 changed files with 26 additions and 7 deletions

View File

@ -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. '

View File

@ -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))

View File

@ -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')))