From e4c5108c9deb8ecdf296f7c8da5f9dc26789d930 Mon Sep 17 00:00:00 2001 From: Stefan Jonasson Date: Fri, 18 Sep 2015 18:12:27 +0200 Subject: [PATCH] Implemented configuration loading from --- .../components/automation/__init__.py | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 45859617624..6243a2f50af 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -40,27 +40,47 @@ def setup(hass, config): found = 1 while config_key in config: - p_config = _migrate_old_config(config[config_key]) + # check for one block syntax + if isinstance(config[config_key], dict): + config_block = _migrate_old_config(config[config_key]) + name = config_block.get(CONF_ALIAS, config_key) + _setup_automation(hass, config_block, name, config) + + # check for multiple block syntax + elif isinstance(config[config_key], list): + list_no = 0 + for config_block in config[config_key]: + name = config_block.get(CONF_ALIAS, "{}, {}".format(config_key, list_no)) + list_no += 1 + config_block = _migrate_old_config(config_block) + _setup_automation(hass, config_block, name, config) + + # any scalar value is incorrect + else: + _LOGGER.error('Error in config in section %s.', config_key) + found += 1 config_key = "{} {}".format(DOMAIN, found) - name = p_config.get(CONF_ALIAS, config_key) - action = _get_action(hass, p_config.get(CONF_ACTION, {}), name) - - if action is None: - continue - - if CONF_CONDITION in p_config or CONF_CONDITION_TYPE in p_config: - action = _process_if(hass, config, p_config, action) - - if action is None: - continue - - _process_trigger(hass, config, p_config.get(CONF_TRIGGER, []), name, - action) - return True +def _setup_automation(hass, config_block, name, config): + """ Setup one instance of automation """ + + action = _get_action(hass, config_block.get(CONF_ACTION, {}), name) + + if action is None: + return False + + if CONF_CONDITION in p_config or CONF_CONDITION_TYPE in p_config: + action = _process_if(hass, config, config_block, action) + + if action is None: + return False + + _process_trigger(hass, config, config_block.get(CONF_TRIGGER, []), name, + action) + return True def _get_action(hass, config, name): """ Return an action based on a config. """