def setup(hass, config):  # pylint: disable=too-many-locals
    """Setup the MySensors component."""
    if not validate_config(config,
                           {DOMAIN: [CONF_GATEWAYS]},
                           _LOGGER):
        return False
    if not all(CONF_DEVICE in gateway
               for gateway in config[DOMAIN][CONF_GATEWAYS]):
        _LOGGER.error('Missing required configuration items '
                      'in %s: %s', DOMAIN, CONF_DEVICE)
        return False

    import mysensors.mysensors as mysensors

    version = str(config[DOMAIN].get(CONF_VERSION, DEFAULT_VERSION))
    is_metric = (hass.config.temperature_unit == TEMP_CELSIUS)
    persistence = config[DOMAIN].get(CONF_PERSISTENCE, True)

    def setup_gateway(device, persistence_file, baud_rate, tcp_port):
        """Return gateway after setup of the gateway."""
        try:
            socket.inet_aton(device)
            # valid ip address
            gateway = mysensors.TCPGateway(
                device, event_callback=None, persistence=persistence,
                persistence_file=persistence_file, protocol_version=version,
                port=tcp_port)
        except __HOLE__:
            # invalid ip address
            gateway = mysensors.SerialGateway(
                device, event_callback=None, persistence=persistence,
                persistence_file=persistence_file, protocol_version=version,
                baud=baud_rate)
        gateway.metric = is_metric
        gateway.debug = config[DOMAIN].get(CONF_DEBUG, False)
        optimistic = config[DOMAIN].get(CONF_OPTIMISTIC, False)
        gateway = GatewayWrapper(gateway, version, optimistic)
        # pylint: disable=attribute-defined-outside-init
        gateway.event_callback = gateway.callback_factory()

        def gw_start(event):
            """Callback to trigger start of gateway and any persistence."""
            gateway.start()
            hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
                                 lambda event: gateway.stop())
            if persistence:
                for node_id in gateway.sensors:
                    gateway.event_callback('persistence', node_id)

        hass.bus.listen_once(EVENT_HOMEASSISTANT_START, gw_start)

        return gateway

    # Setup all devices from config
    global GATEWAYS
    GATEWAYS = {}
    conf_gateways = config[DOMAIN][CONF_GATEWAYS]
    if isinstance(conf_gateways, dict):
        conf_gateways = [conf_gateways]

    for index, gway in enumerate(conf_gateways):
        device = gway[CONF_DEVICE]
        persistence_file = gway.get(
            CONF_PERSISTENCE_FILE,
            hass.config.path('mysensors{}.pickle'.format(index + 1)))
        baud_rate = gway.get(CONF_BAUD_RATE, DEFAULT_BAUD_RATE)
        tcp_port = gway.get(CONF_TCP_PORT, DEFAULT_TCP_PORT)
        GATEWAYS[device] = setup_gateway(
            device, persistence_file, baud_rate, tcp_port)

    for (component, discovery_service) in DISCOVERY_COMPONENTS:
        # Ensure component is loaded
        if not bootstrap.setup_component(hass, component, config):
            return False
        # Fire discovery event
        hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
            ATTR_SERVICE: discovery_service,
            ATTR_DISCOVERED: {}})

    return True
dataset/ETHPy150Open home-assistant/home-assistant/homeassistant/components/mysensors.py/setup