mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Fix double executor in Filesize (#117029)
This commit is contained in:
parent
3774d8ed54
commit
38a3c3a823
@ -2,12 +2,9 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pathlib
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_FILE_PATH
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import PLATFORMS
|
||||
from .coordinator import FileSizeCoordinator
|
||||
@ -15,24 +12,9 @@ from .coordinator import FileSizeCoordinator
|
||||
FileSizeConfigEntry = ConfigEntry[FileSizeCoordinator]
|
||||
|
||||
|
||||
def _get_full_path(hass: HomeAssistant, path: str) -> pathlib.Path:
|
||||
"""Check if path is valid, allowed and return full path."""
|
||||
get_path = pathlib.Path(path)
|
||||
if not hass.config.is_allowed_path(path):
|
||||
raise ConfigEntryNotReady(f"Filepath {path} is not valid or allowed")
|
||||
|
||||
if not get_path.exists() or not get_path.is_file():
|
||||
raise ConfigEntryNotReady(f"Can not access file {path}")
|
||||
|
||||
return get_path.absolute()
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: FileSizeConfigEntry) -> bool:
|
||||
"""Set up from a config entry."""
|
||||
path = await hass.async_add_executor_job(
|
||||
_get_full_path, hass, entry.data[CONF_FILE_PATH]
|
||||
)
|
||||
coordinator = FileSizeCoordinator(hass, path)
|
||||
coordinator = FileSizeCoordinator(hass, entry.data[CONF_FILE_PATH])
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
|
@ -19,7 +19,9 @@ _LOGGER = logging.getLogger(__name__)
|
||||
class FileSizeCoordinator(DataUpdateCoordinator[dict[str, int | float | datetime]]):
|
||||
"""Filesize coordinator."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, path: pathlib.Path) -> None:
|
||||
path: pathlib.Path
|
||||
|
||||
def __init__(self, hass: HomeAssistant, unresolved_path: str) -> None:
|
||||
"""Initialize filesize coordinator."""
|
||||
super().__init__(
|
||||
hass,
|
||||
@ -28,10 +30,25 @@ class FileSizeCoordinator(DataUpdateCoordinator[dict[str, int | float | datetime
|
||||
update_interval=timedelta(seconds=60),
|
||||
always_update=False,
|
||||
)
|
||||
self.path: pathlib.Path = path
|
||||
self._unresolved_path = unresolved_path
|
||||
|
||||
def _get_full_path(self) -> pathlib.Path:
|
||||
"""Check if path is valid, allowed and return full path."""
|
||||
path = self._unresolved_path
|
||||
get_path = pathlib.Path(path)
|
||||
if not self.hass.config.is_allowed_path(path):
|
||||
raise UpdateFailed(f"Filepath {path} is not valid or allowed")
|
||||
|
||||
if not get_path.exists() or not get_path.is_file():
|
||||
raise UpdateFailed(f"Can not access file {path}")
|
||||
|
||||
return get_path.absolute()
|
||||
|
||||
def _update(self) -> os.stat_result:
|
||||
"""Fetch file information."""
|
||||
if not hasattr(self, "path"):
|
||||
self.path = self._get_full_path()
|
||||
|
||||
try:
|
||||
return self.path.stat()
|
||||
except OSError as error:
|
||||
|
Loading…
x
Reference in New Issue
Block a user