core/homeassistant/components/nextcloud/sensor.py

296 lines
12 KiB
Python
Raw Normal View History

Add Nextcloud Integration (#30871) * some sensors working in homeassistant * bring up to date * add codeowner * update requirements * overhaul data imports from api & sensor discovery * remove print statement * delete requirements_test_all * add requrements_test_all.txt * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * describe recursive function * clarify that dict is returned * remove requirements from requirements_test_all * improve and simplify sensor naming * add basic tests * restore pre-commit config * update requirements_test_all * remove codespell requirement * update pre-commit-config * add-back codespell * rename class variables as suggested by @springstan * add dev branch to no-commit-to-branch git hook Because my fork had the same 'dev' branch i wasn't able to push. Going forward I should probably name my branches differently. * move config logic to __init__.py * restore .pre-commit-config.yaml * remove tests * remove nextcloud test requirement * remove debugging code * implement binary sensors * restore .pre-commit-config.yaml * bump dependency version * bump requirements files * bump nextcloud reqirement to latest * update possible exceptions, use fstrings * add list of sensors & fix inconsistency in get_data_points * use domain for config * fix guard clause * repair pre-commit-config * Remove period from logging * include url in unique_id * update requirements_all.txt Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-03-24 10:11:35 +00:00
"""Summary data from Nextcoud."""
from __future__ import annotations
2023-08-27 16:51:31 +00:00
from datetime import UTC, datetime
from typing import Final, cast
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
2023-08-27 16:51:31 +00:00
from homeassistant.const import EntityCategory, UnitOfInformation, UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
Add Nextcloud Integration (#30871) * some sensors working in homeassistant * bring up to date * add codeowner * update requirements * overhaul data imports from api & sensor discovery * remove print statement * delete requirements_test_all * add requrements_test_all.txt * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * describe recursive function * clarify that dict is returned * remove requirements from requirements_test_all * improve and simplify sensor naming * add basic tests * restore pre-commit config * update requirements_test_all * remove codespell requirement * update pre-commit-config * add-back codespell * rename class variables as suggested by @springstan * add dev branch to no-commit-to-branch git hook Because my fork had the same 'dev' branch i wasn't able to push. Going forward I should probably name my branches differently. * move config logic to __init__.py * restore .pre-commit-config.yaml * remove tests * remove nextcloud test requirement * remove debugging code * implement binary sensors * restore .pre-commit-config.yaml * bump dependency version * bump requirements files * bump nextcloud reqirement to latest * update possible exceptions, use fstrings * add list of sensors & fix inconsistency in get_data_points * use domain for config * fix guard clause * repair pre-commit-config * Remove period from logging * include url in unique_id * update requirements_all.txt Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-03-24 10:11:35 +00:00
from .const import DOMAIN
from .coordinator import NextcloudDataUpdateCoordinator
from .entity import NextcloudEntity
2023-03-14 08:51:03 +00:00
2023-08-27 16:51:31 +00:00
UNIT_OF_LOAD: Final[str] = "load"
SENSORS: Final[dict[str, SensorEntityDescription]] = {
"activeUsers_last1hour": SensorEntityDescription(
key="activeUsers_last1hour",
translation_key="nextcloud_activeusers_last1hour",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:account-multiple",
),
"activeUsers_last24hours": SensorEntityDescription(
key="activeUsers_last24hours",
translation_key="nextcloud_activeusers_last24hours",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:account-multiple",
),
"activeUsers_last5minutes": SensorEntityDescription(
key="activeUsers_last5minutes",
translation_key="nextcloud_activeusers_last5minutes",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:account-multiple",
),
"database_type": SensorEntityDescription(
key="database_type",
translation_key="nextcloud_database_type",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:database",
),
"database_version": SensorEntityDescription(
key="database_version",
translation_key="nextcloud_database_version",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:database",
),
"server_php_max_execution_time": SensorEntityDescription(
key="server_php_max_execution_time",
translation_key="nextcloud_server_php_max_execution_time",
device_class=SensorDeviceClass.DURATION,
entity_category=EntityCategory.CONFIG,
icon="mdi:language-php",
native_unit_of_measurement=UnitOfTime.SECONDS,
),
"server_php_memory_limit": SensorEntityDescription(
key="server_php_memory_limit",
translation_key="nextcloud_server_php_memory_limit",
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.CONFIG,
icon="mdi:language-php",
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_display_precision=1,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
),
"server_php_upload_max_filesize": SensorEntityDescription(
key="server_php_upload_max_filesize",
translation_key="nextcloud_server_php_upload_max_filesize",
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.CONFIG,
icon="mdi:language-php",
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_display_precision=1,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
),
"server_php_version": SensorEntityDescription(
key="server_php_version",
translation_key="nextcloud_server_php_version",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:language-php",
),
"server_webserver": SensorEntityDescription(
key="server_webserver",
translation_key="nextcloud_server_webserver",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_fed_shares_sent": SensorEntityDescription(
key="shares_num_fed_shares_sent",
translation_key="nextcloud_shares_num_fed_shares_sent",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_fed_shares_received": SensorEntityDescription(
key="shares_num_fed_shares_received",
translation_key="nextcloud_shares_num_fed_shares_received",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_shares": SensorEntityDescription(
key="shares_num_shares",
translation_key="nextcloud_shares_num_shares",
),
"shares_num_shares_groups": SensorEntityDescription(
key="shares_num_shares_groups",
translation_key="nextcloud_shares_num_shares_groups",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_shares_link": SensorEntityDescription(
key="shares_num_shares_link",
translation_key="nextcloud_shares_num_shares_link",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_shares_link_no_password": SensorEntityDescription(
key="shares_num_shares_link_no_password",
translation_key="nextcloud_shares_num_shares_link_no_password",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_shares_mail": SensorEntityDescription(
key="shares_num_shares_mail",
translation_key="nextcloud_shares_num_shares_mail",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_shares_room": SensorEntityDescription(
key="shares_num_shares_room",
translation_key="nextcloud_shares_num_shares_room",
entity_category=EntityCategory.DIAGNOSTIC,
),
"shares_num_shares_user": SensorEntityDescription(
key="server_num_shares_user",
translation_key="nextcloud_shares_num_shares_user",
entity_category=EntityCategory.DIAGNOSTIC,
),
"storage_num_files": SensorEntityDescription(
key="storage_num_files",
translation_key="nextcloud_storage_num_files",
),
"storage_num_storages": SensorEntityDescription(
key="storage_num_storages",
translation_key="nextcloud_storage_num_storages",
),
"storage_num_storages_home": SensorEntityDescription(
key="storage_num_storages_home",
translation_key="nextcloud_storage_num_storages_home",
entity_category=EntityCategory.DIAGNOSTIC,
),
"storage_num_storages_local": SensorEntityDescription(
key="storage_num_storages_local",
translation_key="nextcloud_storage_num_storages_local",
entity_category=EntityCategory.DIAGNOSTIC,
),
"storage_num_storages_other": SensorEntityDescription(
key="storage_num_storages_other",
translation_key="nextcloud_storage_num_storages_other",
entity_category=EntityCategory.DIAGNOSTIC,
),
"storage_num_users": SensorEntityDescription(
key="storage_num_users",
translation_key="nextcloud_storage_num_users",
),
"system_apps_num_installed": SensorEntityDescription(
key="system_apps_num_installed",
translation_key="nextcloud_system_apps_num_installed",
),
"system_apps_num_updates_available": SensorEntityDescription(
key="system_apps_num_updates_available",
translation_key="nextcloud_system_apps_num_updates_available",
icon="mdi:update",
),
"system_cpuload_1": SensorEntityDescription(
key="system_cpuload_1",
translation_key="nextcloud_system_cpuload_1",
native_unit_of_measurement=UNIT_OF_LOAD,
2023-08-27 16:51:31 +00:00
icon="mdi:chip",
suggested_display_precision=2,
),
"system_cpuload_5": SensorEntityDescription(
key="system_cpuload_5",
translation_key="nextcloud_system_cpuload_5",
native_unit_of_measurement=UNIT_OF_LOAD,
icon="mdi:chip",
suggested_display_precision=2,
),
"system_cpuload_15": SensorEntityDescription(
key="system_cpuload_15",
translation_key="nextcloud_system_cpuload_15",
native_unit_of_measurement=UNIT_OF_LOAD,
icon="mdi:chip",
suggested_display_precision=2,
2023-08-27 16:51:31 +00:00
),
"system_freespace": SensorEntityDescription(
key="system_freespace",
translation_key="nextcloud_system_freespace",
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk",
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
),
"system_mem_free": SensorEntityDescription(
key="system_mem_free",
translation_key="nextcloud_system_mem_free",
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.KILOBYTES,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
),
"system_mem_total": SensorEntityDescription(
key="system_mem_total",
translation_key="nextcloud_system_mem_total",
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.KILOBYTES,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
),
"system_memcache.distributed": SensorEntityDescription(
key="system_memcache.distributed",
translation_key="nextcloud_system_memcache_distributed",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"system_memcache.local": SensorEntityDescription(
key="system_memcache.local",
translation_key="nextcloud_system_memcache_local",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"system_memcache.locking": SensorEntityDescription(
key="system_memcache.locking",
translation_key="nextcloud_system_memcache_locking",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"system_swap_total": SensorEntityDescription(
key="system_swap_total",
translation_key="nextcloud_system_swap_total",
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.KILOBYTES,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
),
"system_swap_free": SensorEntityDescription(
key="system_swap_free",
translation_key="nextcloud_system_swap_free",
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory",
native_unit_of_measurement=UnitOfInformation.KILOBYTES,
suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
),
"system_theme": SensorEntityDescription(
key="system_theme",
translation_key="nextcloud_system_theme",
),
"system_version": SensorEntityDescription(
key="system_version",
translation_key="nextcloud_system_version",
),
}
Add Nextcloud Integration (#30871) * some sensors working in homeassistant * bring up to date * add codeowner * update requirements * overhaul data imports from api & sensor discovery * remove print statement * delete requirements_test_all * add requrements_test_all.txt * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * describe recursive function * clarify that dict is returned * remove requirements from requirements_test_all * improve and simplify sensor naming * add basic tests * restore pre-commit config * update requirements_test_all * remove codespell requirement * update pre-commit-config * add-back codespell * rename class variables as suggested by @springstan * add dev branch to no-commit-to-branch git hook Because my fork had the same 'dev' branch i wasn't able to push. Going forward I should probably name my branches differently. * move config logic to __init__.py * restore .pre-commit-config.yaml * remove tests * remove nextcloud test requirement * remove debugging code * implement binary sensors * restore .pre-commit-config.yaml * bump dependency version * bump requirements files * bump nextcloud reqirement to latest * update possible exceptions, use fstrings * add list of sensors & fix inconsistency in get_data_points * use domain for config * fix guard clause * repair pre-commit-config * Remove period from logging * include url in unique_id * update requirements_all.txt Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-03-24 10:11:35 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
Add Nextcloud Integration (#30871) * some sensors working in homeassistant * bring up to date * add codeowner * update requirements * overhaul data imports from api & sensor discovery * remove print statement * delete requirements_test_all * add requrements_test_all.txt * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * describe recursive function * clarify that dict is returned * remove requirements from requirements_test_all * improve and simplify sensor naming * add basic tests * restore pre-commit config * update requirements_test_all * remove codespell requirement * update pre-commit-config * add-back codespell * rename class variables as suggested by @springstan * add dev branch to no-commit-to-branch git hook Because my fork had the same 'dev' branch i wasn't able to push. Going forward I should probably name my branches differently. * move config logic to __init__.py * restore .pre-commit-config.yaml * remove tests * remove nextcloud test requirement * remove debugging code * implement binary sensors * restore .pre-commit-config.yaml * bump dependency version * bump requirements files * bump nextcloud reqirement to latest * update possible exceptions, use fstrings * add list of sensors & fix inconsistency in get_data_points * use domain for config * fix guard clause * repair pre-commit-config * Remove period from logging * include url in unique_id * update requirements_all.txt Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-03-24 10:11:35 +00:00
"""Set up the Nextcloud sensors."""
coordinator: NextcloudDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
[
2023-08-27 16:51:31 +00:00
NextcloudSensor(coordinator, name, entry, SENSORS[name])
for name in coordinator.data
if name in SENSORS
]
)
Add Nextcloud Integration (#30871) * some sensors working in homeassistant * bring up to date * add codeowner * update requirements * overhaul data imports from api & sensor discovery * remove print statement * delete requirements_test_all * add requrements_test_all.txt * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * describe recursive function * clarify that dict is returned * remove requirements from requirements_test_all * improve and simplify sensor naming * add basic tests * restore pre-commit config * update requirements_test_all * remove codespell requirement * update pre-commit-config * add-back codespell * rename class variables as suggested by @springstan * add dev branch to no-commit-to-branch git hook Because my fork had the same 'dev' branch i wasn't able to push. Going forward I should probably name my branches differently. * move config logic to __init__.py * restore .pre-commit-config.yaml * remove tests * remove nextcloud test requirement * remove debugging code * implement binary sensors * restore .pre-commit-config.yaml * bump dependency version * bump requirements files * bump nextcloud reqirement to latest * update possible exceptions, use fstrings * add list of sensors & fix inconsistency in get_data_points * use domain for config * fix guard clause * repair pre-commit-config * Remove period from logging * include url in unique_id * update requirements_all.txt Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-03-24 10:11:35 +00:00
class NextcloudSensor(NextcloudEntity, SensorEntity):
Add Nextcloud Integration (#30871) * some sensors working in homeassistant * bring up to date * add codeowner * update requirements * overhaul data imports from api & sensor discovery * remove print statement * delete requirements_test_all * add requrements_test_all.txt * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * describe recursive function * clarify that dict is returned * remove requirements from requirements_test_all * improve and simplify sensor naming * add basic tests * restore pre-commit config * update requirements_test_all * remove codespell requirement * update pre-commit-config * add-back codespell * rename class variables as suggested by @springstan * add dev branch to no-commit-to-branch git hook Because my fork had the same 'dev' branch i wasn't able to push. Going forward I should probably name my branches differently. * move config logic to __init__.py * restore .pre-commit-config.yaml * remove tests * remove nextcloud test requirement * remove debugging code * implement binary sensors * restore .pre-commit-config.yaml * bump dependency version * bump requirements files * bump nextcloud reqirement to latest * update possible exceptions, use fstrings * add list of sensors & fix inconsistency in get_data_points * use domain for config * fix guard clause * repair pre-commit-config * Remove period from logging * include url in unique_id * update requirements_all.txt Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-03-24 10:11:35 +00:00
"""Represents a Nextcloud sensor."""
@property
2023-08-27 16:51:31 +00:00
def native_value(self) -> StateType | datetime:
Add Nextcloud Integration (#30871) * some sensors working in homeassistant * bring up to date * add codeowner * update requirements * overhaul data imports from api & sensor discovery * remove print statement * delete requirements_test_all * add requrements_test_all.txt * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Update homeassistant/components/nextcloud/sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * describe recursive function * clarify that dict is returned * remove requirements from requirements_test_all * improve and simplify sensor naming * add basic tests * restore pre-commit config * update requirements_test_all * remove codespell requirement * update pre-commit-config * add-back codespell * rename class variables as suggested by @springstan * add dev branch to no-commit-to-branch git hook Because my fork had the same 'dev' branch i wasn't able to push. Going forward I should probably name my branches differently. * move config logic to __init__.py * restore .pre-commit-config.yaml * remove tests * remove nextcloud test requirement * remove debugging code * implement binary sensors * restore .pre-commit-config.yaml * bump dependency version * bump requirements files * bump nextcloud reqirement to latest * update possible exceptions, use fstrings * add list of sensors & fix inconsistency in get_data_points * use domain for config * fix guard clause * repair pre-commit-config * Remove period from logging * include url in unique_id * update requirements_all.txt Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-03-24 10:11:35 +00:00
"""Return the state for this sensor."""
2023-08-27 16:51:31 +00:00
val = self.coordinator.data.get(self.item)
if (
getattr(self.entity_description, "device_class", None)
== SensorDeviceClass.TIMESTAMP
):
return datetime.fromtimestamp(cast(int, val), tz=UTC)
return val