diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index f501e7fa89c..ea520e88366 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -244,7 +244,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: websocket_api.async_register_command(hass, websocket_subscribe) websocket_api.async_register_command(hass, websocket_mqtt_info) hass.data[DATA_MQTT] = mqtt_data = MqttData(config=mqtt_yaml, client=client) - client.start(mqtt_data) + await client.async_start(mqtt_data) # Restore saved subscriptions if mqtt_data.subscriptions_to_restore: diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index 70e6f573266..0871a0419e5 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -39,9 +39,11 @@ from homeassistant.core import ( ) from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.dispatcher import async_dispatcher_send +from homeassistant.helpers.importlib import async_import_module from homeassistant.helpers.start import async_at_started from homeassistant.helpers.typing import ConfigType from homeassistant.loader import bind_hass +from homeassistant.setup import SetupPhases, async_pause_setup from homeassistant.util.async_ import create_eager_task from homeassistant.util.collection import chunked_or_all from homeassistant.util.logging import catch_log_exception, log_exception @@ -491,13 +493,13 @@ class MQTT: """Handle HA stop.""" await self.async_disconnect() - def start( + async def async_start( self, mqtt_data: MqttData, ) -> None: """Start Home Assistant MQTT client.""" self._mqtt_data = mqtt_data - self.init_client() + await self.async_init_client() @property def subscriptions(self) -> list[Subscription]: @@ -528,8 +530,11 @@ class MQTT: mqttc.on_socket_open = self._async_on_socket_open mqttc.on_socket_register_write = self._async_on_socket_register_write - def init_client(self) -> None: + async def async_init_client(self) -> None: """Initialize paho client.""" + with async_pause_setup(self.hass, SetupPhases.WAIT_IMPORT_PACKAGES): + await async_import_module(self.hass, "paho.mqtt.client") + mqttc = MqttClientSetup(self.conf).client # on_socket_unregister_write and _async_on_socket_close # are only ever called in the event loop