Fix telegram doing blocking I/O in the event loop (#118531)

pull/118557/head
Luca Angemi 2024-05-31 14:45:52 +02:00 committed by GitHub
parent 76391d71d6
commit 5ed9d58a7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 1 deletions

View File

@ -284,6 +284,12 @@ SERVICE_MAP = {
}
def _read_file_as_bytesio(file_path: str) -> io.BytesIO:
"""Read a file and return it as a BytesIO object."""
with open(file_path, "rb") as file:
return io.BytesIO(file.read())
async def load_data(
hass,
url=None,
@ -342,7 +348,9 @@ async def load_data(
)
elif filepath is not None:
if hass.config.is_allowed_path(filepath):
return open(filepath, "rb")
return await hass.async_add_executor_job(
_read_file_as_bytesio, filepath
)
_LOGGER.warning("'%s' are not secure to load data from!", filepath)
else: