Skip to content

Commit

Permalink
Fix file uploads in MQTT config flow not processed in executor (home-…
Browse files Browse the repository at this point in the history
…assistant#130746)

Process file uploads in MQTT config flow in executor
  • Loading branch information
jbouwh authored and dotvav committed Nov 17, 2024
1 parent f8497ed commit b53f279
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions homeassistant/components/mqtt/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
CONF_PROTOCOL,
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.hassio import is_hassio
Expand Down Expand Up @@ -735,6 +735,16 @@ def _validate(
)


async def _get_uploaded_file(hass: HomeAssistant, id: str) -> str:
"""Get file content from uploaded file."""

def _proces_uploaded_file() -> str:
with process_uploaded_file(hass, id) as file_path:
return file_path.read_text(encoding=DEFAULT_ENCODING)

return await hass.async_add_executor_job(_proces_uploaded_file)


async def async_get_broker_settings(
flow: ConfigFlow | OptionsFlow,
fields: OrderedDict[Any, Any],
Expand Down Expand Up @@ -793,8 +803,7 @@ async def _async_validate_broker_settings(
return False
certificate_id: str | None = user_input.get(CONF_CERTIFICATE)
if certificate_id:
with process_uploaded_file(hass, certificate_id) as certificate_file:
certificate = certificate_file.read_text(encoding=DEFAULT_ENCODING)
certificate = await _get_uploaded_file(hass, certificate_id)

# Return to form for file upload CA cert or client cert and key
if (
Expand All @@ -810,15 +819,9 @@ async def _async_validate_broker_settings(
return False

if client_certificate_id:
with process_uploaded_file(
hass, client_certificate_id
) as client_certificate_file:
client_certificate = client_certificate_file.read_text(
encoding=DEFAULT_ENCODING
)
client_certificate = await _get_uploaded_file(hass, client_certificate_id)
if client_key_id:
with process_uploaded_file(hass, client_key_id) as key_file:
client_key = key_file.read_text(encoding=DEFAULT_ENCODING)
client_key = await _get_uploaded_file(hass, client_key_id)

certificate_data: dict[str, Any] = {}
if certificate:
Expand Down

0 comments on commit b53f279

Please sign in to comment.