2014-12-07 07:57:02 +00:00
|
|
|
"""
|
|
|
|
Helper methods for components within Home Assistant.
|
|
|
|
"""
|
2015-09-29 06:09:05 +00:00
|
|
|
import re
|
|
|
|
|
2015-06-02 05:55:33 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID, CONF_PLATFORM, DEVICE_DEFAULT_NAME)
|
2015-01-06 07:02:41 +00:00
|
|
|
from homeassistant.util import ensure_unique_string, slugify
|
2014-12-07 07:57:02 +00:00
|
|
|
|
|
|
|
|
2015-01-19 08:02:25 +00:00
|
|
|
def generate_entity_id(entity_id_format, name, current_ids=None, hass=None):
|
2015-01-20 05:39:24 +00:00
|
|
|
""" Generate a unique entity ID based on given entity IDs or used ids. """
|
2015-06-02 05:55:33 +00:00
|
|
|
name = name.lower() or DEVICE_DEFAULT_NAME.lower()
|
2015-01-19 08:02:25 +00:00
|
|
|
if current_ids is None:
|
|
|
|
if hass is None:
|
|
|
|
raise RuntimeError("Missing required parameter currentids or hass")
|
|
|
|
|
|
|
|
current_ids = hass.states.entity_ids()
|
|
|
|
|
|
|
|
return ensure_unique_string(
|
2015-02-06 08:17:30 +00:00
|
|
|
entity_id_format.format(slugify(name.lower())), current_ids)
|
2015-01-19 08:02:25 +00:00
|
|
|
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
def validate_config(config, items, logger):
|
|
|
|
"""
|
|
|
|
Validates 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.
|
|
|
|
|
|
|
|
Returns 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
|
|
|
|
|
|
|
|
|
|
|
|
def config_per_platform(config, domain, logger):
|
|
|
|
"""
|
|
|
|
Generator to break a component config into different platforms.
|
|
|
|
For example, will find 'switch', 'switch 2', 'switch 3', .. etc
|
|
|
|
"""
|
|
|
|
config_key = domain
|
|
|
|
found = 1
|
|
|
|
|
2015-09-29 06:09:05 +00:00
|
|
|
for config_key in extract_domain_configs(config, domain):
|
2014-12-07 07:57:02 +00:00
|
|
|
platform_config = config[config_key]
|
2015-03-10 23:16:53 +00:00
|
|
|
if not isinstance(platform_config, list):
|
|
|
|
platform_config = [platform_config]
|
2014-12-07 07:57:02 +00:00
|
|
|
|
2015-03-10 23:16:53 +00:00
|
|
|
for item in platform_config:
|
|
|
|
platform_type = item.get(CONF_PLATFORM)
|
2014-12-07 07:57:02 +00:00
|
|
|
|
2015-03-10 23:16:53 +00:00
|
|
|
if platform_type is None:
|
|
|
|
logger.warning('No platform specified for %s', config_key)
|
|
|
|
continue
|
2014-12-07 07:57:02 +00:00
|
|
|
|
2015-03-10 23:16:53 +00:00
|
|
|
yield platform_type, item
|
2014-12-07 07:57:02 +00:00
|
|
|
|
|
|
|
found += 1
|
|
|
|
config_key = "{} {}".format(domain, found)
|
2015-09-29 06:09:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
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))
|