home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.67k stars 30.8k forks source link

Unable to start MQTT if `embedded` is in configuration.yaml #18228

Closed NopeDK closed 5 years ago

NopeDK commented 6 years ago

Home Assistant release with the issue:

0.81.2

Last working Home Assistant release (if known): Unknown, most likely several months ago

Operating environment (Hass.io/Docker/Windows/etc.):

Distributor ID: Raspbian Description: Raspbian GNU/Linux 9.4 (stretch) Release: 9.4 Codename: stretch

Component/platform:

MQTT Embedded Broker Config

Description of problem: When adding the embedded config, the broker is unable to start.

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

# Enables local mqtt
mqtt:
  embedded:
    listeners:
      default:
        max-connections: 50000
        bind: 0.0.0.0:1883
        type: tcp
      ws-1:
        bind: 0.0.0.0:8080
        type: ws
    auth:
      allow-anonymous: false
      password-file: /home/hassio/.homeassistant/users.pw
    plugins:
      - auth_anonymous
    topic-check:
      enabled: true
      plugins:
        - topic_taboo
  broker: 127.0.0.1
  port: 1883
  client_id: !secret mqtt_id
  username: !secret mqtt_username
  password: !secret mqtt_password

Traceback (if applicable):

Additional information: This is due to a breaking change in async_setup#L371

When this function is called, it calls the _async_server_setup to get the client broker config. _async_setup_server then calls async_start to the parse the configuration.

async_start always returns client_config = None if a custom server config has been added.

        if server_config is None:
            server_config, client_config = generate_config(
                hass, passwd, password)
        else:
            client_config = None

The None is then returned to _async_setup_server and returned to async_setup without change.

    success, broker_config = \
        await server.async_start(
            hass, conf.get(CONF_PASSWORD), conf.get(CONF_EMBEDDED))

    if not success:
        return None

    return broker_config

async_setup now immediately checks if broker_config == None which it is due to the custom server config and terminates the MQTT broker with a failure to start error.

        broker_config = await _async_setup_server(hass, config)

        if broker_config is None:
            _LOGGER.error("Unable to start embedded MQTT broker")
            return False

Previous code used a full logic block afterwards to load the configuration:

    # Only setup if embedded config passed in or no broker specified
    if CONF_EMBEDDED not in conf and CONF_BROKER in conf:
        broker_config = None
    else:
        broker_config = yield from _async_setup_server(hass, config)

    if CONF_BROKER in conf:
        broker = conf[CONF_BROKER]
        port = conf[CONF_PORT]
        username = conf.get(CONF_USERNAME)
        password = conf.get(CONF_PASSWORD)
        certificate = conf.get(CONF_CERTIFICATE)
        client_key = conf.get(CONF_CLIENT_KEY)
        client_cert = conf.get(CONF_CLIENT_CERT)
        tls_insecure = conf.get(CONF_TLS_INSECURE)
        protocol = conf[CONF_PROTOCOL]
    elif broker_config:
        # If no broker passed in, auto config to internal server
        broker, port, username, password, certificate, protocol = broker_config
        # Embedded broker doesn't have some ssl variables
        client_key, client_cert, tls_insecure = None, None, None
        # hbmqtt requires a client id to be set.
        if client_id is None:
            client_id = 'home-assistant'
    else:
        err = "Unable to start MQTT broker."
        if conf.get(CONF_EMBEDDED) is not None:
            # Explicit embedded config, requires explicit broker config
            err += " (Broker configuration required.)"
        _LOGGER.error(err)
        return False
mlewand commented 6 years ago

I can reproduce the same issue on Win10.

pergolafabio commented 5 years ago

is there already a solution for it?

NopeDK commented 5 years ago

AFAIK, no change has been made since I created the issue.

domibarton commented 5 years ago

I reverse-engineered the same issue on my installation and I came to exactly the same findings as @NopeDK. There's no way the embedded MQTT server can be customized, as the client_config / broker_config is always None :(

liuyuelin1 commented 5 years ago

is there already a solution for it? I have the same issue~ Why is this question ignored.

domibarton commented 5 years ago

@liuyuelin1 I was installing mosquitto instead of using the internal MQTT server. Works like a charm and is easy to install, maintain. You also have more tools for mosquitto than for the internal server, which makes it great for debugging!

liuyuelin1 commented 5 years ago

@domibarton Thank you! I'm already using them now.

mlewand commented 5 years ago

FYI: same for me. I've been observing this issue for a while, but the best solution for me was to ditch the builtin component and go with mosquitto too.

martins-pukitis commented 5 years ago

Why was it closed? The issue is still there. People are switching from internal MQTT server to mosquitto due to this.

robbiet480 commented 5 years ago

Sorry, went on a issue closing spree earlier and seemed to have misread the most recent comments on this, reopening.

liuyuelin1 commented 5 years ago

Hopefully someone has figured out how to do it, so if you have a built-in module, don't give it up

domibarton commented 5 years ago

I think it's already clear how to fix it, but someone needs to actually do it ;)

robbiet480 commented 5 years ago

Y'all may want to provide feedback on https://github.com/home-assistant/architecture/issues/186