Migrate QNAP extra state attributes to separate states (#109221)

This commit is contained in:
Jan Rieger 2024-07-07 16:18:37 +02:00 committed by GitHub
parent 37ffe09add
commit 790d22dc46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 104 additions and 1 deletions

View File

@ -36,6 +36,18 @@
},
"volume_percentage_used": {
"default": "mdi:chart-pie"
},
"volume_size_total": {
"default": "mdi:chart-pie"
},
"memory_size": {
"default": "mdi:memory"
},
"network_err": {
"default": "mdi:alert"
},
"network_max_speed": {
"default": "mdi:speedometer"
}
}
}

View File

@ -2,6 +2,8 @@
from __future__ import annotations
from datetime import timedelta
from homeassistant import config_entries
from homeassistant.components.sensor import (
SensorDeviceClass,
@ -22,6 +24,7 @@ from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util
from .const import DOMAIN
from .coordinator import QnapCoordinator
@ -53,6 +56,13 @@ _SYSTEM_MON_COND: tuple[SensorEntityDescription, ...] = (
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="uptime",
translation_key="uptime",
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
)
_CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
@ -74,6 +84,17 @@ _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
),
)
_MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="memory_size",
translation_key="memory_size",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
),
SensorEntityDescription(
key="memory_free",
translation_key="memory_free",
@ -133,6 +154,22 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
suggested_display_precision=1,
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
),
SensorEntityDescription(
key="network_err",
translation_key="network_err",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="network_max_speed",
translation_key="network_max_speed",
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
),
)
_DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
@ -152,6 +189,17 @@ _DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
),
)
_VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="volume_size_total",
translation_key="volume_size_total",
native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
),
SensorEntityDescription(
key="volume_size_used",
translation_key="volume_size_used",
@ -312,6 +360,8 @@ class QNAPMemorySensor(QNAPSensor):
return free
total = float(self.coordinator.data["system_stats"]["memory"]["total"])
if self.entity_description.key == "memory_size":
return total
used = total - free
if self.entity_description.key == "memory_used":
@ -320,6 +370,8 @@ class QNAPMemorySensor(QNAPSensor):
if self.entity_description.key == "memory_percent_used":
return used / total * 100
# Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0
@property
def extra_state_attributes(self):
"""Return the state attributes."""
@ -335,10 +387,16 @@ class QNAPNetworkSensor(QNAPSensor):
@property
def native_value(self):
"""Return the state of the sensor."""
nic = self.coordinator.data["system_stats"]["nics"][self.monitor_device]
if self.entity_description.key == "network_link_status":
nic = self.coordinator.data["system_stats"]["nics"][self.monitor_device]
return nic["link_status"]
if self.entity_description.key == "network_max_speed":
return nic["max_speed"]
if self.entity_description.key == "network_err":
return nic["err_packets"]
data = self.coordinator.data["bandwidth"][self.monitor_device]
if self.entity_description.key == "network_tx":
return data["tx"]
@ -346,6 +404,8 @@ class QNAPNetworkSensor(QNAPSensor):
if self.entity_description.key == "network_rx":
return data["rx"]
# Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0
@property
def extra_state_attributes(self):
"""Return the state attributes."""
@ -372,6 +432,18 @@ class QNAPSystemSensor(QNAPSensor):
if self.entity_description.key == "system_temp":
return int(self.coordinator.data["system_stats"]["system"]["temp_c"])
if self.entity_description.key == "uptime":
uptime = self.coordinator.data["system_stats"]["uptime"]
uptime_duration = timedelta(
days=uptime["days"],
hours=uptime["hours"],
minutes=uptime["minutes"],
seconds=uptime["seconds"],
)
return dt_util.now() - uptime_duration
# Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0
@property
def extra_state_attributes(self):
"""Return the state attributes."""
@ -429,6 +501,8 @@ class QNAPVolumeSensor(QNAPSensor):
return free_gb
total_gb = int(data["total_size"])
if self.entity_description.key == "volume_size_total":
return total_gb
used_gb = total_gb - free_gb
if self.entity_description.key == "volume_size_used":
@ -437,6 +511,8 @@ class QNAPVolumeSensor(QNAPSensor):
if self.entity_description.key == "volume_percentage_used":
return used_gb / total_gb * 100
# Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0
@property
def extra_state_attributes(self):
"""Return the state attributes."""

View File

@ -69,6 +69,21 @@
},
"volume_percentage_used": {
"name": "Volume used ({monitor_device})"
},
"uptime": {
"name": "Uptime"
},
"memory_size": {
"name": "Memory size"
},
"network_err": {
"name": "{monitor_device} packet errors"
},
"network_max_speed": {
"name": "{monitor_device} maximum speed"
},
"volume_size_total": {
"name": "Volume size"
}
}
}