Fix filesize doing blocking I/O in the event loop (#90479)

Fix filesize doing I/O in the event loop
pull/90469/head^2
J. Nick Koston 2023-03-29 11:26:28 -10:00 committed by GitHub
parent 1023628821
commit 4c21caa917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 14 deletions

View File

@ -11,24 +11,19 @@ from homeassistant.exceptions import ConfigEntryNotReady
from .const import PLATFORMS
def check_path(path: pathlib.Path) -> bool:
"""Check path."""
return path.exists() and path.is_file()
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from a config entry."""
path = entry.data[CONF_FILE_PATH]
def _check_path(hass: HomeAssistant, path: str) -> None:
"""Check if path is valid and allowed."""
get_path = pathlib.Path(path)
check_file = await hass.async_add_executor_job(check_path, get_path)
if not check_file:
if not get_path.exists() or not get_path.is_file():
raise ConfigEntryNotReady(f"Can not access file {path}")
if not hass.config.is_allowed_path(path):
raise ConfigEntryNotReady(f"Filepath {path} is not valid or allowed")
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from a config entry."""
await hass.async_add_executor_job(_check_path, hass, entry.data[CONF_FILE_PATH])
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True

View File

@ -49,7 +49,9 @@ class FilesizeConfigFlow(ConfigFlow, domain=DOMAIN):
if user_input is not None:
try:
full_path = validate_path(self.hass, user_input[CONF_FILE_PATH])
full_path = await self.hass.async_add_executor_job(
validate_path, self.hass, user_input[CONF_FILE_PATH]
)
except NotValidError:
errors["base"] = "not_valid"
except NotAllowedError:

View File

@ -1950,7 +1950,11 @@ class Config:
)
def is_allowed_path(self, path: str) -> bool:
"""Check if the path is valid for access from outside."""
"""Check if the path is valid for access from outside.
This function does blocking I/O and should not be called from the event loop.
Use hass.async_add_executor_job to schedule it on the executor.
"""
assert path is not None
thepath = pathlib.Path(path)