Fix file uploads in MQTT config flow not processed in executor (#130746)

Process file uploads in MQTT config flow in executor
This commit is contained in:
Jan Bouwhuis 2024-11-16 17:40:01 +01:00 committed by GitHub
parent 0ada59a4fe
commit c219b512eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -33,7 +33,7 @@ from homeassistant.const import (
CONF_PROTOCOL, CONF_PROTOCOL,
CONF_USERNAME, CONF_USERNAME,
) )
from homeassistant.core import callback from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import AbortFlow from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.hassio import is_hassio from homeassistant.helpers.hassio import is_hassio
@ -735,6 +735,16 @@ class MQTTOptionsFlowHandler(OptionsFlow):
) )
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( async def async_get_broker_settings(
flow: ConfigFlow | OptionsFlow, flow: ConfigFlow | OptionsFlow,
fields: OrderedDict[Any, Any], fields: OrderedDict[Any, Any],
@ -793,8 +803,7 @@ async def async_get_broker_settings(
return False return False
certificate_id: str | None = user_input.get(CONF_CERTIFICATE) certificate_id: str | None = user_input.get(CONF_CERTIFICATE)
if certificate_id: if certificate_id:
with process_uploaded_file(hass, certificate_id) as certificate_file: certificate = await _get_uploaded_file(hass, certificate_id)
certificate = certificate_file.read_text(encoding=DEFAULT_ENCODING)
# Return to form for file upload CA cert or client cert and key # Return to form for file upload CA cert or client cert and key
if ( if (
@ -810,15 +819,9 @@ async def async_get_broker_settings(
return False return False
if client_certificate_id: if client_certificate_id:
with process_uploaded_file( client_certificate = await _get_uploaded_file(hass, client_certificate_id)
hass, client_certificate_id
) as client_certificate_file:
client_certificate = client_certificate_file.read_text(
encoding=DEFAULT_ENCODING
)
if client_key_id: if client_key_id:
with process_uploaded_file(hass, client_key_id) as key_file: client_key = await _get_uploaded_file(hass, client_key_id)
client_key = key_file.read_text(encoding=DEFAULT_ENCODING)
certificate_data: dict[str, Any] = {} certificate_data: dict[str, Any] = {}
if certificate: if certificate: