Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 6 additions & 31 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import Event, ServiceCall, callback
from homeassistant.exceptions import (
ConfigEntryNotReady,
HomeAssistantError,
Unauthorized,
)
from homeassistant.exceptions import HomeAssistantError, Unauthorized
from homeassistant.helpers import config_validation as cv, event, template
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
Expand Down Expand Up @@ -649,13 +645,7 @@ async def async_setup_entry(hass, entry):
tls_version=tls_version,
)

result: str = await hass.data[DATA_MQTT].async_connect()

if result == CONNECTION_FAILED:
return False

if result == CONNECTION_FAILED_RECOVERABLE:
raise ConfigEntryNotReady
hass.data[DATA_MQTT].async_connect()

async def async_stop_mqtt(_event: Event):
"""Stop MQTT component."""
Expand Down Expand Up @@ -824,26 +814,10 @@ async def async_publish(
self._mqttc.publish, topic, payload, qos, retain
)

async def async_connect(self) -> str:
def async_connect(self) -> str:
"""Connect to the host. Does process messages yet."""
# pylint: disable=import-outside-toplevel
import paho.mqtt.client as mqtt

result: int = None
try:
result = await self.hass.async_add_executor_job(
self._mqttc.connect, self.broker, self.port, self.keepalive
)
except OSError as err:
_LOGGER.error("Failed to connect due to exception: %s", err)
return CONNECTION_FAILED_RECOVERABLE

if result != 0:
_LOGGER.error("Failed to connect: %s", mqtt.error_string(result))
return CONNECTION_FAILED

self._mqttc.connect_async(self.broker, self.port, self.keepalive)
self._mqttc.loop_start()
return CONNECTION_SUCCESS

async def async_disconnect(self):
"""Stop the MQTT client."""
Expand Down Expand Up @@ -933,6 +907,7 @@ def _mqtt_on_connect(self, _mqttc, _userdata, _flags, result_code: int) -> None:
return

self.connected = True
_LOGGER.info("Connected to MQTT server (%s)", result_code)

# Group subscriptions to only re-subscribe once for each topic.
keyfunc = attrgetter("topic")
Expand Down Expand Up @@ -999,7 +974,7 @@ def _mqtt_handle_message(self, msg) -> None:
def _mqtt_on_disconnect(self, _mqttc, _userdata, result_code: int) -> None:
"""Disconnected callback."""
self.connected = False
_LOGGER.warning("Disconnected from MQTT (%s).", result_code)
_LOGGER.warning("Disconnected from MQTT server (%s)", result_code)


def _raise_on_error(result_code: int) -> None:
Expand Down