From 5ed9d58a7bcd37b530bbaec5e27b7ec32c5a8a40 Mon Sep 17 00:00:00 2001 From: Luca Angemi Date: Fri, 31 May 2024 14:45:52 +0200 Subject: [PATCH] Fix telegram doing blocking I/O in the event loop (#118531) --- homeassistant/components/telegram_bot/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index 7a056665ed4..df5bebb47d4 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -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: