Skip to content

Commit

Permalink
Ensure paho.mqtt.client is imported in the executor (#118412)
Browse files Browse the repository at this point in the history
fixes #118405
  • Loading branch information
bdraco authored and frenck committed May 29, 2024
1 parent 23d9b4b commit 7ee2f09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ async def _setup_client(
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:
Expand Down
11 changes: 8 additions & 3 deletions homeassistant/components/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
)
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
Expand Down Expand Up @@ -491,13 +493,13 @@ async def _async_ha_stop(self, _event: Event) -> None:
"""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]:
Expand Down Expand Up @@ -528,8 +530,11 @@ async def _async_connect_in_executor(self) -> AsyncGenerator[None, None]:
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
Expand Down

0 comments on commit 7ee2f09

Please sign in to comment.