core/homeassistant/helpers/__init__.py

50 lines
1.6 KiB
Python
Raw Normal View History

2016-03-07 22:39:52 +00:00
"""Helper methods for components within Home Assistant."""
2015-09-29 06:09:05 +00:00
import re
from homeassistant.const import CONF_PLATFORM
def validate_config(config, items, logger):
2016-03-07 22:39:52 +00:00
"""Validate if all items are available in the configuration.
config is the general dictionary with all the configurations.
items is a dict with per domain which attributes we require.
logger is the logger from the caller to log the errors to.
2016-03-07 22:39:52 +00:00
Return True if all required items were found.
"""
errors_found = False
for domain in items.keys():
config.setdefault(domain, {})
errors = [item for item in items[domain] if item not in config[domain]]
if errors:
logger.error(
"Missing required configuration items in {}: {}".format(
domain, ", ".join(errors)))
errors_found = True
return not errors_found
2016-03-28 01:48:51 +00:00
def config_per_platform(config, domain):
2016-03-07 22:39:52 +00:00
"""Generator to break a component config into different platforms.
For example, will find 'switch', 'switch 2', 'switch 3', .. etc
"""
2015-09-29 06:09:05 +00:00
for config_key in extract_domain_configs(config, domain):
platform_config = config[config_key]
if not isinstance(platform_config, list):
platform_config = [platform_config]
for item in platform_config:
2016-03-28 01:48:51 +00:00
yield item.get(CONF_PLATFORM), item
2015-09-29 06:09:05 +00:00
def extract_domain_configs(config, domain):
2016-03-07 22:39:52 +00:00
"""Extract keys from config for given domain name."""
2015-09-29 06:09:05 +00:00
pattern = re.compile(r'^{}(| .+)$'.format(domain))
return [key for key in config.keys() if pattern.match(key)]