Fix blocking I/O of SSLContext.load_default_certs in Ecovacs (#123856)
This commit is contained in:
parent
e33a7ecefa
commit
c761d75550
2 changed files with 31 additions and 19 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import partial
|
||||
import logging
|
||||
import ssl
|
||||
from typing import Any
|
||||
|
@ -100,11 +101,14 @@ async def _validate_input(
|
|||
if not user_input.get(CONF_VERIFY_MQTT_CERTIFICATE, True) and mqtt_url:
|
||||
ssl_context = get_default_no_verify_context()
|
||||
|
||||
mqtt_config = create_mqtt_config(
|
||||
device_id=device_id,
|
||||
country=country,
|
||||
override_mqtt_url=mqtt_url,
|
||||
ssl_context=ssl_context,
|
||||
mqtt_config = await hass.async_add_executor_job(
|
||||
partial(
|
||||
create_mqtt_config,
|
||||
device_id=device_id,
|
||||
country=country,
|
||||
override_mqtt_url=mqtt_url,
|
||||
ssl_context=ssl_context,
|
||||
)
|
||||
)
|
||||
|
||||
client = MqttClient(mqtt_config, authenticator)
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from functools import partial
|
||||
import logging
|
||||
import ssl
|
||||
from typing import Any
|
||||
|
@ -64,32 +65,28 @@ class EcovacsController:
|
|||
if not config.get(CONF_VERIFY_MQTT_CERTIFICATE, True) and mqtt_url:
|
||||
ssl_context = get_default_no_verify_context()
|
||||
|
||||
self._mqtt = MqttClient(
|
||||
create_mqtt_config(
|
||||
device_id=self._device_id,
|
||||
country=country,
|
||||
override_mqtt_url=mqtt_url,
|
||||
ssl_context=ssl_context,
|
||||
),
|
||||
self._authenticator,
|
||||
self._mqtt_config_fn = partial(
|
||||
create_mqtt_config,
|
||||
device_id=self._device_id,
|
||||
country=country,
|
||||
override_mqtt_url=mqtt_url,
|
||||
ssl_context=ssl_context,
|
||||
)
|
||||
self._mqtt_client: MqttClient | None = None
|
||||
|
||||
self._added_legacy_entities: set[str] = set()
|
||||
|
||||
async def initialize(self) -> None:
|
||||
"""Init controller."""
|
||||
mqtt_config_verfied = False
|
||||
try:
|
||||
devices = await self._api_client.get_devices()
|
||||
credentials = await self._authenticator.authenticate()
|
||||
for device_config in devices:
|
||||
if isinstance(device_config, DeviceInfo):
|
||||
# MQTT device
|
||||
if not mqtt_config_verfied:
|
||||
await self._mqtt.verify_config()
|
||||
mqtt_config_verfied = True
|
||||
device = Device(device_config, self._authenticator)
|
||||
await device.initialize(self._mqtt)
|
||||
mqtt = await self._get_mqtt_client()
|
||||
await device.initialize(mqtt)
|
||||
self._devices.append(device)
|
||||
else:
|
||||
# Legacy device
|
||||
|
@ -116,7 +113,8 @@ class EcovacsController:
|
|||
await device.teardown()
|
||||
for legacy_device in self._legacy_devices:
|
||||
await self._hass.async_add_executor_job(legacy_device.disconnect)
|
||||
await self._mqtt.disconnect()
|
||||
if self._mqtt_client is not None:
|
||||
await self._mqtt_client.disconnect()
|
||||
await self._authenticator.teardown()
|
||||
|
||||
def add_legacy_entity(self, device: VacBot, component: str) -> None:
|
||||
|
@ -127,6 +125,16 @@ class EcovacsController:
|
|||
"""Check if legacy entity is added."""
|
||||
return f"{device.vacuum['did']}_{component}" in self._added_legacy_entities
|
||||
|
||||
async def _get_mqtt_client(self) -> MqttClient:
|
||||
"""Return validated MQTT client."""
|
||||
if self._mqtt_client is None:
|
||||
config = await self._hass.async_add_executor_job(self._mqtt_config_fn)
|
||||
mqtt = MqttClient(config, self._authenticator)
|
||||
await mqtt.verify_config()
|
||||
self._mqtt_client = mqtt
|
||||
|
||||
return self._mqtt_client
|
||||
|
||||
@property
|
||||
def devices(self) -> list[Device]:
|
||||
"""Return devices."""
|
||||
|
|
Loading…
Add table
Reference in a new issue