mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Fix filesize doing blocking I/O in the event loop (#90479)
Fix filesize doing I/O in the event loop
This commit is contained in:
parent
30af4c769e
commit
2a627e63f1
@ -11,24 +11,19 @@ from homeassistant.exceptions import ConfigEntryNotReady
|
|||||||
from .const import PLATFORMS
|
from .const import PLATFORMS
|
||||||
|
|
||||||
|
|
||||||
def check_path(path: pathlib.Path) -> bool:
|
def _check_path(hass: HomeAssistant, path: str) -> None:
|
||||||
"""Check path."""
|
"""Check if path is valid and allowed."""
|
||||||
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]
|
|
||||||
get_path = pathlib.Path(path)
|
get_path = pathlib.Path(path)
|
||||||
|
if not get_path.exists() or not get_path.is_file():
|
||||||
check_file = await hass.async_add_executor_job(check_path, get_path)
|
|
||||||
if not check_file:
|
|
||||||
raise ConfigEntryNotReady(f"Can not access file {path}")
|
raise ConfigEntryNotReady(f"Can not access file {path}")
|
||||||
|
|
||||||
if not hass.config.is_allowed_path(path):
|
if not hass.config.is_allowed_path(path):
|
||||||
raise ConfigEntryNotReady(f"Filepath {path} is not valid or allowed")
|
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)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -49,7 +49,9 @@ class FilesizeConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
try:
|
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:
|
except NotValidError:
|
||||||
errors["base"] = "not_valid"
|
errors["base"] = "not_valid"
|
||||||
except NotAllowedError:
|
except NotAllowedError:
|
||||||
|
@ -1950,7 +1950,11 @@ class Config:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def is_allowed_path(self, path: str) -> bool:
|
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
|
assert path is not None
|
||||||
|
|
||||||
thepath = pathlib.Path(path)
|
thepath = pathlib.Path(path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user