Fix implicit-return in qnap (#122901)

This commit is contained in:
epenet 2024-07-31 12:38:15 +02:00 committed by GitHub
parent 02d4d1a75b
commit 6a45124878
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from typing import Any
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -348,6 +349,8 @@ class QNAPCPUSensor(QNAPSensor):
if self.entity_description.key == "cpu_usage": if self.entity_description.key == "cpu_usage":
return self.coordinator.data["system_stats"]["cpu"]["usage_percent"] return self.coordinator.data["system_stats"]["cpu"]["usage_percent"]
return None
class QNAPMemorySensor(QNAPSensor): class QNAPMemorySensor(QNAPSensor):
"""A QNAP sensor that monitors memory stats.""" """A QNAP sensor that monitors memory stats."""
@ -370,20 +373,25 @@ 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
return None
# Deprecated since Home Assistant 2024.6.0 # Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0 # Can be removed completely in 2024.12.0
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes.""" """Return the state attributes."""
if self.coordinator.data: if self.coordinator.data:
data = self.coordinator.data["system_stats"]["memory"] data = self.coordinator.data["system_stats"]["memory"]
size = round(float(data["total"]) / 1024, 2) size = round(float(data["total"]) / 1024, 2)
return {ATTR_MEMORY_SIZE: f"{size} {UnitOfInformation.GIBIBYTES}"} return {ATTR_MEMORY_SIZE: f"{size} {UnitOfInformation.GIBIBYTES}"}
return None
class QNAPNetworkSensor(QNAPSensor): class QNAPNetworkSensor(QNAPSensor):
"""A QNAP sensor that monitors network stats.""" """A QNAP sensor that monitors network stats."""
monitor_device: str
@property @property
def native_value(self): def native_value(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
@ -404,10 +412,12 @@ class QNAPNetworkSensor(QNAPSensor):
if self.entity_description.key == "network_rx": if self.entity_description.key == "network_rx":
return data["rx"] return data["rx"]
return None
# Deprecated since Home Assistant 2024.6.0 # Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0 # Can be removed completely in 2024.12.0
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes.""" """Return the state attributes."""
if self.coordinator.data: if self.coordinator.data:
data = self.coordinator.data["system_stats"]["nics"][self.monitor_device] data = self.coordinator.data["system_stats"]["nics"][self.monitor_device]
@ -418,6 +428,7 @@ class QNAPNetworkSensor(QNAPSensor):
ATTR_MAX_SPEED: data["max_speed"], ATTR_MAX_SPEED: data["max_speed"],
ATTR_PACKETS_ERR: data["err_packets"], ATTR_PACKETS_ERR: data["err_packets"],
} }
return None
class QNAPSystemSensor(QNAPSensor): class QNAPSystemSensor(QNAPSensor):
@ -442,10 +453,12 @@ class QNAPSystemSensor(QNAPSensor):
) )
return dt_util.now() - uptime_duration return dt_util.now() - uptime_duration
return None
# Deprecated since Home Assistant 2024.6.0 # Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0 # Can be removed completely in 2024.12.0
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes.""" """Return the state attributes."""
if self.coordinator.data: if self.coordinator.data:
data = self.coordinator.data["system_stats"] data = self.coordinator.data["system_stats"]
@ -459,11 +472,14 @@ class QNAPSystemSensor(QNAPSensor):
ATTR_SERIAL: data["system"]["serial_number"], ATTR_SERIAL: data["system"]["serial_number"],
ATTR_UPTIME: f"{days:0>2d}d {hours:0>2d}h {minutes:0>2d}m", ATTR_UPTIME: f"{days:0>2d}d {hours:0>2d}h {minutes:0>2d}m",
} }
return None
class QNAPDriveSensor(QNAPSensor): class QNAPDriveSensor(QNAPSensor):
"""A QNAP sensor that monitors HDD/SSD drive stats.""" """A QNAP sensor that monitors HDD/SSD drive stats."""
monitor_device: str
@property @property
def native_value(self): def native_value(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
@ -475,8 +491,10 @@ class QNAPDriveSensor(QNAPSensor):
if self.entity_description.key == "drive_temp": if self.entity_description.key == "drive_temp":
return int(data["temp_c"]) if data["temp_c"] is not None else 0 return int(data["temp_c"]) if data["temp_c"] is not None else 0
return None
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes.""" """Return the state attributes."""
if self.coordinator.data: if self.coordinator.data:
data = self.coordinator.data["smart_drive_health"][self.monitor_device] data = self.coordinator.data["smart_drive_health"][self.monitor_device]
@ -486,11 +504,14 @@ class QNAPDriveSensor(QNAPSensor):
ATTR_SERIAL: data["serial"], ATTR_SERIAL: data["serial"],
ATTR_TYPE: data["type"], ATTR_TYPE: data["type"],
} }
return None
class QNAPVolumeSensor(QNAPSensor): class QNAPVolumeSensor(QNAPSensor):
"""A QNAP sensor that monitors storage volume stats.""" """A QNAP sensor that monitors storage volume stats."""
monitor_device: str
@property @property
def native_value(self): def native_value(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
@ -511,10 +532,12 @@ 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
return None
# Deprecated since Home Assistant 2024.6.0 # Deprecated since Home Assistant 2024.6.0
# Can be removed completely in 2024.12.0 # Can be removed completely in 2024.12.0
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes.""" """Return the state attributes."""
if self.coordinator.data: if self.coordinator.data:
data = self.coordinator.data["volumes"][self.monitor_device] data = self.coordinator.data["volumes"][self.monitor_device]
@ -523,3 +546,4 @@ class QNAPVolumeSensor(QNAPSensor):
return { return {
ATTR_VOLUME_SIZE: f"{round(total_gb, 1)} {UnitOfInformation.GIBIBYTES}" ATTR_VOLUME_SIZE: f"{round(total_gb, 1)} {UnitOfInformation.GIBIBYTES}"
} }
return None