<commit_before>"""
homeassistant.bootstrap
~~~~~~~~~~~~~~~~~~~~~~~
Provides methods to bootstrap a home assistant instance.

Each method will return a tuple (bus, statemachine).

After bootstrapping you can add your own components or
start by calling homeassistant.start_home_assistant(bus)
"""

import os
import configparser
import yaml
import io
import logging
from collections import defaultdict

import homeassistant
import homeassistant.loader as loader
import homeassistant.components as core_components
import homeassistant.components.group as group
from homeassistant.const import EVENT_COMPONENT_LOADED

_LOGGER = logging.getLogger(__name__)

ATTR_COMPONENT = "component"


def setup_component(hass, domain, config=None):
    """ Setup a component for Home Assistant. """
    # Check if already loaded
    if domain in hass.components:
        return

    _ensure_loader_prepared(hass)

    if config is None:
        config = defaultdict(dict)

    component = loader.get_component(domain)

    try:
        if component.setup(hass, config):
            hass.components.append(component.DOMAIN)

            # Assumption: if a component does not depend on groups
            # it communicates with devices
            if group.DOMAIN not in component.DEPENDENCIES:
                hass.pool.add_worker()

            hass.bus.fire(
                EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: component.DOMAIN})

            return True

        else:
            _LOGGER.error("component %s failed to initialize", domain)

    except Exception:  # pylint: disable=broad-except
        _LOGGER.exception("Error during setup of component %s", domain)

    return False


# pylint: disable=too-many-branches, too-many-statements
def from_config_dict(config, hass=None):
    """
    Tries to configure Home Assistant from a config dict.

    Dynamically loads required components and its dependencies.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

    enable_logging(hass)

    _ensure_loader_prepared(hass)

    # Make a copy because we are mutating it.
    # Convert it to defaultdict so components can always have config dict
    config = defaultdict(dict, config)

    # Filter out the repeating and common config section [homeassistant]
    components = (key for key in config.keys()
                  if ' ' not in key and key != homeassistant.DOMAIN)

    if not core_components.setup(hass, config):
        _LOGGER.error("Home Assistant core failed to initialize. "
                      "Further initialization aborted.")

        return hass

    _LOGGER.info("Home Assistant core initialized")<commit_msg>Now converting config values to dictionaries if they are None<commit_after>"""
homeassistant.bootstrap
~~~~~~~~~~~~~~~~~~~~~~~
Provides methods to bootstrap a home assistant instance.

Each method will return a tuple (bus, statemachine).

After bootstrapping you can add your own components or
start by calling homeassistant.start_home_assistant(bus)
"""

import os
import configparser
import yaml
import io
import logging
from collections import defaultdict

import homeassistant
import homeassistant.loader as loader
import homeassistant.components as core_components
import homeassistant.components.group as group
from homeassistant.const import EVENT_COMPONENT_LOADED

_LOGGER = logging.getLogger(__name__)

ATTR_COMPONENT = "component"


def setup_component(hass, domain, config=None):
    """ Setup a component for Home Assistant. """
    # Check if already loaded
    if domain in hass.components:
        return

    _ensure_loader_prepared(hass)

    if config is None:
        config = defaultdict(dict)

    component = loader.get_component(domain)

    try:
        if component.setup(hass, config):
            hass.components.append(component.DOMAIN)

            # Assumption: if a component does not depend on groups
            # it communicates with devices
            if group.DOMAIN not in component.DEPENDENCIES:
                hass.pool.add_worker()

            hass.bus.fire(
                EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: component.DOMAIN})

            return True

        else:
            _LOGGER.error("component %s failed to initialize", domain)

    except Exception:  # pylint: disable=broad-except
        _LOGGER.exception("Error during setup of component %s", domain)

    return False


# pylint: disable=too-many-branches, too-many-statements
def from_config_dict(config, hass=None):
    """
    Tries to configure Home Assistant from a config dict.

    Dynamically loads required components and its dependencies.
    """
    if hass is None:
        hass = homeassistant.HomeAssistant()

    enable_logging(hass)

    _ensure_loader_prepared(hass)

    # Make a copy because we are mutating it.
    # Convert it to defaultdict so components can always have config dict
    # Convert values to dictionaries if they are None
    config = defaultdict(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 != homeassistant.DOMAIN)

    if not core_components.setup(hass, config):
        _LOGGER.error("Home Assistant core failed to initialize. "
                      "Further initialization aborted.")

        return hass

    _LOGGER.info("Home Assistant core initialized")
{'fraction_non_alphanumeric': 0.07495866250229653, 'fraction_numerical': 0.0, 'mean_word_length': 3.118003025718608, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3681012', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1414, 'n_words': 499}