Rework to use list of entity descriptions in Nextcloud integration (#99150)

This commit is contained in:
Michael 2023-08-27 20:18:55 +02:00 committed by GitHub
parent c88672c352
commit 0ce9d21bea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 61 deletions

View File

@ -16,28 +16,28 @@ from .const import DOMAIN
from .coordinator import NextcloudDataUpdateCoordinator from .coordinator import NextcloudDataUpdateCoordinator
from .entity import NextcloudEntity from .entity import NextcloudEntity
BINARY_SENSORS: Final[dict[str, BinarySensorEntityDescription]] = { BINARY_SENSORS: Final[list[BinarySensorEntityDescription]] = [
"system_debug": BinarySensorEntityDescription( BinarySensorEntityDescription(
key="system_debug", key="system_debug",
translation_key="nextcloud_system_debug", translation_key="nextcloud_system_debug",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"system_enable_avatars": BinarySensorEntityDescription( BinarySensorEntityDescription(
key="system_enable_avatars", key="system_enable_avatars",
translation_key="nextcloud_system_enable_avatars", translation_key="nextcloud_system_enable_avatars",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"system_enable_previews": BinarySensorEntityDescription( BinarySensorEntityDescription(
key="system_enable_previews", key="system_enable_previews",
translation_key="nextcloud_system_enable_previews", translation_key="nextcloud_system_enable_previews",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"system_filelocking.enabled": BinarySensorEntityDescription( BinarySensorEntityDescription(
key="system_filelocking.enabled", key="system_filelocking.enabled",
translation_key="nextcloud_system_filelocking_enabled", translation_key="nextcloud_system_filelocking_enabled",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
} ]
async def async_setup_entry( async def async_setup_entry(
@ -47,9 +47,9 @@ async def async_setup_entry(
coordinator: NextcloudDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: NextcloudDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities( async_add_entities(
[ [
NextcloudBinarySensor(coordinator, name, entry, BINARY_SENSORS[name]) NextcloudBinarySensor(coordinator, entry, sensor)
for name in coordinator.data for sensor in BINARY_SENSORS
if name in BINARY_SENSORS if sensor.key in coordinator.data
] ]
) )
@ -60,5 +60,5 @@ class NextcloudBinarySensor(NextcloudEntity, BinarySensorEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
val = self.coordinator.data.get(self.item) val = self.coordinator.data.get(self.entity_description.key)
return val is True or val == "yes" return val is True or val == "yes"

View File

@ -18,18 +18,16 @@ class NextcloudEntity(CoordinatorEntity[NextcloudDataUpdateCoordinator]):
def __init__( def __init__(
self, self,
coordinator: NextcloudDataUpdateCoordinator, coordinator: NextcloudDataUpdateCoordinator,
item: str,
entry: ConfigEntry, entry: ConfigEntry,
desc: EntityDescription, description: EntityDescription,
) -> None: ) -> None:
"""Initialize the Nextcloud sensor.""" """Initialize the Nextcloud sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.item = item self._attr_unique_id = f"{coordinator.url}#{description.key}"
self._attr_unique_id = f"{coordinator.url}#{item}"
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
configuration_url=coordinator.url, configuration_url=coordinator.url,
identifiers={(DOMAIN, entry.entry_id)}, identifiers={(DOMAIN, entry.entry_id)},
name=urlparse(coordinator.url).netloc, name=urlparse(coordinator.url).netloc,
sw_version=coordinator.data.get("system_version"), sw_version=coordinator.data.get("system_version"),
) )
self.entity_description = desc self.entity_description = description

View File

@ -21,38 +21,38 @@ from .entity import NextcloudEntity
UNIT_OF_LOAD: Final[str] = "load" UNIT_OF_LOAD: Final[str] = "load"
SENSORS: Final[dict[str, SensorEntityDescription]] = { SENSORS: Final[list[SensorEntityDescription]] = [
"activeUsers_last1hour": SensorEntityDescription( SensorEntityDescription(
key="activeUsers_last1hour", key="activeUsers_last1hour",
translation_key="nextcloud_activeusers_last1hour", translation_key="nextcloud_activeusers_last1hour",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:account-multiple", icon="mdi:account-multiple",
), ),
"activeUsers_last24hours": SensorEntityDescription( SensorEntityDescription(
key="activeUsers_last24hours", key="activeUsers_last24hours",
translation_key="nextcloud_activeusers_last24hours", translation_key="nextcloud_activeusers_last24hours",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:account-multiple", icon="mdi:account-multiple",
), ),
"activeUsers_last5minutes": SensorEntityDescription( SensorEntityDescription(
key="activeUsers_last5minutes", key="activeUsers_last5minutes",
translation_key="nextcloud_activeusers_last5minutes", translation_key="nextcloud_activeusers_last5minutes",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:account-multiple", icon="mdi:account-multiple",
), ),
"database_type": SensorEntityDescription( SensorEntityDescription(
key="database_type", key="database_type",
translation_key="nextcloud_database_type", translation_key="nextcloud_database_type",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:database", icon="mdi:database",
), ),
"database_version": SensorEntityDescription( SensorEntityDescription(
key="database_version", key="database_version",
translation_key="nextcloud_database_version", translation_key="nextcloud_database_version",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:database", icon="mdi:database",
), ),
"server_php_max_execution_time": SensorEntityDescription( SensorEntityDescription(
key="server_php_max_execution_time", key="server_php_max_execution_time",
translation_key="nextcloud_server_php_max_execution_time", translation_key="nextcloud_server_php_max_execution_time",
device_class=SensorDeviceClass.DURATION, device_class=SensorDeviceClass.DURATION,
@ -60,7 +60,7 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
icon="mdi:language-php", icon="mdi:language-php",
native_unit_of_measurement=UnitOfTime.SECONDS, native_unit_of_measurement=UnitOfTime.SECONDS,
), ),
"server_php_memory_limit": SensorEntityDescription( SensorEntityDescription(
key="server_php_memory_limit", key="server_php_memory_limit",
translation_key="nextcloud_server_php_memory_limit", translation_key="nextcloud_server_php_memory_limit",
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
@ -70,7 +70,7 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
suggested_display_precision=1, suggested_display_precision=1,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES, suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
), ),
"server_php_upload_max_filesize": SensorEntityDescription( SensorEntityDescription(
key="server_php_upload_max_filesize", key="server_php_upload_max_filesize",
translation_key="nextcloud_server_php_upload_max_filesize", translation_key="nextcloud_server_php_upload_max_filesize",
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
@ -80,119 +80,119 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
suggested_display_precision=1, suggested_display_precision=1,
suggested_unit_of_measurement=UnitOfInformation.MEGABYTES, suggested_unit_of_measurement=UnitOfInformation.MEGABYTES,
), ),
"server_php_version": SensorEntityDescription( SensorEntityDescription(
key="server_php_version", key="server_php_version",
translation_key="nextcloud_server_php_version", translation_key="nextcloud_server_php_version",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:language-php", icon="mdi:language-php",
), ),
"server_webserver": SensorEntityDescription( SensorEntityDescription(
key="server_webserver", key="server_webserver",
translation_key="nextcloud_server_webserver", translation_key="nextcloud_server_webserver",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_fed_shares_sent": SensorEntityDescription( SensorEntityDescription(
key="shares_num_fed_shares_sent", key="shares_num_fed_shares_sent",
translation_key="nextcloud_shares_num_fed_shares_sent", translation_key="nextcloud_shares_num_fed_shares_sent",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_fed_shares_received": SensorEntityDescription( SensorEntityDescription(
key="shares_num_fed_shares_received", key="shares_num_fed_shares_received",
translation_key="nextcloud_shares_num_fed_shares_received", translation_key="nextcloud_shares_num_fed_shares_received",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_shares": SensorEntityDescription( SensorEntityDescription(
key="shares_num_shares", key="shares_num_shares",
translation_key="nextcloud_shares_num_shares", translation_key="nextcloud_shares_num_shares",
), ),
"shares_num_shares_groups": SensorEntityDescription( SensorEntityDescription(
key="shares_num_shares_groups", key="shares_num_shares_groups",
translation_key="nextcloud_shares_num_shares_groups", translation_key="nextcloud_shares_num_shares_groups",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_shares_link": SensorEntityDescription( SensorEntityDescription(
key="shares_num_shares_link", key="shares_num_shares_link",
translation_key="nextcloud_shares_num_shares_link", translation_key="nextcloud_shares_num_shares_link",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_shares_link_no_password": SensorEntityDescription( SensorEntityDescription(
key="shares_num_shares_link_no_password", key="shares_num_shares_link_no_password",
translation_key="nextcloud_shares_num_shares_link_no_password", translation_key="nextcloud_shares_num_shares_link_no_password",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_shares_mail": SensorEntityDescription( SensorEntityDescription(
key="shares_num_shares_mail", key="shares_num_shares_mail",
translation_key="nextcloud_shares_num_shares_mail", translation_key="nextcloud_shares_num_shares_mail",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_shares_room": SensorEntityDescription( SensorEntityDescription(
key="shares_num_shares_room", key="shares_num_shares_room",
translation_key="nextcloud_shares_num_shares_room", translation_key="nextcloud_shares_num_shares_room",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"shares_num_shares_user": SensorEntityDescription( SensorEntityDescription(
key="server_num_shares_user", key="server_num_shares_user",
translation_key="nextcloud_shares_num_shares_user", translation_key="nextcloud_shares_num_shares_user",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"storage_num_files": SensorEntityDescription( SensorEntityDescription(
key="storage_num_files", key="storage_num_files",
translation_key="nextcloud_storage_num_files", translation_key="nextcloud_storage_num_files",
), ),
"storage_num_storages": SensorEntityDescription( SensorEntityDescription(
key="storage_num_storages", key="storage_num_storages",
translation_key="nextcloud_storage_num_storages", translation_key="nextcloud_storage_num_storages",
), ),
"storage_num_storages_home": SensorEntityDescription( SensorEntityDescription(
key="storage_num_storages_home", key="storage_num_storages_home",
translation_key="nextcloud_storage_num_storages_home", translation_key="nextcloud_storage_num_storages_home",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"storage_num_storages_local": SensorEntityDescription( SensorEntityDescription(
key="storage_num_storages_local", key="storage_num_storages_local",
translation_key="nextcloud_storage_num_storages_local", translation_key="nextcloud_storage_num_storages_local",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"storage_num_storages_other": SensorEntityDescription( SensorEntityDescription(
key="storage_num_storages_other", key="storage_num_storages_other",
translation_key="nextcloud_storage_num_storages_other", translation_key="nextcloud_storage_num_storages_other",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),
"storage_num_users": SensorEntityDescription( SensorEntityDescription(
key="storage_num_users", key="storage_num_users",
translation_key="nextcloud_storage_num_users", translation_key="nextcloud_storage_num_users",
), ),
"system_apps_num_installed": SensorEntityDescription( SensorEntityDescription(
key="system_apps_num_installed", key="system_apps_num_installed",
translation_key="nextcloud_system_apps_num_installed", translation_key="nextcloud_system_apps_num_installed",
), ),
"system_apps_num_updates_available": SensorEntityDescription( SensorEntityDescription(
key="system_apps_num_updates_available", key="system_apps_num_updates_available",
translation_key="nextcloud_system_apps_num_updates_available", translation_key="nextcloud_system_apps_num_updates_available",
icon="mdi:update", icon="mdi:update",
), ),
"system_cpuload_1": SensorEntityDescription( SensorEntityDescription(
key="system_cpuload_1", key="system_cpuload_1",
translation_key="nextcloud_system_cpuload_1", translation_key="nextcloud_system_cpuload_1",
native_unit_of_measurement=UNIT_OF_LOAD, native_unit_of_measurement=UNIT_OF_LOAD,
icon="mdi:chip", icon="mdi:chip",
suggested_display_precision=2, suggested_display_precision=2,
), ),
"system_cpuload_5": SensorEntityDescription( SensorEntityDescription(
key="system_cpuload_5", key="system_cpuload_5",
translation_key="nextcloud_system_cpuload_5", translation_key="nextcloud_system_cpuload_5",
native_unit_of_measurement=UNIT_OF_LOAD, native_unit_of_measurement=UNIT_OF_LOAD,
icon="mdi:chip", icon="mdi:chip",
suggested_display_precision=2, suggested_display_precision=2,
), ),
"system_cpuload_15": SensorEntityDescription( SensorEntityDescription(
key="system_cpuload_15", key="system_cpuload_15",
translation_key="nextcloud_system_cpuload_15", translation_key="nextcloud_system_cpuload_15",
native_unit_of_measurement=UNIT_OF_LOAD, native_unit_of_measurement=UNIT_OF_LOAD,
icon="mdi:chip", icon="mdi:chip",
suggested_display_precision=2, suggested_display_precision=2,
), ),
"system_freespace": SensorEntityDescription( SensorEntityDescription(
key="system_freespace", key="system_freespace",
translation_key="nextcloud_system_freespace", translation_key="nextcloud_system_freespace",
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
@ -201,7 +201,7 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
suggested_display_precision=2, suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES, suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
), ),
"system_mem_free": SensorEntityDescription( SensorEntityDescription(
key="system_mem_free", key="system_mem_free",
translation_key="nextcloud_system_mem_free", translation_key="nextcloud_system_mem_free",
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
@ -210,7 +210,7 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
suggested_display_precision=2, suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES, suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
), ),
"system_mem_total": SensorEntityDescription( SensorEntityDescription(
key="system_mem_total", key="system_mem_total",
translation_key="nextcloud_system_mem_total", translation_key="nextcloud_system_mem_total",
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
@ -219,25 +219,25 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
suggested_display_precision=2, suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES, suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
), ),
"system_memcache.distributed": SensorEntityDescription( SensorEntityDescription(
key="system_memcache.distributed", key="system_memcache.distributed",
translation_key="nextcloud_system_memcache_distributed", translation_key="nextcloud_system_memcache_distributed",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
"system_memcache.local": SensorEntityDescription( SensorEntityDescription(
key="system_memcache.local", key="system_memcache.local",
translation_key="nextcloud_system_memcache_local", translation_key="nextcloud_system_memcache_local",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
"system_memcache.locking": SensorEntityDescription( SensorEntityDescription(
key="system_memcache.locking", key="system_memcache.locking",
translation_key="nextcloud_system_memcache_locking", translation_key="nextcloud_system_memcache_locking",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
"system_swap_total": SensorEntityDescription( SensorEntityDescription(
key="system_swap_total", key="system_swap_total",
translation_key="nextcloud_system_swap_total", translation_key="nextcloud_system_swap_total",
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
@ -246,7 +246,7 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
suggested_display_precision=2, suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES, suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
), ),
"system_swap_free": SensorEntityDescription( SensorEntityDescription(
key="system_swap_free", key="system_swap_free",
translation_key="nextcloud_system_swap_free", translation_key="nextcloud_system_swap_free",
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
@ -255,15 +255,15 @@ SENSORS: Final[dict[str, SensorEntityDescription]] = {
suggested_display_precision=2, suggested_display_precision=2,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES, suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
), ),
"system_theme": SensorEntityDescription( SensorEntityDescription(
key="system_theme", key="system_theme",
translation_key="nextcloud_system_theme", translation_key="nextcloud_system_theme",
), ),
"system_version": SensorEntityDescription( SensorEntityDescription(
key="system_version", key="system_version",
translation_key="nextcloud_system_version", translation_key="nextcloud_system_version",
), ),
} ]
async def async_setup_entry( async def async_setup_entry(
@ -273,9 +273,9 @@ async def async_setup_entry(
coordinator: NextcloudDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: NextcloudDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities( async_add_entities(
[ [
NextcloudSensor(coordinator, name, entry, SENSORS[name]) NextcloudSensor(coordinator, entry, sensor)
for name in coordinator.data for sensor in SENSORS
if name in SENSORS if sensor.key in coordinator.data
] ]
) )
@ -286,7 +286,7 @@ class NextcloudSensor(NextcloudEntity, SensorEntity):
@property @property
def native_value(self) -> StateType | datetime: def native_value(self) -> StateType | datetime:
"""Return the state for this sensor.""" """Return the state for this sensor."""
val = self.coordinator.data.get(self.item) val = self.coordinator.data.get(self.entity_description.key)
if ( if (
getattr(self.entity_description, "device_class", None) getattr(self.entity_description, "device_class", None)
== SensorDeviceClass.TIMESTAMP == SensorDeviceClass.TIMESTAMP