Files
homeassistant-core/homeassistant/components/folder/sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.8 KiB
Python
Raw Normal View History

2019-04-03 17:40:03 +02:00
"""Sensor for monitoring the contents of a folder."""
2022-01-05 13:34:15 +01:00
from __future__ import annotations
2018-02-22 07:21:07 +00:00
from datetime import timedelta
import glob
import logging
import os
import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
)
from homeassistant.const import UnitOfInformation
2022-01-05 13:34:15 +01:00
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
2022-01-05 13:34:15 +01:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2018-02-22 07:21:07 +00:00
_LOGGER = logging.getLogger(__name__)
CONF_FOLDER_PATHS = "folder"
CONF_FILTER = "filter"
DEFAULT_FILTER = "*"
2018-03-03 14:03:06 -08:00
SCAN_INTERVAL = timedelta(minutes=1)
2018-02-22 07:21:07 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_FOLDER_PATHS): cv.isdir,
vol.Optional(CONF_FILTER, default=DEFAULT_FILTER): cv.string,
}
)
def get_files_list(folder_path: str, filter_term: str) -> list[str]:
2018-02-22 07:21:07 +00:00
"""Return the list of files, applying filter."""
query = folder_path + filter_term
2024-04-06 11:07:37 +02:00
return glob.glob(query)
2018-02-22 07:21:07 +00:00
def get_size(files_list: list[str]) -> int:
2018-02-22 07:21:07 +00:00
"""Return the sum of the size in bytes of files in the list."""
2018-04-08 10:32:49 +01:00
size_list = [os.stat(f).st_size for f in files_list if os.path.isfile(f)]
2018-02-22 07:21:07 +00:00
return sum(size_list)
2022-01-05 13:34:15 +01:00
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2018-02-22 07:21:07 +00:00
"""Set up the folder sensor."""
path: str = config[CONF_FOLDER_PATHS]
2018-02-22 07:21:07 +00:00
if not hass.config.is_allowed_path(path):
_LOGGER.error("Folder %s is not valid or allowed", path)
2018-02-22 07:21:07 +00:00
else:
2022-01-05 13:34:15 +01:00
folder = Folder(path, config[CONF_FILTER])
2018-08-24 16:37:30 +02:00
add_entities([folder], True)
2018-02-22 07:21:07 +00:00
class Folder(SensorEntity):
2018-02-22 07:21:07 +00:00
"""Representation of a folder."""
_attr_device_class = SensorDeviceClass.DATA_SIZE
_attr_icon = "mdi:folder"
_attr_native_unit_of_measurement = UnitOfInformation.MEGABYTES
2018-02-22 07:21:07 +00:00
def __init__(self, folder_path: str, filter_term: str) -> None:
2018-02-22 07:21:07 +00:00
"""Initialize the data object."""
folder_path = os.path.join(folder_path, "") # If no trailing / add it
self._folder_path = folder_path # Need to check its a valid path
self._filter_term = filter_term
self._attr_name = os.path.split(os.path.split(folder_path)[0])[1]
2018-02-22 07:21:07 +00:00
2022-08-22 13:36:33 +02:00
def update(self) -> None:
2018-02-22 07:21:07 +00:00
"""Update the sensor."""
files_list = get_files_list(self._folder_path, self._filter_term)
number_of_files = len(files_list)
size = get_size(files_list)
2018-02-22 07:21:07 +00:00
self._attr_native_value = round(size / 1e6, 2)
self._attr_extra_state_attributes = {
2018-02-22 07:21:07 +00:00
"path": self._folder_path,
"filter": self._filter_term,
"number_of_files": number_of_files,
"bytes": size,
"file_list": files_list,
2018-02-22 07:21:07 +00:00
}