1 """Coordinator for monitoring the size of a file."""
3 from __future__
import annotations
5 from datetime
import datetime, timedelta
14 from .const
import DOMAIN
16 _LOGGER = logging.getLogger(__name__)
20 """Filesize coordinator."""
24 def __init__(self, hass: HomeAssistant, unresolved_path: str) ->
None:
25 """Initialize filesize coordinator."""
36 """Check if path is valid, allowed and return full path."""
38 get_path = pathlib.Path(path)
39 if not self.
hasshass.config.is_allowed_path(path):
40 raise UpdateFailed(f
"Filepath {path} is not valid or allowed")
42 if not get_path.exists()
or not get_path.is_file():
45 return get_path.absolute()
48 """Fetch file information."""
50 return self.
pathpath.stat()
51 except OSError
as error:
52 raise UpdateFailed(f
"Can not retrieve file statistics {error}")
from error
59 """Fetch file information."""
60 statinfo = await self.
hasshass.async_add_executor_job(self.
_update_update)
61 size = statinfo.st_size
62 last_updated = dt_util.utc_from_timestamp(statinfo.st_mtime)
63 created = dt_util.utc_from_timestamp(statinfo.st_ctime)
65 _LOGGER.debug(
"size %s, last updated %s", size, last_updated)
66 data: dict[str, int | float | datetime] = {
67 "file": round(size / 1e6, 2),
69 "last_updated": last_updated,
pathlib.Path _get_full_path(self)
dict[str, float|int|datetime] _async_update_data(self)
None __init__(self, HomeAssistant hass, str unresolved_path)
os.stat_result _update(self)