Remove data rate converting code from NZBGet (#98806)
parent
30628766ae
commit
c4ae9ae430
|
@ -13,6 +13,7 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import CONF_NAME, UnitOfDataRate, UnitOfInformation
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from . import NZBGetEntity
|
||||
|
@ -32,7 +33,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||
key="AverageDownloadRate",
|
||||
name="Average Speed",
|
||||
device_class=SensorDeviceClass.DATA_RATE,
|
||||
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
|
||||
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="DownloadPaused",
|
||||
|
@ -42,7 +44,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||
key="DownloadRate",
|
||||
name="Speed",
|
||||
device_class=SensorDeviceClass.DATA_RATE,
|
||||
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
|
||||
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="DownloadedSizeMB",
|
||||
|
@ -80,7 +83,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||
key="DownloadLimit",
|
||||
name="Speed Limit",
|
||||
device_class=SensorDeviceClass.DATA_RATE,
|
||||
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
|
||||
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
),
|
||||
)
|
||||
|
||||
|
@ -121,30 +125,17 @@ class NZBGetSensor(NZBGetEntity, SensorEntity):
|
|||
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{entry_id}_{description.key}"
|
||||
self._native_value: datetime | None = None
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
def native_value(self) -> StateType | datetime:
|
||||
"""Return the state of the sensor."""
|
||||
sensor_type = self.entity_description.key
|
||||
value = self.coordinator.data["status"].get(sensor_type)
|
||||
|
||||
if value is None:
|
||||
_LOGGER.warning("Unable to locate value for %s", sensor_type)
|
||||
self._native_value = None
|
||||
elif "DownloadRate" in sensor_type and value > 0:
|
||||
# Convert download rate from Bytes/s to MBytes/s
|
||||
self._native_value = round(value / 2**20, 2)
|
||||
elif "DownloadLimit" in sensor_type and value > 0:
|
||||
# Convert download rate from Bytes/s to MBytes/s
|
||||
self._native_value = round(value / 2**20, 2)
|
||||
elif "UpTimeSec" in sensor_type and value > 0:
|
||||
if value is not None and "UpTimeSec" in sensor_type and value > 0:
|
||||
uptime = utcnow().replace(microsecond=0) - timedelta(seconds=value)
|
||||
if not isinstance(self._attr_native_value, datetime) or abs(
|
||||
uptime - self._attr_native_value
|
||||
) > timedelta(seconds=5):
|
||||
self._native_value = uptime
|
||||
else:
|
||||
self._native_value = value
|
||||
|
||||
return self._native_value
|
||||
return uptime
|
||||
return value
|
||||
|
|
|
@ -34,14 +34,14 @@ async def test_sensors(hass: HomeAssistant, nzbget_api) -> None:
|
|||
),
|
||||
"average_speed": (
|
||||
"AverageDownloadRate",
|
||||
"1.19",
|
||||
"1.250000",
|
||||
UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
SensorDeviceClass.DATA_RATE,
|
||||
),
|
||||
"download_paused": ("DownloadPaused", "False", None, None),
|
||||
"speed": (
|
||||
"DownloadRate",
|
||||
"2.38",
|
||||
"2.500000",
|
||||
UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
SensorDeviceClass.DATA_RATE,
|
||||
),
|
||||
|
@ -68,7 +68,7 @@ async def test_sensors(hass: HomeAssistant, nzbget_api) -> None:
|
|||
"uptime": ("UpTimeSec", uptime.isoformat(), None, SensorDeviceClass.TIMESTAMP),
|
||||
"speed_limit": (
|
||||
"DownloadLimit",
|
||||
"0.95",
|
||||
"1.000000",
|
||||
UnitOfDataRate.MEGABYTES_PER_SECOND,
|
||||
SensorDeviceClass.DATA_RATE,
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue