Use DATA_SIZE device class in folder integration (#83897)
parent
5e5e89ea18
commit
14a47f5dff
|
@ -8,8 +8,12 @@ import os
|
|||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
from homeassistant.const import DATA_MEGABYTES
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
)
|
||||
from homeassistant.const import UnitOfInformation
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -31,14 +35,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def get_files_list(folder_path, filter_term):
|
||||
def get_files_list(folder_path: str, filter_term: str) -> list[str]:
|
||||
"""Return the list of files, applying filter."""
|
||||
query = folder_path + filter_term
|
||||
files_list = glob.glob(query)
|
||||
return files_list
|
||||
|
||||
|
||||
def get_size(files_list):
|
||||
def get_size(files_list: list[str]) -> int:
|
||||
"""Return the sum of the size in bytes of files in the list."""
|
||||
size_list = [os.stat(f).st_size for f in files_list if os.path.isfile(f)]
|
||||
return sum(size_list)
|
||||
|
@ -51,7 +55,7 @@ def setup_platform(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the folder sensor."""
|
||||
path = config[CONF_FOLDER_PATHS]
|
||||
path: str = config[CONF_FOLDER_PATHS]
|
||||
|
||||
if not hass.config.is_allowed_path(path):
|
||||
_LOGGER.error("Folder %s is not valid or allowed", path)
|
||||
|
@ -63,55 +67,28 @@ def setup_platform(
|
|||
class Folder(SensorEntity):
|
||||
"""Representation of a folder."""
|
||||
|
||||
ICON = "mdi:folder"
|
||||
_attr_device_class = SensorDeviceClass.DATA_SIZE
|
||||
_attr_icon = "mdi:folder"
|
||||
_attr_native_unit_of_measurement = UnitOfInformation.MEGABYTES
|
||||
|
||||
def __init__(self, folder_path, filter_term):
|
||||
def __init__(self, folder_path: str, filter_term: str) -> None:
|
||||
"""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._number_of_files = None
|
||||
self._size = None
|
||||
self._name = os.path.split(os.path.split(folder_path)[0])[1]
|
||||
self._unit_of_measurement = DATA_MEGABYTES
|
||||
self._file_list = None
|
||||
self._attr_name = os.path.split(os.path.split(folder_path)[0])[1]
|
||||
|
||||
def update(self) -> None:
|
||||
"""Update the sensor."""
|
||||
files_list = get_files_list(self._folder_path, self._filter_term)
|
||||
self._file_list = files_list
|
||||
self._number_of_files = len(files_list)
|
||||
self._size = get_size(files_list)
|
||||
number_of_files = len(files_list)
|
||||
size = get_size(files_list)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the state of the sensor."""
|
||||
decimals = 2
|
||||
size_mb = round(self._size / 1e6, decimals)
|
||||
return size_mb
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return self.ICON
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return other details about the sensor state."""
|
||||
return {
|
||||
self._attr_native_value = round(size / 1e6, 2)
|
||||
self._attr_extra_state_attributes = {
|
||||
"path": self._folder_path,
|
||||
"filter": self._filter_term,
|
||||
"number_of_files": self._number_of_files,
|
||||
"bytes": self._size,
|
||||
"file_list": self._file_list,
|
||||
"number_of_files": number_of_files,
|
||||
"bytes": size,
|
||||
"file_list": files_list,
|
||||
}
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return self._unit_of_measurement
|
||||
|
|
Loading…
Reference in New Issue