Ensure paho.mqtt.client is imported in the executor (#118412)

fixes #118405
This commit is contained in:
J. Nick Koston 2024-05-29 09:55:34 -10:00 committed by GitHub
parent 5fae2bd7c5
commit 1743d1700d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 4 deletions

View File

@ -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:

View File

@ -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