<commit_before>"""
homeassistant.components.automation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Allows to setup simple automation rules via the config file.
"""
import logging

from homeassistant.bootstrap import prepare_setup_platform
from homeassistant.util import split_entity_id
from homeassistant.const import ATTR_ENTITY_ID, CONF_PLATFORM
from homeassistant.components import logbook

DOMAIN = 'automation'

DEPENDENCIES = ['group']

CONF_ALIAS = 'alias'
CONF_SERVICE = 'execute_service'
CONF_SERVICE_ENTITY_ID = 'service_entity_id'
CONF_SERVICE_DATA = 'service_data'

CONF_CONDITION = 'condition'
CONF_ACTION = 'action'
CONF_TRIGGER = 'trigger'
CONF_CONDITION_TYPE = 'condition_type'

CONDITION_USE_TRIGGER_VALUES = 'use_trigger_values'
CONDITION_TYPE_AND = 'and'
CONDITION_TYPE_OR = 'or'

DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND

_LOGGER = logging.getLogger(__name__)


def setup(hass, config):
    """ Sets up automation. """
    config_key = DOMAIN
    found = 1

    while config_key in config:
        # 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)

    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. """

    if CONF_SERVICE not in config:
        _LOGGER.error('Error setting up %s, no action specified.', name)
        return None

    def action():
        """ Action to be executed. """
        _LOGGER.info('Executing %s', name)
        logbook.log_entry(hass, name, 'has been triggered', DOMAIN)

        domain, service = split_entity_id(config[CONF_SERVICE])
        service_data = config.get(CONF_SERVICE_DATA, {})

        if not isinstance(service_data, dict):
            _LOGGER.error("%s should be a dictionary", CONF_SERVICE_DATA)
            service_data = {}

        if CONF_SERVICE_ENTITY_ID in config:
            try:
                service_data[ATTR_ENTITY_ID] = \
                    config[CONF_SERVICE_ENTITY_ID].split(",")
            except AttributeError:<commit_msg>Fixed var name + flake8<commit_after>"""
homeassistant.components.automation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Allows to setup simple automation rules via the config file.
"""
import logging

from homeassistant.bootstrap import prepare_setup_platform
from homeassistant.util import split_entity_id
from homeassistant.const import ATTR_ENTITY_ID, CONF_PLATFORM
from homeassistant.components import logbook

DOMAIN = 'automation'

DEPENDENCIES = ['group']

CONF_ALIAS = 'alias'
CONF_SERVICE = 'execute_service'
CONF_SERVICE_ENTITY_ID = 'service_entity_id'
CONF_SERVICE_DATA = 'service_data'

CONF_CONDITION = 'condition'
CONF_ACTION = 'action'
CONF_TRIGGER = 'trigger'
CONF_CONDITION_TYPE = 'condition_type'

CONDITION_USE_TRIGGER_VALUES = 'use_trigger_values'
CONDITION_TYPE_AND = 'and'
CONDITION_TYPE_OR = 'or'

DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND

_LOGGER = logging.getLogger(__name__)


def setup(hass, config):
    """ Sets up automation. """
    config_key = DOMAIN
    found = 1

    while config_key in config:
        # 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)

    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 config_block or CONF_CONDITION_TYPE in config_block:
        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. """

    if CONF_SERVICE not in config:
        _LOGGER.error('Error setting up %s, no action specified.', name)
        return None

    def action():
        """ Action to be executed. """
        _LOGGER.info('Executing %s', name)
        logbook.log_entry(hass, name, 'has been triggered', DOMAIN)

        domain, service = split_entity_id(config[CONF_SERVICE])
        service_data = config.get(CONF_SERVICE_DATA, {})

        if not isinstance(service_data, dict):
            _LOGGER.error("%s should be a dictionary", CONF_SERVICE_DATA)
            service_data = {}

        if CONF_SERVICE_ENTITY_ID in config:
            try:
                service_data[ATTR_ENTITY_ID] = \
                    config[CONF_SERVICE_ENTITY_ID].split(",")
            except AttributeError:
{'fraction_non_alphanumeric': 0.097441237726867, 'fraction_numerical': 0.001338887235941684, 'mean_word_length': 2.966371681415929, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3623659', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1995, 'n_words': 502}