mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Migrate QNAP extra state attributes to separate states (#109221)
This commit is contained in:
parent
37ffe09add
commit
790d22dc46
@ -36,6 +36,18 @@
|
|||||||
},
|
},
|
||||||
"volume_percentage_used": {
|
"volume_percentage_used": {
|
||||||
"default": "mdi:chart-pie"
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
@ -22,6 +24,7 @@ from homeassistant.exceptions import PlatformNotReady
|
|||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import QnapCoordinator
|
from .coordinator import QnapCoordinator
|
||||||
@ -53,6 +56,13 @@ _SYSTEM_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
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, ...] = (
|
_CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
@ -74,6 +84,17 @@ _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
_MEMORY_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(
|
SensorEntityDescription(
|
||||||
key="memory_free",
|
key="memory_free",
|
||||||
translation_key="memory_free",
|
translation_key="memory_free",
|
||||||
@ -133,6 +154,22 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
suggested_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
|
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, ...] = (
|
_DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
@ -152,6 +189,17 @@ _DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
_VOLUME_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(
|
SensorEntityDescription(
|
||||||
key="volume_size_used",
|
key="volume_size_used",
|
||||||
translation_key="volume_size_used",
|
translation_key="volume_size_used",
|
||||||
@ -312,6 +360,8 @@ class QNAPMemorySensor(QNAPSensor):
|
|||||||
return free
|
return free
|
||||||
|
|
||||||
total = float(self.coordinator.data["system_stats"]["memory"]["total"])
|
total = float(self.coordinator.data["system_stats"]["memory"]["total"])
|
||||||
|
if self.entity_description.key == "memory_size":
|
||||||
|
return total
|
||||||
|
|
||||||
used = total - free
|
used = total - free
|
||||||
if self.entity_description.key == "memory_used":
|
if self.entity_description.key == "memory_used":
|
||||||
@ -320,6 +370,8 @@ class QNAPMemorySensor(QNAPSensor):
|
|||||||
if self.entity_description.key == "memory_percent_used":
|
if self.entity_description.key == "memory_percent_used":
|
||||||
return used / total * 100
|
return used / total * 100
|
||||||
|
|
||||||
|
# Deprecated since Home Assistant 2024.6.0
|
||||||
|
# Can be removed completely in 2024.12.0
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
@ -335,10 +387,16 @@ class QNAPNetworkSensor(QNAPSensor):
|
|||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
if self.entity_description.key == "network_link_status":
|
|
||||||
nic = self.coordinator.data["system_stats"]["nics"][self.monitor_device]
|
nic = self.coordinator.data["system_stats"]["nics"][self.monitor_device]
|
||||||
|
if self.entity_description.key == "network_link_status":
|
||||||
return nic["link_status"]
|
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]
|
data = self.coordinator.data["bandwidth"][self.monitor_device]
|
||||||
if self.entity_description.key == "network_tx":
|
if self.entity_description.key == "network_tx":
|
||||||
return data["tx"]
|
return data["tx"]
|
||||||
@ -346,6 +404,8 @@ class QNAPNetworkSensor(QNAPSensor):
|
|||||||
if self.entity_description.key == "network_rx":
|
if self.entity_description.key == "network_rx":
|
||||||
return data["rx"]
|
return data["rx"]
|
||||||
|
|
||||||
|
# Deprecated since Home Assistant 2024.6.0
|
||||||
|
# Can be removed completely in 2024.12.0
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
@ -372,6 +432,18 @@ class QNAPSystemSensor(QNAPSensor):
|
|||||||
if self.entity_description.key == "system_temp":
|
if self.entity_description.key == "system_temp":
|
||||||
return int(self.coordinator.data["system_stats"]["system"]["temp_c"])
|
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
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
@ -429,6 +501,8 @@ class QNAPVolumeSensor(QNAPSensor):
|
|||||||
return free_gb
|
return free_gb
|
||||||
|
|
||||||
total_gb = int(data["total_size"])
|
total_gb = int(data["total_size"])
|
||||||
|
if self.entity_description.key == "volume_size_total":
|
||||||
|
return total_gb
|
||||||
|
|
||||||
used_gb = total_gb - free_gb
|
used_gb = total_gb - free_gb
|
||||||
if self.entity_description.key == "volume_size_used":
|
if self.entity_description.key == "volume_size_used":
|
||||||
@ -437,6 +511,8 @@ class QNAPVolumeSensor(QNAPSensor):
|
|||||||
if self.entity_description.key == "volume_percentage_used":
|
if self.entity_description.key == "volume_percentage_used":
|
||||||
return used_gb / total_gb * 100
|
return used_gb / total_gb * 100
|
||||||
|
|
||||||
|
# Deprecated since Home Assistant 2024.6.0
|
||||||
|
# Can be removed completely in 2024.12.0
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
|
@ -69,6 +69,21 @@
|
|||||||
},
|
},
|
||||||
"volume_percentage_used": {
|
"volume_percentage_used": {
|
||||||
"name": "Volume used ({monitor_device})"
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user